Add files using upload-large-folder tool
Browse files- projects/ui/DeepCode/assets/Deepcode.png +3 -0
- projects/ui/DeepCode/assets/logo.png +3 -0
- projects/ui/DeepCode/cli/__init__.py +18 -0
- projects/ui/DeepCode/cli/cli_app.py +352 -0
- projects/ui/DeepCode/cli/cli_interface.py +786 -0
- projects/ui/DeepCode/cli/cli_launcher.py +155 -0
- projects/ui/DeepCode/cli/main_cli.py +280 -0
- projects/ui/DeepCode/config/mcp_tool_definitions.py +375 -0
- projects/ui/DeepCode/config/mcp_tool_definitions_index.py +620 -0
- projects/ui/DeepCode/prompts/code_prompts.py +1768 -0
- projects/ui/DeepCode/schema/mcp-agent.config.schema.json +854 -0
- projects/ui/DeepCode/tools/__init__.py +0 -0
- projects/ui/DeepCode/tools/bocha_search_server.py +219 -0
- projects/ui/DeepCode/tools/code_implementation_server.py +1517 -0
- projects/ui/DeepCode/tools/code_indexer.py +1677 -0
- projects/ui/DeepCode/tools/code_reference_indexer.py +495 -0
- projects/ui/DeepCode/tools/command_executor.py +324 -0
- projects/ui/DeepCode/tools/document_segmentation_server.py +1937 -0
- projects/ui/DeepCode/tools/git_command.py +356 -0
- projects/ui/DeepCode/tools/indexer_config.yaml +141 -0
projects/ui/DeepCode/assets/Deepcode.png
ADDED
|
Git LFS Details
|
projects/ui/DeepCode/assets/logo.png
ADDED
|
Git LFS Details
|
projects/ui/DeepCode/cli/__init__.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
CLI Module for DeepCode Agent
|
| 3 |
+
DeepCode智能体CLI模块
|
| 4 |
+
|
| 5 |
+
包含以下组件 / Contains the following components:
|
| 6 |
+
- cli_app: CLI应用主程序 / CLI application main program
|
| 7 |
+
- cli_interface: CLI界面组件 / CLI interface components
|
| 8 |
+
- cli_launcher: CLI启动器 / CLI launcher
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
__version__ = "1.0.0"
|
| 12 |
+
__author__ = "DeepCode Team - Data Intelligence Lab @ HKU"
|
| 13 |
+
|
| 14 |
+
from .cli_app import main as cli_main
|
| 15 |
+
from .cli_interface import CLIInterface
|
| 16 |
+
from .cli_launcher import main as launcher_main
|
| 17 |
+
|
| 18 |
+
__all__ = ["cli_main", "CLIInterface", "launcher_main"]
|
projects/ui/DeepCode/cli/cli_app.py
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
DeepCode - CLI Application Main Program
|
| 4 |
+
深度代码 - CLI应用主程序
|
| 5 |
+
|
| 6 |
+
🧬 Open-Source Code Agent by Data Intelligence Lab @ HKU
|
| 7 |
+
⚡ Revolutionizing research reproducibility through collaborative AI
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
import sys
|
| 12 |
+
import asyncio
|
| 13 |
+
import time
|
| 14 |
+
import json
|
| 15 |
+
|
| 16 |
+
# 禁止生成.pyc文件
|
| 17 |
+
os.environ["PYTHONDONTWRITEBYTECODE"] = "1"
|
| 18 |
+
|
| 19 |
+
# 添加项目根目录到路径
|
| 20 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 21 |
+
parent_dir = os.path.dirname(current_dir)
|
| 22 |
+
if parent_dir not in sys.path:
|
| 23 |
+
sys.path.insert(0, parent_dir)
|
| 24 |
+
|
| 25 |
+
# 导入MCP应用和工作流
|
| 26 |
+
|
| 27 |
+
from cli.workflows import CLIWorkflowAdapter
|
| 28 |
+
from cli.cli_interface import CLIInterface, Colors
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class CLIApp:
|
| 32 |
+
"""CLI应用主类 - 升级版智能体编排引擎"""
|
| 33 |
+
|
| 34 |
+
def __init__(self):
|
| 35 |
+
self.cli = CLIInterface()
|
| 36 |
+
self.workflow_adapter = CLIWorkflowAdapter(cli_interface=self.cli)
|
| 37 |
+
self.app = None # Will be initialized by workflow adapter
|
| 38 |
+
self.logger = None
|
| 39 |
+
self.context = None
|
| 40 |
+
# Document segmentation configuration
|
| 41 |
+
self.segmentation_config = {"enabled": True, "size_threshold_chars": 50000}
|
| 42 |
+
|
| 43 |
+
async def initialize_mcp_app(self):
|
| 44 |
+
"""初始化MCP应用 - 使用工作流适配器"""
|
| 45 |
+
# Workflow adapter will handle MCP initialization
|
| 46 |
+
return await self.workflow_adapter.initialize_mcp_app()
|
| 47 |
+
|
| 48 |
+
async def cleanup_mcp_app(self):
|
| 49 |
+
"""清理MCP应用 - 使用工作流适配器"""
|
| 50 |
+
await self.workflow_adapter.cleanup_mcp_app()
|
| 51 |
+
|
| 52 |
+
def update_segmentation_config(self):
|
| 53 |
+
"""Update document segmentation configuration in mcp_agent.config.yaml"""
|
| 54 |
+
import yaml
|
| 55 |
+
import os
|
| 56 |
+
|
| 57 |
+
config_path = os.path.join(
|
| 58 |
+
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
| 59 |
+
"mcp_agent.config.yaml",
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
# Read current config
|
| 64 |
+
with open(config_path, "r", encoding="utf-8") as f:
|
| 65 |
+
config = yaml.safe_load(f)
|
| 66 |
+
|
| 67 |
+
# Update document segmentation settings
|
| 68 |
+
if "document_segmentation" not in config:
|
| 69 |
+
config["document_segmentation"] = {}
|
| 70 |
+
|
| 71 |
+
config["document_segmentation"]["enabled"] = self.segmentation_config[
|
| 72 |
+
"enabled"
|
| 73 |
+
]
|
| 74 |
+
config["document_segmentation"]["size_threshold_chars"] = (
|
| 75 |
+
self.segmentation_config["size_threshold_chars"]
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Write updated config
|
| 79 |
+
with open(config_path, "w", encoding="utf-8") as f:
|
| 80 |
+
yaml.dump(config, f, default_flow_style=False, allow_unicode=True)
|
| 81 |
+
|
| 82 |
+
self.cli.print_status(
|
| 83 |
+
"📄 Document segmentation configuration updated", "success"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
self.cli.print_status(
|
| 88 |
+
f"⚠️ Failed to update segmentation config: {str(e)}", "warning"
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
async def process_input(self, input_source: str, input_type: str):
|
| 92 |
+
"""处理输入源(URL或文件)- 使用升级版智能体编排引擎"""
|
| 93 |
+
try:
|
| 94 |
+
# Update segmentation configuration before processing
|
| 95 |
+
self.update_segmentation_config()
|
| 96 |
+
|
| 97 |
+
self.cli.print_separator()
|
| 98 |
+
self.cli.print_status(
|
| 99 |
+
"🚀 Starting intelligent agent orchestration...", "processing"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# 显示处理阶段(根据配置决定)
|
| 103 |
+
self.cli.display_processing_stages(0, self.cli.enable_indexing)
|
| 104 |
+
|
| 105 |
+
# 使用工作流适配器进行处理
|
| 106 |
+
result = await self.workflow_adapter.process_input_with_orchestration(
|
| 107 |
+
input_source=input_source,
|
| 108 |
+
input_type=input_type,
|
| 109 |
+
enable_indexing=self.cli.enable_indexing,
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
if result["status"] == "success":
|
| 113 |
+
# 显示完成状态
|
| 114 |
+
final_stage = 8 if self.cli.enable_indexing else 5
|
| 115 |
+
self.cli.display_processing_stages(
|
| 116 |
+
final_stage, self.cli.enable_indexing
|
| 117 |
+
)
|
| 118 |
+
self.cli.print_status(
|
| 119 |
+
"🎉 Agent orchestration completed successfully!", "complete"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# 显示结果
|
| 123 |
+
self.display_results(
|
| 124 |
+
result.get("analysis_result", ""),
|
| 125 |
+
result.get("download_result", ""),
|
| 126 |
+
result.get("repo_result", ""),
|
| 127 |
+
result.get("pipeline_mode", "comprehensive"),
|
| 128 |
+
)
|
| 129 |
+
else:
|
| 130 |
+
self.cli.print_status(
|
| 131 |
+
f"❌ Processing failed: {result.get('error', 'Unknown error')}",
|
| 132 |
+
"error",
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# 添加到历史记录
|
| 136 |
+
self.cli.add_to_history(input_source, result)
|
| 137 |
+
|
| 138 |
+
return result
|
| 139 |
+
|
| 140 |
+
except Exception as e:
|
| 141 |
+
error_msg = str(e)
|
| 142 |
+
self.cli.print_error_box("Agent Orchestration Error", error_msg)
|
| 143 |
+
self.cli.print_status(f"Error during orchestration: {error_msg}", "error")
|
| 144 |
+
|
| 145 |
+
# 添加错误到历史记录
|
| 146 |
+
error_result = {"status": "error", "error": error_msg}
|
| 147 |
+
self.cli.add_to_history(input_source, error_result)
|
| 148 |
+
|
| 149 |
+
return error_result
|
| 150 |
+
|
| 151 |
+
def display_results(
|
| 152 |
+
self,
|
| 153 |
+
analysis_result: str,
|
| 154 |
+
download_result: str,
|
| 155 |
+
repo_result: str,
|
| 156 |
+
pipeline_mode: str = "comprehensive",
|
| 157 |
+
):
|
| 158 |
+
"""显示处理结果"""
|
| 159 |
+
self.cli.print_results_header()
|
| 160 |
+
|
| 161 |
+
# 显示流水线模式
|
| 162 |
+
if pipeline_mode == "chat":
|
| 163 |
+
mode_display = "💬 Chat Planning Mode"
|
| 164 |
+
elif pipeline_mode == "comprehensive":
|
| 165 |
+
mode_display = "🧠 Comprehensive Mode"
|
| 166 |
+
else:
|
| 167 |
+
mode_display = "⚡ Optimized Mode"
|
| 168 |
+
print(
|
| 169 |
+
f"{Colors.BOLD}{Colors.PURPLE}🤖 PIPELINE MODE: {mode_display}{Colors.ENDC}"
|
| 170 |
+
)
|
| 171 |
+
self.cli.print_separator("─", 79, Colors.PURPLE)
|
| 172 |
+
|
| 173 |
+
print(f"{Colors.BOLD}{Colors.OKCYAN}📊 ANALYSIS PHASE RESULTS:{Colors.ENDC}")
|
| 174 |
+
self.cli.print_separator("─", 79, Colors.CYAN)
|
| 175 |
+
|
| 176 |
+
# 尝试解析并格式化分析结果
|
| 177 |
+
try:
|
| 178 |
+
if analysis_result.strip().startswith("{"):
|
| 179 |
+
parsed_analysis = json.loads(analysis_result)
|
| 180 |
+
print(json.dumps(parsed_analysis, indent=2, ensure_ascii=False))
|
| 181 |
+
else:
|
| 182 |
+
print(
|
| 183 |
+
analysis_result[:1000] + "..."
|
| 184 |
+
if len(analysis_result) > 1000
|
| 185 |
+
else analysis_result
|
| 186 |
+
)
|
| 187 |
+
except Exception:
|
| 188 |
+
print(
|
| 189 |
+
analysis_result[:1000] + "..."
|
| 190 |
+
if len(analysis_result) > 1000
|
| 191 |
+
else analysis_result
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
print(f"\n{Colors.BOLD}{Colors.PURPLE}📥 DOWNLOAD PHASE RESULTS:{Colors.ENDC}")
|
| 195 |
+
self.cli.print_separator("─", 79, Colors.PURPLE)
|
| 196 |
+
print(
|
| 197 |
+
download_result[:1000] + "..."
|
| 198 |
+
if len(download_result) > 1000
|
| 199 |
+
else download_result
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
print(
|
| 203 |
+
f"\n{Colors.BOLD}{Colors.GREEN}⚙️ IMPLEMENTATION PHASE RESULTS:{Colors.ENDC}"
|
| 204 |
+
)
|
| 205 |
+
self.cli.print_separator("─", 79, Colors.GREEN)
|
| 206 |
+
print(repo_result[:1000] + "..." if len(repo_result) > 1000 else repo_result)
|
| 207 |
+
|
| 208 |
+
# 尝试提取生成的代码目录信息
|
| 209 |
+
if "Code generated in:" in repo_result:
|
| 210 |
+
code_dir = (
|
| 211 |
+
repo_result.split("Code generated in:")[-1].strip().split("\n")[0]
|
| 212 |
+
)
|
| 213 |
+
print(
|
| 214 |
+
f"\n{Colors.BOLD}{Colors.YELLOW}📁 Generated Code Directory: {Colors.ENDC}{code_dir}"
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
# 显示处理完成的工作流阶段
|
| 218 |
+
print(
|
| 219 |
+
f"\n{Colors.BOLD}{Colors.OKCYAN}🔄 COMPLETED WORKFLOW STAGES:{Colors.ENDC}"
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
if pipeline_mode == "chat":
|
| 223 |
+
stages = [
|
| 224 |
+
"🚀 Engine Initialization",
|
| 225 |
+
"💬 Requirements Analysis",
|
| 226 |
+
"🏗️ Workspace Setup",
|
| 227 |
+
"📝 Implementation Plan Generation",
|
| 228 |
+
"⚙️ Code Implementation",
|
| 229 |
+
]
|
| 230 |
+
else:
|
| 231 |
+
stages = [
|
| 232 |
+
"📄 Document Processing",
|
| 233 |
+
"🔍 Reference Analysis",
|
| 234 |
+
"📋 Plan Generation",
|
| 235 |
+
"📦 Repository Download",
|
| 236 |
+
"🗂️ Codebase Indexing",
|
| 237 |
+
"⚙️ Code Implementation",
|
| 238 |
+
]
|
| 239 |
+
|
| 240 |
+
for stage in stages:
|
| 241 |
+
print(f" ✅ {stage}")
|
| 242 |
+
|
| 243 |
+
self.cli.print_separator()
|
| 244 |
+
|
| 245 |
+
async def run_interactive_session(self):
|
| 246 |
+
"""运行交互式会话"""
|
| 247 |
+
# 清屏并显示启动界面
|
| 248 |
+
self.cli.clear_screen()
|
| 249 |
+
self.cli.print_logo()
|
| 250 |
+
self.cli.print_welcome_banner()
|
| 251 |
+
|
| 252 |
+
# 初始化MCP应用
|
| 253 |
+
await self.initialize_mcp_app()
|
| 254 |
+
|
| 255 |
+
try:
|
| 256 |
+
# 主交互循环
|
| 257 |
+
while self.cli.is_running:
|
| 258 |
+
self.cli.create_menu()
|
| 259 |
+
choice = self.cli.get_user_input()
|
| 260 |
+
|
| 261 |
+
if choice in ["q", "quit", "exit"]:
|
| 262 |
+
self.cli.print_goodbye()
|
| 263 |
+
break
|
| 264 |
+
|
| 265 |
+
elif choice in ["u", "url"]:
|
| 266 |
+
url = self.cli.get_url_input()
|
| 267 |
+
if url:
|
| 268 |
+
await self.process_input(url, "url")
|
| 269 |
+
|
| 270 |
+
elif choice in ["f", "file"]:
|
| 271 |
+
file_path = self.cli.upload_file_gui()
|
| 272 |
+
if file_path:
|
| 273 |
+
await self.process_input(f"file://{file_path}", "file")
|
| 274 |
+
|
| 275 |
+
elif choice in ["t", "chat", "text"]:
|
| 276 |
+
chat_input = self.cli.get_chat_input()
|
| 277 |
+
if chat_input:
|
| 278 |
+
await self.process_input(chat_input, "chat")
|
| 279 |
+
|
| 280 |
+
elif choice in ["h", "history"]:
|
| 281 |
+
self.cli.show_history()
|
| 282 |
+
|
| 283 |
+
elif choice in ["c", "config", "configure"]:
|
| 284 |
+
# Sync current segmentation config from CLI interface
|
| 285 |
+
self.segmentation_config["enabled"] = self.cli.segmentation_enabled
|
| 286 |
+
self.segmentation_config["size_threshold_chars"] = (
|
| 287 |
+
self.cli.segmentation_threshold
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
self.cli.show_configuration_menu()
|
| 291 |
+
|
| 292 |
+
# Sync back from CLI interface after configuration changes
|
| 293 |
+
self.segmentation_config["enabled"] = self.cli.segmentation_enabled
|
| 294 |
+
self.segmentation_config["size_threshold_chars"] = (
|
| 295 |
+
self.cli.segmentation_threshold
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
+
else:
|
| 299 |
+
self.cli.print_status(
|
| 300 |
+
"Invalid choice. Please select U, F, T, C, H, or Q.", "warning"
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
+
# 询问是否继续
|
| 304 |
+
if self.cli.is_running and choice in ["u", "f", "t", "chat", "text"]:
|
| 305 |
+
if not self.cli.ask_continue():
|
| 306 |
+
self.cli.is_running = False
|
| 307 |
+
self.cli.print_status("Session ended by user", "info")
|
| 308 |
+
|
| 309 |
+
except KeyboardInterrupt:
|
| 310 |
+
print(f"\n{Colors.WARNING}⚠️ Process interrupted by user{Colors.ENDC}")
|
| 311 |
+
except Exception as e:
|
| 312 |
+
print(f"\n{Colors.FAIL}❌ Unexpected error: {str(e)}{Colors.ENDC}")
|
| 313 |
+
finally:
|
| 314 |
+
# 清理资源
|
| 315 |
+
await self.cleanup_mcp_app()
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
async def main():
|
| 319 |
+
"""主函数"""
|
| 320 |
+
start_time = time.time()
|
| 321 |
+
|
| 322 |
+
try:
|
| 323 |
+
# 创建并运行CLI应用
|
| 324 |
+
app = CLIApp()
|
| 325 |
+
await app.run_interactive_session()
|
| 326 |
+
|
| 327 |
+
except KeyboardInterrupt:
|
| 328 |
+
print(f"\n{Colors.WARNING}⚠️ Application interrupted by user{Colors.ENDC}")
|
| 329 |
+
except Exception as e:
|
| 330 |
+
print(f"\n{Colors.FAIL}❌ Application error: {str(e)}{Colors.ENDC}")
|
| 331 |
+
finally:
|
| 332 |
+
end_time = time.time()
|
| 333 |
+
print(
|
| 334 |
+
f"\n{Colors.BOLD}{Colors.CYAN}⏱️ Total runtime: {end_time - start_time:.2f} seconds{Colors.ENDC}"
|
| 335 |
+
)
|
| 336 |
+
|
| 337 |
+
# 清理缓存文件
|
| 338 |
+
print(f"{Colors.YELLOW}🧹 Cleaning up cache files...{Colors.ENDC}")
|
| 339 |
+
if os.name == "nt": # Windows
|
| 340 |
+
os.system(
|
| 341 |
+
"powershell -Command \"Get-ChildItem -Path . -Filter '__pycache__' -Recurse -Directory | Remove-Item -Recurse -Force\" 2>nul"
|
| 342 |
+
)
|
| 343 |
+
else: # Unix/Linux/macOS
|
| 344 |
+
os.system('find . -type d -name "__pycache__" -exec rm -r {} + 2>/dev/null')
|
| 345 |
+
|
| 346 |
+
print(
|
| 347 |
+
f"{Colors.OKGREEN}✨ Goodbye! Thanks for using DeepCode CLI! ✨{Colors.ENDC}"
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
if __name__ == "__main__":
|
| 352 |
+
asyncio.run(main())
|
projects/ui/DeepCode/cli/cli_interface.py
ADDED
|
@@ -0,0 +1,786 @@
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Enhanced CLI Interface Module for DeepCode
|
| 4 |
+
增强版CLI界面模块 - 专为DeepCode设计
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import time
|
| 9 |
+
import platform
|
| 10 |
+
from typing import Optional
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class Colors:
|
| 14 |
+
"""ANSI color codes for terminal styling"""
|
| 15 |
+
|
| 16 |
+
HEADER = "\033[95m"
|
| 17 |
+
OKBLUE = "\033[94m"
|
| 18 |
+
OKCYAN = "\033[96m"
|
| 19 |
+
OKGREEN = "\033[92m"
|
| 20 |
+
WARNING = "\033[93m"
|
| 21 |
+
FAIL = "\033[91m"
|
| 22 |
+
ENDC = "\033[0m"
|
| 23 |
+
BOLD = "\033[1m"
|
| 24 |
+
UNDERLINE = "\033[4m"
|
| 25 |
+
|
| 26 |
+
# Gradient colors
|
| 27 |
+
PURPLE = "\033[35m"
|
| 28 |
+
MAGENTA = "\033[95m"
|
| 29 |
+
BLUE = "\033[34m"
|
| 30 |
+
CYAN = "\033[36m"
|
| 31 |
+
GREEN = "\033[32m"
|
| 32 |
+
YELLOW = "\033[33m"
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class CLIInterface:
|
| 36 |
+
"""Enhanced CLI interface with modern styling for DeepCode"""
|
| 37 |
+
|
| 38 |
+
def __init__(self):
|
| 39 |
+
self.uploaded_file = None
|
| 40 |
+
self.is_running = True
|
| 41 |
+
self.processing_history = []
|
| 42 |
+
self.enable_indexing = True # Default configuration
|
| 43 |
+
self.segmentation_enabled = True # Default to smart segmentation
|
| 44 |
+
self.segmentation_threshold = 50000 # Default threshold
|
| 45 |
+
|
| 46 |
+
# Check tkinter availability for file dialogs
|
| 47 |
+
self.tkinter_available = True
|
| 48 |
+
try:
|
| 49 |
+
import tkinter as tk
|
| 50 |
+
|
| 51 |
+
# Test if tkinter can create a window
|
| 52 |
+
test_root = tk.Tk()
|
| 53 |
+
test_root.withdraw()
|
| 54 |
+
test_root.destroy()
|
| 55 |
+
except Exception:
|
| 56 |
+
self.tkinter_available = False
|
| 57 |
+
|
| 58 |
+
def clear_screen(self):
|
| 59 |
+
"""Clear terminal screen"""
|
| 60 |
+
os.system("cls" if os.name == "nt" else "clear")
|
| 61 |
+
|
| 62 |
+
def print_logo(self):
|
| 63 |
+
"""Print enhanced ASCII logo for DeepCode CLI"""
|
| 64 |
+
logo = f"""
|
| 65 |
+
{Colors.CYAN}╔═══════════════════════════════════════════════════════════════════════════════╗
|
| 66 |
+
║ ║
|
| 67 |
+
║ {Colors.BOLD}{Colors.MAGENTA}██████╗ ███████╗███████╗██████╗ ██████╗ ██████╗ ██████╗ ███████╗{Colors.CYAN} ║
|
| 68 |
+
║ {Colors.BOLD}{Colors.PURPLE}██╔══██╗██╔════╝██╔════╝██╔══██╗██╔════╝██╔═══██╗██╔══██╗██╔════╝{Colors.CYAN} ║
|
| 69 |
+
║ {Colors.BOLD}{Colors.BLUE}██║ ██║█████╗ █████╗ ██████╔╝██║ ██║ ██║██║ ██║█████╗ {Colors.CYAN} ║
|
| 70 |
+
║ {Colors.BOLD}{Colors.OKBLUE}██║ ██║██╔══╝ ██╔══╝ ██╔═══╝ ██║ ██║ ██║██║ ██║██╔══╝ {Colors.CYAN} ║
|
| 71 |
+
║ {Colors.BOLD}{Colors.OKCYAN}██████╔╝███████╗███████╗██║ ╚██████╗╚██████╔╝██████╔╝███████╗{Colors.CYAN} ║
|
| 72 |
+
║ {Colors.BOLD}{Colors.GREEN}╚═════╝ ╚══════╝╚══════╝╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝{Colors.CYAN} ║
|
| 73 |
+
║ ║
|
| 74 |
+
║ {Colors.BOLD}{Colors.GREEN}🧬 OPEN-SOURCE CODE AGENT • DATA INTELLIGENCE LAB @ HKU 🚀 {Colors.CYAN}║
|
| 75 |
+
║ {Colors.BOLD}{Colors.GREEN}⚡ REVOLUTIONIZING RESEARCH REPRODUCIBILITY ⚡ {Colors.CYAN}║
|
| 76 |
+
║ ║
|
| 77 |
+
╚═══════════════════════════════════════════════════════════════════════════════╝{Colors.ENDC}
|
| 78 |
+
"""
|
| 79 |
+
print(logo)
|
| 80 |
+
|
| 81 |
+
def print_welcome_banner(self):
|
| 82 |
+
"""Print enhanced welcome banner"""
|
| 83 |
+
banner = f"""
|
| 84 |
+
{Colors.BOLD}{Colors.CYAN}╔═══════════════════════════════════════════════════════════════════════════════╗
|
| 85 |
+
║ WELCOME TO DEEPCODE CLI ║
|
| 86 |
+
╠═══════════════════════════════════════════════════════════════════════════════╣
|
| 87 |
+
║ {Colors.YELLOW}Open-Source Code Agent | Data Intelligence Lab @ HKU | MIT License {Colors.CYAN}║
|
| 88 |
+
║ {Colors.GREEN}Status: Ready | Engine: Multi-Agent Architecture Initialized {Colors.CYAN}║
|
| 89 |
+
║ {Colors.PURPLE}Mission: Revolutionizing Research Reproducibility {Colors.CYAN}║
|
| 90 |
+
║ ║
|
| 91 |
+
║ {Colors.BOLD}{Colors.OKCYAN}💎 CORE CAPABILITIES:{Colors.ENDC} {Colors.CYAN}║
|
| 92 |
+
║ {Colors.BOLD}{Colors.OKCYAN}▶ Automated Paper-to-Code Reproduction {Colors.CYAN}║
|
| 93 |
+
║ {Colors.BOLD}{Colors.OKCYAN}▶ Collaborative Multi-Agent Architecture {Colors.CYAN}║
|
| 94 |
+
║ {Colors.BOLD}{Colors.OKCYAN}▶ Intelligent Code Implementation & Validation {Colors.CYAN}║
|
| 95 |
+
║ {Colors.BOLD}{Colors.OKCYAN}▶ Future Vision: One Sentence → Complete Codebase {Colors.CYAN}║
|
| 96 |
+
╚═══════════════════════════════════════════════════════════════════════════════╝{Colors.ENDC}
|
| 97 |
+
"""
|
| 98 |
+
print(banner)
|
| 99 |
+
|
| 100 |
+
def print_separator(self, char="═", length=79, color=Colors.CYAN):
|
| 101 |
+
"""Print a styled separator line"""
|
| 102 |
+
print(f"{color}{char * length}{Colors.ENDC}")
|
| 103 |
+
|
| 104 |
+
def print_status(self, message: str, status_type: str = "info"):
|
| 105 |
+
"""Print status message with appropriate styling"""
|
| 106 |
+
status_styles = {
|
| 107 |
+
"success": f"{Colors.OKGREEN}✅",
|
| 108 |
+
"error": f"{Colors.FAIL}❌",
|
| 109 |
+
"warning": f"{Colors.WARNING}⚠️ ",
|
| 110 |
+
"info": f"{Colors.OKBLUE}ℹ️ ",
|
| 111 |
+
"processing": f"{Colors.YELLOW}⏳",
|
| 112 |
+
"upload": f"{Colors.PURPLE}📁",
|
| 113 |
+
"download": f"{Colors.CYAN}📥",
|
| 114 |
+
"analysis": f"{Colors.MAGENTA}🔍",
|
| 115 |
+
"implementation": f"{Colors.GREEN}⚙️ ",
|
| 116 |
+
"complete": f"{Colors.OKGREEN}🎉",
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
icon = status_styles.get(status_type, status_styles["info"])
|
| 120 |
+
timestamp = time.strftime("%H:%M:%S")
|
| 121 |
+
print(
|
| 122 |
+
f"[{Colors.BOLD}{timestamp}{Colors.ENDC}] {icon} {Colors.BOLD}{message}{Colors.ENDC}"
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
def create_menu(self):
|
| 126 |
+
"""Create enhanced interactive menu"""
|
| 127 |
+
# Display current configuration
|
| 128 |
+
pipeline_mode = "🧠 COMPREHENSIVE" if self.enable_indexing else "⚡ OPTIMIZED"
|
| 129 |
+
index_status = "✅ Enabled" if self.enable_indexing else "🔶 Disabled"
|
| 130 |
+
segmentation_mode = (
|
| 131 |
+
"📄 SMART" if self.segmentation_enabled else "📋 TRADITIONAL"
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
menu = f"""
|
| 135 |
+
{Colors.BOLD}{Colors.CYAN}╔═══════════════════════════════════════════════════════════════════════════════╗
|
| 136 |
+
║ MAIN MENU ║
|
| 137 |
+
╠═══════════════════════════════════════════════════════════════════════════════╣
|
| 138 |
+
║ {Colors.OKGREEN}🌐 [U] Process URL {Colors.CYAN}│ {Colors.PURPLE}📁 [F] Upload File {Colors.CYAN}│ {Colors.MAGENTA}💬 [T] Chat Input{Colors.CYAN} ║
|
| 139 |
+
║ {Colors.OKCYAN}⚙️ [C] Configure {Colors.CYAN}│ {Colors.YELLOW}📊 [H] History {Colors.CYAN}│ {Colors.FAIL}❌ [Q] Quit{Colors.CYAN} ║
|
| 140 |
+
║ ║
|
| 141 |
+
║ {Colors.BOLD}🤖 Current Pipeline Mode: {pipeline_mode}{Colors.CYAN} ║
|
| 142 |
+
║ {Colors.BOLD}🗂️ Codebase Indexing: {index_status}{Colors.CYAN} ║
|
| 143 |
+
║ {Colors.BOLD}📄 Document Processing: {segmentation_mode}{Colors.CYAN} ║
|
| 144 |
+
║ ║
|
| 145 |
+
║ {Colors.YELLOW}📝 URL Processing:{Colors.CYAN} ║
|
| 146 |
+
║ {Colors.YELLOW} ▶ Enter research paper URL (arXiv, IEEE, ACM, etc.) {Colors.CYAN}║
|
| 147 |
+
║ {Colors.YELLOW} ▶ Supports direct PDF links and academic paper pages {Colors.CYAN}║
|
| 148 |
+
║ ║
|
| 149 |
+
║ {Colors.PURPLE}📁 File Processing:{Colors.CYAN} ║
|
| 150 |
+
║ {Colors.PURPLE} ▶ Upload PDF, DOCX, PPTX, HTML, or TXT files {Colors.CYAN}║
|
| 151 |
+
║ {Colors.PURPLE} ▶ Intelligent file format detection and processing {Colors.CYAN}║
|
| 152 |
+
║ ║
|
| 153 |
+
║ {Colors.MAGENTA}💬 Chat Input:{Colors.CYAN} ║
|
| 154 |
+
║ {Colors.MAGENTA} ▶ Describe your coding requirements in natural language {Colors.CYAN}║
|
| 155 |
+
║ {Colors.MAGENTA} ▶ AI generates implementation plan and code automatically {Colors.CYAN}║
|
| 156 |
+
║ ║
|
| 157 |
+
║ {Colors.OKCYAN}🔄 Processing Pipeline:{Colors.CYAN} ║
|
| 158 |
+
║ {Colors.OKCYAN} ▶ Intelligent agent orchestration → Code synthesis {Colors.CYAN}║
|
| 159 |
+
║ {Colors.OKCYAN} ▶ Multi-agent coordination with progress tracking {Colors.CYAN}║
|
| 160 |
+
╚═══════════════════════════════════════════════════════════════════════════════╝{Colors.ENDC}
|
| 161 |
+
"""
|
| 162 |
+
print(menu)
|
| 163 |
+
|
| 164 |
+
def get_user_input(self):
|
| 165 |
+
"""Get user input with styled prompt"""
|
| 166 |
+
print(f"\n{Colors.BOLD}{Colors.OKCYAN}➤ Your choice: {Colors.ENDC}", end="")
|
| 167 |
+
return input().strip().lower()
|
| 168 |
+
|
| 169 |
+
def upload_file_gui(self) -> Optional[str]:
|
| 170 |
+
"""Enhanced file upload interface with better error handling"""
|
| 171 |
+
if not self.tkinter_available:
|
| 172 |
+
self.print_status(
|
| 173 |
+
"GUI file dialog not available - using manual input", "warning"
|
| 174 |
+
)
|
| 175 |
+
return self._get_manual_file_path()
|
| 176 |
+
|
| 177 |
+
def select_file():
|
| 178 |
+
try:
|
| 179 |
+
import tkinter as tk
|
| 180 |
+
from tkinter import filedialog
|
| 181 |
+
|
| 182 |
+
root = tk.Tk()
|
| 183 |
+
root.withdraw()
|
| 184 |
+
root.attributes("-topmost", True)
|
| 185 |
+
|
| 186 |
+
file_types = [
|
| 187 |
+
("Research Papers", "*.pdf;*.docx;*.doc"),
|
| 188 |
+
("PDF Files", "*.pdf"),
|
| 189 |
+
("Word Documents", "*.docx;*.doc"),
|
| 190 |
+
("PowerPoint Files", "*.pptx;*.ppt"),
|
| 191 |
+
("HTML Files", "*.html;*.htm"),
|
| 192 |
+
("Text Files", "*.txt;*.md"),
|
| 193 |
+
("All Files", "*.*"),
|
| 194 |
+
]
|
| 195 |
+
|
| 196 |
+
if platform.system() == "Darwin":
|
| 197 |
+
file_types = [
|
| 198 |
+
("Research Papers", ".pdf .docx .doc"),
|
| 199 |
+
("PDF Files", ".pdf"),
|
| 200 |
+
("Word Documents", ".docx .doc"),
|
| 201 |
+
("PowerPoint Files", ".pptx .ppt"),
|
| 202 |
+
("HTML Files", ".html .htm"),
|
| 203 |
+
("Text Files", ".txt .md"),
|
| 204 |
+
("All Files", ".*"),
|
| 205 |
+
]
|
| 206 |
+
|
| 207 |
+
file_path = filedialog.askopenfilename(
|
| 208 |
+
title="Select Research File - DeepCode CLI",
|
| 209 |
+
filetypes=file_types,
|
| 210 |
+
initialdir=os.getcwd(),
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
root.destroy()
|
| 214 |
+
return file_path
|
| 215 |
+
|
| 216 |
+
except Exception as e:
|
| 217 |
+
self.print_status(f"File dialog error: {str(e)}", "error")
|
| 218 |
+
return self._get_manual_file_path()
|
| 219 |
+
|
| 220 |
+
self.print_status("Opening file browser dialog...", "upload")
|
| 221 |
+
file_path = select_file()
|
| 222 |
+
|
| 223 |
+
if file_path:
|
| 224 |
+
self.print_status(
|
| 225 |
+
f"File selected: {os.path.basename(file_path)}", "success"
|
| 226 |
+
)
|
| 227 |
+
return file_path
|
| 228 |
+
else:
|
| 229 |
+
self.print_status("No file selected", "warning")
|
| 230 |
+
return None
|
| 231 |
+
|
| 232 |
+
def _get_manual_file_path(self) -> Optional[str]:
|
| 233 |
+
"""Get file path through manual input with validation"""
|
| 234 |
+
self.print_separator("─", 79, Colors.YELLOW)
|
| 235 |
+
print(f"{Colors.BOLD}{Colors.YELLOW}📁 Manual File Path Input{Colors.ENDC}")
|
| 236 |
+
print(
|
| 237 |
+
f"{Colors.CYAN}Please enter the full path to your research paper file:{Colors.ENDC}"
|
| 238 |
+
)
|
| 239 |
+
print(
|
| 240 |
+
f"{Colors.CYAN}Supported formats: PDF, DOCX, PPTX, HTML, TXT, MD{Colors.ENDC}"
|
| 241 |
+
)
|
| 242 |
+
self.print_separator("─", 79, Colors.YELLOW)
|
| 243 |
+
|
| 244 |
+
while True:
|
| 245 |
+
print(f"\n{Colors.BOLD}{Colors.OKCYAN}📂 File path: {Colors.ENDC}", end="")
|
| 246 |
+
file_path = input().strip()
|
| 247 |
+
|
| 248 |
+
if not file_path:
|
| 249 |
+
self.print_status(
|
| 250 |
+
"Empty path entered. Please try again or press Ctrl+C to cancel.",
|
| 251 |
+
"warning",
|
| 252 |
+
)
|
| 253 |
+
continue
|
| 254 |
+
|
| 255 |
+
file_path = os.path.expanduser(file_path)
|
| 256 |
+
file_path = os.path.abspath(file_path)
|
| 257 |
+
|
| 258 |
+
if not os.path.exists(file_path):
|
| 259 |
+
self.print_status(f"File not found: {file_path}", "error")
|
| 260 |
+
retry = (
|
| 261 |
+
input(f"{Colors.YELLOW}Try again? (y/n): {Colors.ENDC}")
|
| 262 |
+
.strip()
|
| 263 |
+
.lower()
|
| 264 |
+
)
|
| 265 |
+
if retry != "y":
|
| 266 |
+
return None
|
| 267 |
+
continue
|
| 268 |
+
|
| 269 |
+
if not os.path.isfile(file_path):
|
| 270 |
+
self.print_status(f"Path is not a file: {file_path}", "error")
|
| 271 |
+
continue
|
| 272 |
+
|
| 273 |
+
supported_extensions = {
|
| 274 |
+
".pdf",
|
| 275 |
+
".docx",
|
| 276 |
+
".doc",
|
| 277 |
+
".pptx",
|
| 278 |
+
".ppt",
|
| 279 |
+
".html",
|
| 280 |
+
".htm",
|
| 281 |
+
".txt",
|
| 282 |
+
".md",
|
| 283 |
+
}
|
| 284 |
+
file_ext = os.path.splitext(file_path)[1].lower()
|
| 285 |
+
|
| 286 |
+
if file_ext not in supported_extensions:
|
| 287 |
+
self.print_status(f"Unsupported file format: {file_ext}", "warning")
|
| 288 |
+
proceed = (
|
| 289 |
+
input(f"{Colors.YELLOW}Process anyway? (y/n): {Colors.ENDC}")
|
| 290 |
+
.strip()
|
| 291 |
+
.lower()
|
| 292 |
+
)
|
| 293 |
+
if proceed != "y":
|
| 294 |
+
continue
|
| 295 |
+
|
| 296 |
+
self.print_status(
|
| 297 |
+
f"File validated: {os.path.basename(file_path)}", "success"
|
| 298 |
+
)
|
| 299 |
+
return file_path
|
| 300 |
+
|
| 301 |
+
def get_url_input(self) -> str:
|
| 302 |
+
"""Enhanced URL input with validation"""
|
| 303 |
+
self.print_separator("─", 79, Colors.GREEN)
|
| 304 |
+
print(f"{Colors.BOLD}{Colors.GREEN}🌐 URL Input Interface{Colors.ENDC}")
|
| 305 |
+
print(
|
| 306 |
+
f"{Colors.CYAN}Enter a research paper URL from supported platforms:{Colors.ENDC}"
|
| 307 |
+
)
|
| 308 |
+
print(
|
| 309 |
+
f"{Colors.CYAN}• arXiv (arxiv.org) • IEEE Xplore (ieeexplore.ieee.org){Colors.ENDC}"
|
| 310 |
+
)
|
| 311 |
+
print(
|
| 312 |
+
f"{Colors.CYAN}• ACM Digital Library • SpringerLink • Nature • Science{Colors.ENDC}"
|
| 313 |
+
)
|
| 314 |
+
print(
|
| 315 |
+
f"{Colors.CYAN}• Direct PDF links • Academic publisher websites{Colors.ENDC}"
|
| 316 |
+
)
|
| 317 |
+
self.print_separator("─", 79, Colors.GREEN)
|
| 318 |
+
|
| 319 |
+
while True:
|
| 320 |
+
print(f"\n{Colors.BOLD}{Colors.OKCYAN}🔗 URL: {Colors.ENDC}", end="")
|
| 321 |
+
url = input().strip()
|
| 322 |
+
|
| 323 |
+
if not url:
|
| 324 |
+
self.print_status(
|
| 325 |
+
"Empty URL entered. Please try again or press Ctrl+C to cancel.",
|
| 326 |
+
"warning",
|
| 327 |
+
)
|
| 328 |
+
continue
|
| 329 |
+
|
| 330 |
+
if not url.startswith(("http://", "https://")):
|
| 331 |
+
self.print_status("URL must start with http:// or https://", "error")
|
| 332 |
+
retry = (
|
| 333 |
+
input(f"{Colors.YELLOW}Try again? (y/n): {Colors.ENDC}")
|
| 334 |
+
.strip()
|
| 335 |
+
.lower()
|
| 336 |
+
)
|
| 337 |
+
if retry != "y":
|
| 338 |
+
return ""
|
| 339 |
+
continue
|
| 340 |
+
|
| 341 |
+
academic_domains = [
|
| 342 |
+
"arxiv.org",
|
| 343 |
+
"ieeexplore.ieee.org",
|
| 344 |
+
"dl.acm.org",
|
| 345 |
+
"link.springer.com",
|
| 346 |
+
"nature.com",
|
| 347 |
+
"science.org",
|
| 348 |
+
"scholar.google.com",
|
| 349 |
+
"researchgate.net",
|
| 350 |
+
"semanticscholar.org",
|
| 351 |
+
]
|
| 352 |
+
|
| 353 |
+
is_academic = any(domain in url.lower() for domain in academic_domains)
|
| 354 |
+
if not is_academic and not url.lower().endswith(".pdf"):
|
| 355 |
+
self.print_status(
|
| 356 |
+
"URL doesn't appear to be from a known academic platform", "warning"
|
| 357 |
+
)
|
| 358 |
+
proceed = (
|
| 359 |
+
input(f"{Colors.YELLOW}Process anyway? (y/n): {Colors.ENDC}")
|
| 360 |
+
.strip()
|
| 361 |
+
.lower()
|
| 362 |
+
)
|
| 363 |
+
if proceed != "y":
|
| 364 |
+
continue
|
| 365 |
+
|
| 366 |
+
self.print_status(f"URL validated: {url}", "success")
|
| 367 |
+
return url
|
| 368 |
+
|
| 369 |
+
def get_chat_input(self) -> str:
|
| 370 |
+
"""Enhanced chat input interface for coding requirements"""
|
| 371 |
+
self.print_separator("─", 79, Colors.PURPLE)
|
| 372 |
+
print(f"{Colors.BOLD}{Colors.PURPLE}💬 Chat Input Interface{Colors.ENDC}")
|
| 373 |
+
print(
|
| 374 |
+
f"{Colors.CYAN}Describe your coding requirements in natural language.{Colors.ENDC}"
|
| 375 |
+
)
|
| 376 |
+
print(
|
| 377 |
+
f"{Colors.CYAN}Our AI will analyze your needs and generate a comprehensive implementation plan.{Colors.ENDC}"
|
| 378 |
+
)
|
| 379 |
+
self.print_separator("─", 79, Colors.PURPLE)
|
| 380 |
+
|
| 381 |
+
# Display examples to help users
|
| 382 |
+
print(f"\n{Colors.BOLD}{Colors.YELLOW}💡 Examples:{Colors.ENDC}")
|
| 383 |
+
print(f"{Colors.CYAN}Academic Research:{Colors.ENDC}")
|
| 384 |
+
print(
|
| 385 |
+
" • 'I need to implement a reinforcement learning algorithm for robotic control'"
|
| 386 |
+
)
|
| 387 |
+
print(
|
| 388 |
+
" • 'Create a neural network for image classification with attention mechanisms'"
|
| 389 |
+
)
|
| 390 |
+
print(f"{Colors.CYAN}Engineering Projects:{Colors.ENDC}")
|
| 391 |
+
print(
|
| 392 |
+
" • 'Develop a web application for project management with user authentication'"
|
| 393 |
+
)
|
| 394 |
+
print(" • 'Create a data visualization dashboard for sales analytics'")
|
| 395 |
+
print(f"{Colors.CYAN}Mixed Projects:{Colors.ENDC}")
|
| 396 |
+
print(
|
| 397 |
+
" • 'Implement a machine learning model with a web interface for real-time predictions'"
|
| 398 |
+
)
|
| 399 |
+
|
| 400 |
+
self.print_separator("─", 79, Colors.PURPLE)
|
| 401 |
+
|
| 402 |
+
print(
|
| 403 |
+
f"\n{Colors.BOLD}{Colors.OKCYAN}✏️ Enter your coding requirements below:{Colors.ENDC}"
|
| 404 |
+
)
|
| 405 |
+
print(
|
| 406 |
+
f"{Colors.YELLOW}(Type your description, press Enter twice when finished, or Ctrl+C to cancel){Colors.ENDC}"
|
| 407 |
+
)
|
| 408 |
+
|
| 409 |
+
lines = []
|
| 410 |
+
empty_line_count = 0
|
| 411 |
+
|
| 412 |
+
while True:
|
| 413 |
+
try:
|
| 414 |
+
if len(lines) == 0:
|
| 415 |
+
print(f"{Colors.BOLD}> {Colors.ENDC}", end="")
|
| 416 |
+
else:
|
| 417 |
+
print(f"{Colors.BOLD} {Colors.ENDC}", end="")
|
| 418 |
+
|
| 419 |
+
line = input()
|
| 420 |
+
|
| 421 |
+
if line.strip() == "":
|
| 422 |
+
empty_line_count += 1
|
| 423 |
+
if empty_line_count >= 2:
|
| 424 |
+
# Two consecutive empty lines means user finished input
|
| 425 |
+
break
|
| 426 |
+
lines.append("") # Keep empty line for formatting
|
| 427 |
+
else:
|
| 428 |
+
empty_line_count = 0
|
| 429 |
+
lines.append(line)
|
| 430 |
+
|
| 431 |
+
except KeyboardInterrupt:
|
| 432 |
+
print(f"\n{Colors.WARNING}Input cancelled by user{Colors.ENDC}")
|
| 433 |
+
return ""
|
| 434 |
+
|
| 435 |
+
# Join all lines and clean up
|
| 436 |
+
user_input = "\n".join(lines).strip()
|
| 437 |
+
|
| 438 |
+
if not user_input:
|
| 439 |
+
self.print_status("No input provided", "warning")
|
| 440 |
+
return ""
|
| 441 |
+
|
| 442 |
+
if len(user_input) < 20:
|
| 443 |
+
self.print_status(
|
| 444 |
+
"Input too short. Please provide more detailed requirements (at least 20 characters)",
|
| 445 |
+
"warning",
|
| 446 |
+
)
|
| 447 |
+
retry = (
|
| 448 |
+
input(f"{Colors.YELLOW}Try again? (y/n): {Colors.ENDC}").strip().lower()
|
| 449 |
+
)
|
| 450 |
+
if retry == "y":
|
| 451 |
+
return self.get_chat_input() # Recursive call for retry
|
| 452 |
+
return ""
|
| 453 |
+
|
| 454 |
+
# Display input summary
|
| 455 |
+
word_count = len(user_input.split())
|
| 456 |
+
char_count = len(user_input)
|
| 457 |
+
|
| 458 |
+
print(f"\n{Colors.BOLD}{Colors.GREEN}📋 Input Summary:{Colors.ENDC}")
|
| 459 |
+
print(f" • {Colors.CYAN}Word count: {word_count}{Colors.ENDC}")
|
| 460 |
+
print(f" • {Colors.CYAN}Character count: {char_count}{Colors.ENDC}")
|
| 461 |
+
|
| 462 |
+
# Show preview
|
| 463 |
+
preview = user_input[:200] + "..." if len(user_input) > 200 else user_input
|
| 464 |
+
print(f"\n{Colors.BOLD}{Colors.CYAN}📄 Preview:{Colors.ENDC}")
|
| 465 |
+
print(f"{Colors.YELLOW}{preview}{Colors.ENDC}")
|
| 466 |
+
|
| 467 |
+
# Confirm with user
|
| 468 |
+
confirm = (
|
| 469 |
+
input(
|
| 470 |
+
f"\n{Colors.BOLD}{Colors.OKCYAN}Proceed with this input? (y/n): {Colors.ENDC}"
|
| 471 |
+
)
|
| 472 |
+
.strip()
|
| 473 |
+
.lower()
|
| 474 |
+
)
|
| 475 |
+
if confirm != "y":
|
| 476 |
+
retry = (
|
| 477 |
+
input(f"{Colors.YELLOW}Edit input? (y/n): {Colors.ENDC}")
|
| 478 |
+
.strip()
|
| 479 |
+
.lower()
|
| 480 |
+
)
|
| 481 |
+
if retry == "y":
|
| 482 |
+
return self.get_chat_input() # Recursive call for retry
|
| 483 |
+
return ""
|
| 484 |
+
|
| 485 |
+
self.print_status(
|
| 486 |
+
f"Chat input captured: {word_count} words, {char_count} characters",
|
| 487 |
+
"success",
|
| 488 |
+
)
|
| 489 |
+
return user_input
|
| 490 |
+
|
| 491 |
+
def show_progress_bar(self, message: str, duration: float = 2.0):
|
| 492 |
+
"""Show animated progress bar"""
|
| 493 |
+
print(f"\n{Colors.BOLD}{Colors.CYAN}{message}{Colors.ENDC}")
|
| 494 |
+
|
| 495 |
+
bar_length = 50
|
| 496 |
+
for i in range(bar_length + 1):
|
| 497 |
+
percent = (i / bar_length) * 100
|
| 498 |
+
filled = "█" * i
|
| 499 |
+
empty = "░" * (bar_length - i)
|
| 500 |
+
|
| 501 |
+
print(
|
| 502 |
+
f"\r{Colors.OKGREEN}[{filled}{empty}] {percent:3.0f}%{Colors.ENDC}",
|
| 503 |
+
end="",
|
| 504 |
+
flush=True,
|
| 505 |
+
)
|
| 506 |
+
time.sleep(duration / bar_length)
|
| 507 |
+
|
| 508 |
+
print(f"\n{Colors.OKGREEN}✓ {message} completed{Colors.ENDC}")
|
| 509 |
+
|
| 510 |
+
def show_spinner(self, message: str, duration: float = 1.0):
|
| 511 |
+
"""Show spinner animation"""
|
| 512 |
+
spinner_chars = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
|
| 513 |
+
end_time = time.time() + duration
|
| 514 |
+
|
| 515 |
+
print(
|
| 516 |
+
f"{Colors.BOLD}{Colors.CYAN}{message}... {Colors.ENDC}", end="", flush=True
|
| 517 |
+
)
|
| 518 |
+
|
| 519 |
+
i = 0
|
| 520 |
+
while time.time() < end_time:
|
| 521 |
+
print(
|
| 522 |
+
f"\r{Colors.BOLD}{Colors.CYAN}{message}... {Colors.YELLOW}{spinner_chars[i % len(spinner_chars)]}{Colors.ENDC}",
|
| 523 |
+
end="",
|
| 524 |
+
flush=True,
|
| 525 |
+
)
|
| 526 |
+
time.sleep(0.1)
|
| 527 |
+
i += 1
|
| 528 |
+
|
| 529 |
+
print(
|
| 530 |
+
f"\r{Colors.BOLD}{Colors.CYAN}{message}... {Colors.OKGREEN}✓{Colors.ENDC}"
|
| 531 |
+
)
|
| 532 |
+
|
| 533 |
+
def display_processing_stages(
|
| 534 |
+
self,
|
| 535 |
+
current_stage: int = 0,
|
| 536 |
+
enable_indexing: bool = True,
|
| 537 |
+
chat_mode: bool = False,
|
| 538 |
+
):
|
| 539 |
+
"""Display processing pipeline stages with current progress"""
|
| 540 |
+
if chat_mode:
|
| 541 |
+
# Chat mode - simplified workflow for user requirements
|
| 542 |
+
stages = [
|
| 543 |
+
("🚀", "Initialize", "Setting up chat engine"),
|
| 544 |
+
("💬", "Planning", "Analyzing requirements"),
|
| 545 |
+
("🏗️", "Setup", "Creating workspace"),
|
| 546 |
+
("📝", "Save Plan", "Saving implementation plan"),
|
| 547 |
+
("⚙️", "Implement", "Generating code"),
|
| 548 |
+
]
|
| 549 |
+
pipeline_mode = "CHAT PLANNING"
|
| 550 |
+
elif enable_indexing:
|
| 551 |
+
# Full pipeline with all stages
|
| 552 |
+
stages = [
|
| 553 |
+
("🚀", "Initialize", "Setting up AI engine"),
|
| 554 |
+
("📊", "Analyze", "Analyzing research content"),
|
| 555 |
+
("📥", "Download", "Processing document"),
|
| 556 |
+
("📋", "Plan", "Generating code architecture"),
|
| 557 |
+
("🔍", "References", "Analyzing references"),
|
| 558 |
+
("📦", "Repos", "Downloading repositories"),
|
| 559 |
+
("🗂️", "Index", "Building code index"),
|
| 560 |
+
("⚙️", "Implement", "Implementing code"),
|
| 561 |
+
]
|
| 562 |
+
pipeline_mode = "COMPREHENSIVE"
|
| 563 |
+
else:
|
| 564 |
+
# Fast mode - skip indexing related stages
|
| 565 |
+
stages = [
|
| 566 |
+
("🚀", "Initialize", "Setting up AI engine"),
|
| 567 |
+
("📊", "Analyze", "Analyzing research content"),
|
| 568 |
+
("📥", "Download", "Processing document"),
|
| 569 |
+
("📋", "Plan", "Generating code architecture"),
|
| 570 |
+
("⚙️", "Implement", "Implementing code"),
|
| 571 |
+
]
|
| 572 |
+
pipeline_mode = "OPTIMIZED"
|
| 573 |
+
|
| 574 |
+
print(
|
| 575 |
+
f"\n{Colors.BOLD}{Colors.CYAN}📋 {pipeline_mode} PIPELINE STATUS{Colors.ENDC}"
|
| 576 |
+
)
|
| 577 |
+
self.print_separator("─", 79, Colors.CYAN)
|
| 578 |
+
|
| 579 |
+
for i, (icon, name, desc) in enumerate(stages):
|
| 580 |
+
if i < current_stage:
|
| 581 |
+
status = f"{Colors.OKGREEN}✓ COMPLETED{Colors.ENDC}"
|
| 582 |
+
elif i == current_stage:
|
| 583 |
+
status = f"{Colors.YELLOW}⏳ IN PROGRESS{Colors.ENDC}"
|
| 584 |
+
else:
|
| 585 |
+
status = f"{Colors.CYAN}⏸️ PENDING{Colors.ENDC}"
|
| 586 |
+
|
| 587 |
+
print(
|
| 588 |
+
f"{icon} {Colors.BOLD}{name:<12}{Colors.ENDC} │ {desc:<25} │ {status}"
|
| 589 |
+
)
|
| 590 |
+
|
| 591 |
+
self.print_separator("─", 79, Colors.CYAN)
|
| 592 |
+
|
| 593 |
+
def print_results_header(self):
|
| 594 |
+
"""Print results section header"""
|
| 595 |
+
header = f"""
|
| 596 |
+
{Colors.BOLD}{Colors.OKGREEN}╔═══════════════════════════════════════════════════════════════════════════════╗
|
| 597 |
+
║ PROCESSING RESULTS ║
|
| 598 |
+
╚═══════════════════════════════════════════════════════════════════════════════╝{Colors.ENDC}
|
| 599 |
+
"""
|
| 600 |
+
print(header)
|
| 601 |
+
|
| 602 |
+
def print_error_box(self, title: str, error_msg: str):
|
| 603 |
+
"""Print formatted error box"""
|
| 604 |
+
print(
|
| 605 |
+
f"\n{Colors.FAIL}╔══════════════════════════════════════════════════════════════╗"
|
| 606 |
+
)
|
| 607 |
+
print(f"║ {Colors.BOLD}ERROR: {title:<50}{Colors.FAIL} ║")
|
| 608 |
+
print("╠══════════════════════════════════════════════════════════════╣")
|
| 609 |
+
|
| 610 |
+
words = error_msg.split()
|
| 611 |
+
lines = []
|
| 612 |
+
current_line = ""
|
| 613 |
+
|
| 614 |
+
for word in words:
|
| 615 |
+
if len(current_line + word) <= 54:
|
| 616 |
+
current_line += word + " "
|
| 617 |
+
else:
|
| 618 |
+
lines.append(current_line.strip())
|
| 619 |
+
current_line = word + " "
|
| 620 |
+
if current_line:
|
| 621 |
+
lines.append(current_line.strip())
|
| 622 |
+
|
| 623 |
+
for line in lines:
|
| 624 |
+
print(f"║ {line:<56} ║")
|
| 625 |
+
|
| 626 |
+
print(
|
| 627 |
+
f"╚══════════════════════════════════════════════════════════════╝{Colors.ENDC}"
|
| 628 |
+
)
|
| 629 |
+
|
| 630 |
+
def cleanup_cache(self):
|
| 631 |
+
"""清理Python缓存文件 / Clean up Python cache files"""
|
| 632 |
+
try:
|
| 633 |
+
self.print_status("Cleaning up cache files...", "info")
|
| 634 |
+
# 清理__pycache__目录
|
| 635 |
+
os.system('find . -type d -name "__pycache__" -exec rm -r {} + 2>/dev/null')
|
| 636 |
+
# 清理.pyc文件
|
| 637 |
+
os.system('find . -name "*.pyc" -delete 2>/dev/null')
|
| 638 |
+
self.print_status("Cache cleanup completed", "success")
|
| 639 |
+
except Exception as e:
|
| 640 |
+
self.print_status(f"Cache cleanup failed: {e}", "warning")
|
| 641 |
+
|
| 642 |
+
def print_goodbye(self):
|
| 643 |
+
"""Print goodbye message"""
|
| 644 |
+
# 清理缓存文件
|
| 645 |
+
self.cleanup_cache()
|
| 646 |
+
|
| 647 |
+
goodbye = f"""
|
| 648 |
+
{Colors.BOLD}{Colors.CYAN}╔═════════════════════════════════════════════════════���═════════════════════════╗
|
| 649 |
+
║ GOODBYE ║
|
| 650 |
+
╠═══════════════════════════════════════════════════════════════════════════════╣
|
| 651 |
+
║ {Colors.OKGREEN}🎉 Thank you for using DeepCode CLI! {Colors.CYAN}║
|
| 652 |
+
║ ║
|
| 653 |
+
║ {Colors.YELLOW}🧬 Join our community in revolutionizing research reproducibility {Colors.CYAN}║
|
| 654 |
+
║ {Colors.PURPLE}⚡ Together, we're building the future of automated code generation {Colors.CYAN}║
|
| 655 |
+
║ ║
|
| 656 |
+
║ {Colors.OKCYAN}💡 Questions? Contribute to our open-source mission at GitHub {Colors.CYAN}║
|
| 657 |
+
║ {Colors.GREEN}🧹 Cache files cleaned up for optimal performance {Colors.CYAN}║
|
| 658 |
+
║ ║
|
| 659 |
+
╚═══════════════════════════════════════════════════════════════════════════════╝{Colors.ENDC}
|
| 660 |
+
"""
|
| 661 |
+
print(goodbye)
|
| 662 |
+
|
| 663 |
+
def ask_continue(self) -> bool:
|
| 664 |
+
"""Ask if user wants to continue with another paper"""
|
| 665 |
+
self.print_separator("─", 79, Colors.YELLOW)
|
| 666 |
+
print(f"\n{Colors.BOLD}{Colors.YELLOW}🔄 Process another paper?{Colors.ENDC}")
|
| 667 |
+
choice = input(f"{Colors.OKCYAN}Continue? (y/n): {Colors.ENDC}").strip().lower()
|
| 668 |
+
return choice in ["y", "yes", "1", "true"]
|
| 669 |
+
|
| 670 |
+
def add_to_history(self, input_source: str, result: dict):
|
| 671 |
+
"""Add processing result to history"""
|
| 672 |
+
entry = {
|
| 673 |
+
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
| 674 |
+
"input_source": input_source,
|
| 675 |
+
"status": result.get("status", "unknown"),
|
| 676 |
+
"result": result,
|
| 677 |
+
}
|
| 678 |
+
self.processing_history.append(entry)
|
| 679 |
+
|
| 680 |
+
def show_history(self):
|
| 681 |
+
"""Display processing history"""
|
| 682 |
+
if not self.processing_history:
|
| 683 |
+
self.print_status("No processing history available", "info")
|
| 684 |
+
return
|
| 685 |
+
|
| 686 |
+
print(f"\n{Colors.BOLD}{Colors.CYAN}📚 PROCESSING HISTORY{Colors.ENDC}")
|
| 687 |
+
self.print_separator("─", 79, Colors.CYAN)
|
| 688 |
+
|
| 689 |
+
for i, entry in enumerate(self.processing_history, 1):
|
| 690 |
+
status_icon = "✅" if entry["status"] == "success" else "❌"
|
| 691 |
+
source = entry["input_source"]
|
| 692 |
+
if len(source) > 50:
|
| 693 |
+
source = source[:47] + "..."
|
| 694 |
+
|
| 695 |
+
print(f"{i}. {status_icon} {entry['timestamp']} | {source}")
|
| 696 |
+
|
| 697 |
+
self.print_separator("─", 79, Colors.CYAN)
|
| 698 |
+
|
| 699 |
+
def show_configuration_menu(self):
|
| 700 |
+
"""Show configuration options menu"""
|
| 701 |
+
self.clear_screen()
|
| 702 |
+
|
| 703 |
+
# Get segmentation config status
|
| 704 |
+
segmentation_enabled = getattr(self, "segmentation_enabled", True)
|
| 705 |
+
segmentation_threshold = getattr(self, "segmentation_threshold", 50000)
|
| 706 |
+
|
| 707 |
+
print(f"""
|
| 708 |
+
{Colors.BOLD}{Colors.CYAN}╔═══════════════════════════════════════════════════════════════════════════════╗
|
| 709 |
+
║ CONFIGURATION MENU ║
|
| 710 |
+
╠═══════════════════════════════════════════════════════════════════════════════╣
|
| 711 |
+
║ ║
|
| 712 |
+
║ {Colors.BOLD}🤖 Agent Orchestration Engine Configuration{Colors.CYAN} ║
|
| 713 |
+
║ ║
|
| 714 |
+
║ {Colors.OKCYAN}[1] Pipeline Mode:{Colors.CYAN} ║
|
| 715 |
+
║ {Colors.BOLD}🧠 Comprehensive Mode{Colors.CYAN} - Full intelligence analysis (Default) ║
|
| 716 |
+
║ ✓ Research Analysis + Resource Processing ║
|
| 717 |
+
║ ✓ Reference Intelligence Discovery ║
|
| 718 |
+
║ ✓ Automated Repository Acquisition ║
|
| 719 |
+
║ ✓ Codebase Intelligence Orchestration ║
|
| 720 |
+
║ ✓ Intelligent Code Implementation Synthesis ║
|
| 721 |
+
║ ║
|
| 722 |
+
║ {Colors.BOLD}⚡ Optimized Mode{Colors.CYAN} - Fast processing (Skip indexing) ║
|
| 723 |
+
║ ✓ Research Analysis + Resource Processing ║
|
| 724 |
+
║ ✓ Code Architecture Synthesis ║
|
| 725 |
+
║ ✓ Intelligent Code Implementation Synthesis ║
|
| 726 |
+
║ ✗ Reference Intelligence Discovery (Skipped) ║
|
| 727 |
+
║ ✗ Repository Acquisition (Skipped) ║
|
| 728 |
+
║ ✗ Codebase Intelligence Orchestration (Skipped) ║
|
| 729 |
+
║ ║
|
| 730 |
+
║ {Colors.OKCYAN}[2] Document Processing:{Colors.CYAN} ║
|
| 731 |
+
║ {Colors.BOLD}📄 Smart Segmentation{Colors.CYAN} - Intelligent document analysis (Default) ║
|
| 732 |
+
║ ✓ Semantic boundary detection ║
|
| 733 |
+
║ ✓ Algorithm integrity preservation ║
|
| 734 |
+
║ ✓ Formula chain recognition ║
|
| 735 |
+
║ ✓ Adaptive character limits ║
|
| 736 |
+
║ ║
|
| 737 |
+
║ {Colors.BOLD}📋 Traditional Processing{Colors.CYAN} - Full document reading ║
|
| 738 |
+
║ ✓ Complete document analysis ║
|
| 739 |
+
║ ✗ Smart segmentation (Disabled) ║
|
| 740 |
+
║ ║
|
| 741 |
+
║ {Colors.YELLOW}Current Settings:{Colors.CYAN} ║
|
| 742 |
+
║ Pipeline: {'🧠 Comprehensive Mode' if self.enable_indexing else '⚡ Optimized Mode'} ║
|
| 743 |
+
║ Document: {'📄 Smart Segmentation' if segmentation_enabled else '📋 Traditional Processing'} ║
|
| 744 |
+
║ Threshold: {segmentation_threshold} characters ║
|
| 745 |
+
║ ║
|
| 746 |
+
║ {Colors.OKGREEN}[T] Toggle Pipeline {Colors.BLUE}[S] Toggle Segmentation {Colors.FAIL}[B] Back{Colors.CYAN} ║
|
| 747 |
+
╚═══════════════════════════════════════════════════════════════════════════════╝{Colors.ENDC}
|
| 748 |
+
""")
|
| 749 |
+
|
| 750 |
+
while True:
|
| 751 |
+
print(
|
| 752 |
+
f"\n{Colors.BOLD}{Colors.OKCYAN}➤ Configuration choice: {Colors.ENDC}",
|
| 753 |
+
end="",
|
| 754 |
+
)
|
| 755 |
+
choice = input().strip().lower()
|
| 756 |
+
|
| 757 |
+
if choice in ["t", "toggle"]:
|
| 758 |
+
self.enable_indexing = not self.enable_indexing
|
| 759 |
+
mode = "🧠 Comprehensive" if self.enable_indexing else "⚡ Optimized"
|
| 760 |
+
self.print_status(f"Pipeline mode switched to: {mode}", "success")
|
| 761 |
+
time.sleep(1)
|
| 762 |
+
self.show_configuration_menu()
|
| 763 |
+
return
|
| 764 |
+
|
| 765 |
+
elif choice in ["s", "segmentation"]:
|
| 766 |
+
current_state = getattr(self, "segmentation_enabled", True)
|
| 767 |
+
self.segmentation_enabled = not current_state
|
| 768 |
+
seg_mode = (
|
| 769 |
+
"📄 Smart Segmentation"
|
| 770 |
+
if self.segmentation_enabled
|
| 771 |
+
else "📋 Traditional Processing"
|
| 772 |
+
)
|
| 773 |
+
self.print_status(
|
| 774 |
+
f"Document processing switched to: {seg_mode}", "success"
|
| 775 |
+
)
|
| 776 |
+
time.sleep(1)
|
| 777 |
+
self.show_configuration_menu()
|
| 778 |
+
return
|
| 779 |
+
|
| 780 |
+
elif choice in ["b", "back"]:
|
| 781 |
+
return
|
| 782 |
+
|
| 783 |
+
else:
|
| 784 |
+
self.print_status(
|
| 785 |
+
"Invalid choice. Please enter 'T', 'S', or 'B'.", "warning"
|
| 786 |
+
)
|
projects/ui/DeepCode/cli/cli_launcher.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
DeepCode - CLI Research Engine Launcher
|
| 4 |
+
DeepCode - CLI研究引擎启动器
|
| 5 |
+
|
| 6 |
+
🧬 Open-Source Code Agent by Data Intelligence Lab @ HKU (CLI Edition)
|
| 7 |
+
⚡ Revolutionizing research reproducibility through collaborative AI via command line
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import sys
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def check_dependencies():
|
| 15 |
+
"""检查必要的依赖是否已安装 / Check if necessary dependencies are installed"""
|
| 16 |
+
import importlib.util
|
| 17 |
+
|
| 18 |
+
print("🔍 Checking CLI dependencies...")
|
| 19 |
+
|
| 20 |
+
missing_deps = []
|
| 21 |
+
|
| 22 |
+
# Check asyncio availability
|
| 23 |
+
if importlib.util.find_spec("asyncio") is not None:
|
| 24 |
+
print("✅ Asyncio is available")
|
| 25 |
+
else:
|
| 26 |
+
missing_deps.append("asyncio")
|
| 27 |
+
|
| 28 |
+
# Check PyYAML availability
|
| 29 |
+
if importlib.util.find_spec("yaml") is not None:
|
| 30 |
+
print("✅ PyYAML is installed")
|
| 31 |
+
else:
|
| 32 |
+
missing_deps.append("pyyaml")
|
| 33 |
+
|
| 34 |
+
# Check Tkinter availability
|
| 35 |
+
if importlib.util.find_spec("tkinter") is not None:
|
| 36 |
+
print("✅ Tkinter is available (for file dialogs)")
|
| 37 |
+
else:
|
| 38 |
+
print("⚠️ Tkinter not available - file dialogs will use manual input")
|
| 39 |
+
|
| 40 |
+
# Check for MCP agent dependencies
|
| 41 |
+
if importlib.util.find_spec("mcp_agent.app") is not None:
|
| 42 |
+
print("✅ MCP Agent framework is available")
|
| 43 |
+
else:
|
| 44 |
+
missing_deps.append("mcp-agent")
|
| 45 |
+
|
| 46 |
+
# Check for workflow dependencies
|
| 47 |
+
# 添加项目根目录到路径
|
| 48 |
+
current_dir = Path(__file__).parent
|
| 49 |
+
project_root = current_dir.parent
|
| 50 |
+
if str(project_root) not in sys.path:
|
| 51 |
+
sys.path.insert(0, str(project_root))
|
| 52 |
+
|
| 53 |
+
if importlib.util.find_spec("workflows.agent_orchestration_engine") is not None:
|
| 54 |
+
print("✅ Workflow modules are available")
|
| 55 |
+
else:
|
| 56 |
+
print("⚠️ Workflow modules may not be properly configured")
|
| 57 |
+
|
| 58 |
+
# Check for CLI components
|
| 59 |
+
if importlib.util.find_spec("cli.cli_app") is not None:
|
| 60 |
+
print("✅ CLI application components are available")
|
| 61 |
+
else:
|
| 62 |
+
print("❌ CLI application components missing")
|
| 63 |
+
missing_deps.append("cli-components")
|
| 64 |
+
|
| 65 |
+
if missing_deps:
|
| 66 |
+
print("\n❌ Missing dependencies:")
|
| 67 |
+
for dep in missing_deps:
|
| 68 |
+
print(f" - {dep}")
|
| 69 |
+
print("\nPlease install missing dependencies using:")
|
| 70 |
+
print(
|
| 71 |
+
f"pip install {' '.join([d for d in missing_deps if d != 'cli-components'])}"
|
| 72 |
+
)
|
| 73 |
+
if "cli-components" in missing_deps:
|
| 74 |
+
print(
|
| 75 |
+
"CLI components appear to be missing - please check the cli/ directory"
|
| 76 |
+
)
|
| 77 |
+
return False
|
| 78 |
+
|
| 79 |
+
print("✅ All CLI dependencies satisfied")
|
| 80 |
+
return True
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def print_banner():
|
| 84 |
+
"""显示CLI启动横幅 / Display CLI startup banner"""
|
| 85 |
+
banner = """
|
| 86 |
+
╔══════════════════════════════════════════════════════════════╗
|
| 87 |
+
║ ║
|
| 88 |
+
║ 🧬 DeepCode - Open-Source Code Agent ║
|
| 89 |
+
║ ║
|
| 90 |
+
║ ⚡ DATA INTELLIGENCE LAB @ HKU ⚡ ║
|
| 91 |
+
║ ║
|
| 92 |
+
║ ║
|
| 93 |
+
║ ║
|
| 94 |
+
╚══════════════════════════════════════════════════════════════╝
|
| 95 |
+
"""
|
| 96 |
+
print(banner)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def main():
|
| 100 |
+
"""主函数 / Main function"""
|
| 101 |
+
print_banner()
|
| 102 |
+
|
| 103 |
+
# 检查依赖 / Check dependencies
|
| 104 |
+
if not check_dependencies():
|
| 105 |
+
print("\n🚨 Please install missing dependencies and try again.")
|
| 106 |
+
sys.exit(1)
|
| 107 |
+
|
| 108 |
+
# 获取当前脚本目录 / Get current script directory
|
| 109 |
+
current_dir = Path(__file__).parent
|
| 110 |
+
project_root = current_dir.parent
|
| 111 |
+
cli_app_path = current_dir / "cli_app.py"
|
| 112 |
+
|
| 113 |
+
# 检查cli_app.py是否存在 / Check if cli_app.py exists
|
| 114 |
+
if not cli_app_path.exists():
|
| 115 |
+
print(f"❌ CLI application file not found: {cli_app_path}")
|
| 116 |
+
print("Please ensure the cli/cli_app.py file exists.")
|
| 117 |
+
sys.exit(1)
|
| 118 |
+
|
| 119 |
+
print(f"\n📁 CLI App location: {cli_app_path}")
|
| 120 |
+
print("🖥️ Starting DeepCode CLI interface...")
|
| 121 |
+
print("🚀 Initializing command line application")
|
| 122 |
+
print("=" * 70)
|
| 123 |
+
print("💡 Tip: Follow the interactive prompts to process your research")
|
| 124 |
+
print("🛑 Press Ctrl+C to exit at any time")
|
| 125 |
+
print("=" * 70)
|
| 126 |
+
|
| 127 |
+
# 启动CLI应用 / Launch CLI application
|
| 128 |
+
try:
|
| 129 |
+
# 导入并运行CLI应用
|
| 130 |
+
if str(project_root) not in sys.path:
|
| 131 |
+
sys.path.insert(0, str(project_root)) # 添加项目根目录到路径
|
| 132 |
+
from cli.cli_app import main as cli_main
|
| 133 |
+
|
| 134 |
+
print("\n🎯 Launching CLI application...")
|
| 135 |
+
|
| 136 |
+
# 使用asyncio运行主函数
|
| 137 |
+
import asyncio
|
| 138 |
+
|
| 139 |
+
asyncio.run(cli_main())
|
| 140 |
+
|
| 141 |
+
except KeyboardInterrupt:
|
| 142 |
+
print("\n\n🛑 DeepCode CLI stopped by user")
|
| 143 |
+
print("Thank you for using DeepCode CLI! 🧬")
|
| 144 |
+
except ImportError as e:
|
| 145 |
+
print(f"\n❌ Failed to import CLI application: {e}")
|
| 146 |
+
print("Please check if all modules are properly installed.")
|
| 147 |
+
sys.exit(1)
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print(f"\n❌ Unexpected error: {e}")
|
| 150 |
+
print("Please check your Python environment and try again.")
|
| 151 |
+
sys.exit(1)
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
if __name__ == "__main__":
|
| 155 |
+
main()
|
projects/ui/DeepCode/cli/main_cli.py
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
DeepCode CLI - Open-Source Code Agent
|
| 4 |
+
深度代码CLI - 开源代码智能体
|
| 5 |
+
|
| 6 |
+
🧬 Data Intelligence Lab @ HKU
|
| 7 |
+
⚡ Revolutionizing Research Reproducibility through Multi-Agent Architecture
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
import sys
|
| 12 |
+
import asyncio
|
| 13 |
+
import argparse
|
| 14 |
+
|
| 15 |
+
# 禁止生成.pyc文件
|
| 16 |
+
os.environ["PYTHONDONTWRITEBYTECODE"] = "1"
|
| 17 |
+
|
| 18 |
+
# 添加项目根目录到路径
|
| 19 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 20 |
+
parent_dir = os.path.dirname(current_dir)
|
| 21 |
+
if parent_dir not in sys.path:
|
| 22 |
+
sys.path.insert(0, parent_dir)
|
| 23 |
+
|
| 24 |
+
# 导入CLI应用
|
| 25 |
+
from cli.cli_app import CLIApp, Colors
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def print_enhanced_banner():
|
| 29 |
+
"""显示增强版启动横幅"""
|
| 30 |
+
banner = f"""
|
| 31 |
+
{Colors.CYAN}╔══════════════════════════════════════════════════════════════════════════════╗
|
| 32 |
+
║ ║
|
| 33 |
+
║ {Colors.BOLD}{Colors.MAGENTA}🧬 DeepCode - Open-Source Code Agent{Colors.CYAN} ║
|
| 34 |
+
║ ║
|
| 35 |
+
║ {Colors.BOLD}{Colors.YELLOW}⚡ DATA INTELLIGENCE LAB @ HKU ⚡{Colors.CYAN} ║
|
| 36 |
+
║ ║
|
| 37 |
+
║ Revolutionizing research reproducibility through collaborative AI ║
|
| 38 |
+
║ Building the future where code is reproduced from natural language ║
|
| 39 |
+
║ ║
|
| 40 |
+
║ {Colors.BOLD}{Colors.GREEN}🤖 Key Features:{Colors.CYAN} ║
|
| 41 |
+
║ • Automated paper-to-code reproduction ║
|
| 42 |
+
║ • Multi-agent collaborative architecture ║
|
| 43 |
+
║ • Open-source and extensible design ║
|
| 44 |
+
║ • Join our growing research community ║
|
| 45 |
+
║ ║
|
| 46 |
+
╚══════════════════════════════════════════════════════════════════════════════╝{Colors.ENDC}
|
| 47 |
+
"""
|
| 48 |
+
print(banner)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def check_environment():
|
| 52 |
+
"""检查运行环境"""
|
| 53 |
+
print(f"{Colors.CYAN}🔍 Checking environment...{Colors.ENDC}")
|
| 54 |
+
|
| 55 |
+
# 检查Python版本
|
| 56 |
+
if sys.version_info < (3, 8):
|
| 57 |
+
print(
|
| 58 |
+
f"{Colors.FAIL}❌ Python 3.8+ required. Current: {sys.version}{Colors.ENDC}"
|
| 59 |
+
)
|
| 60 |
+
return False
|
| 61 |
+
|
| 62 |
+
print(f"{Colors.OKGREEN}✅ Python {sys.version.split()[0]} - OK{Colors.ENDC}")
|
| 63 |
+
|
| 64 |
+
# 检查必要模块
|
| 65 |
+
required_modules = [
|
| 66 |
+
("asyncio", "Async IO support"),
|
| 67 |
+
("pathlib", "Path handling"),
|
| 68 |
+
("typing", "Type hints"),
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
missing_modules = []
|
| 72 |
+
for module, desc in required_modules:
|
| 73 |
+
try:
|
| 74 |
+
__import__(module)
|
| 75 |
+
print(f"{Colors.OKGREEN}✅ {desc} - OK{Colors.ENDC}")
|
| 76 |
+
except ImportError:
|
| 77 |
+
missing_modules.append(module)
|
| 78 |
+
print(f"{Colors.FAIL}❌ {desc} - Missing{Colors.ENDC}")
|
| 79 |
+
|
| 80 |
+
if missing_modules:
|
| 81 |
+
print(
|
| 82 |
+
f"{Colors.FAIL}❌ Missing required modules: {', '.join(missing_modules)}{Colors.ENDC}"
|
| 83 |
+
)
|
| 84 |
+
return False
|
| 85 |
+
|
| 86 |
+
print(f"{Colors.OKGREEN}✅ Environment check passed{Colors.ENDC}")
|
| 87 |
+
return True
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def parse_arguments():
|
| 91 |
+
"""解析命令行参数"""
|
| 92 |
+
parser = argparse.ArgumentParser(
|
| 93 |
+
description="DeepCode CLI - Open-Source Code Agent by Data Intelligence Lab @ HKU",
|
| 94 |
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
| 95 |
+
epilog=f"""
|
| 96 |
+
{Colors.BOLD}Examples:{Colors.ENDC}
|
| 97 |
+
{Colors.CYAN}python main_cli.py{Colors.ENDC} # Interactive mode
|
| 98 |
+
{Colors.CYAN}python main_cli.py --file paper.pdf{Colors.ENDC} # Process file directly
|
| 99 |
+
{Colors.CYAN}python main_cli.py --url https://...{Colors.ENDC} # Process URL directly
|
| 100 |
+
{Colors.CYAN}python main_cli.py --chat "Build a web app..."{Colors.ENDC} # Process chat requirements
|
| 101 |
+
{Colors.CYAN}python main_cli.py --optimized{Colors.ENDC} # Use optimized mode
|
| 102 |
+
{Colors.CYAN}python main_cli.py --disable-segmentation{Colors.ENDC} # Disable document segmentation
|
| 103 |
+
{Colors.CYAN}python main_cli.py --segmentation-threshold 30000{Colors.ENDC} # Custom segmentation threshold
|
| 104 |
+
|
| 105 |
+
{Colors.BOLD}Pipeline Modes:{Colors.ENDC}
|
| 106 |
+
{Colors.GREEN}Comprehensive{Colors.ENDC}: Full intelligence analysis with indexing
|
| 107 |
+
{Colors.YELLOW}Optimized{Colors.ENDC}: Fast processing without indexing
|
| 108 |
+
|
| 109 |
+
{Colors.BOLD}Document Processing:{Colors.ENDC}
|
| 110 |
+
{Colors.BLUE}Smart Segmentation{Colors.ENDC}: Intelligent document segmentation for large papers
|
| 111 |
+
{Colors.MAGENTA}Supported Formats{Colors.ENDC}: PDF, DOCX, DOC, PPT, PPTX, XLS, XLSX, HTML, TXT, MD
|
| 112 |
+
""",
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
parser.add_argument(
|
| 116 |
+
"--file", "-f", type=str, help="Process a specific file (PDF, DOCX, TXT, etc.)"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
parser.add_argument(
|
| 120 |
+
"--url", "-u", type=str, help="Process a research paper from URL"
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
parser.add_argument(
|
| 124 |
+
"--chat",
|
| 125 |
+
"-t",
|
| 126 |
+
type=str,
|
| 127 |
+
help="Process coding requirements via chat input (provide requirements as argument)",
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
parser.add_argument(
|
| 131 |
+
"--optimized",
|
| 132 |
+
"-o",
|
| 133 |
+
action="store_true",
|
| 134 |
+
help="Use optimized mode (skip indexing for faster processing)",
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
parser.add_argument(
|
| 138 |
+
"--disable-segmentation",
|
| 139 |
+
action="store_true",
|
| 140 |
+
help="Disable intelligent document segmentation (use traditional full-document processing)",
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
parser.add_argument(
|
| 144 |
+
"--segmentation-threshold",
|
| 145 |
+
type=int,
|
| 146 |
+
default=50000,
|
| 147 |
+
help="Document size threshold (characters) to trigger segmentation (default: 50000)",
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
parser.add_argument(
|
| 151 |
+
"--verbose", "-v", action="store_true", help="Enable verbose output"
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
return parser.parse_args()
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
async def run_direct_processing(app: CLIApp, input_source: str, input_type: str):
|
| 158 |
+
"""直接处理模式(非交互式)"""
|
| 159 |
+
try:
|
| 160 |
+
print(
|
| 161 |
+
f"\n{Colors.BOLD}{Colors.CYAN}🚀 Starting direct processing mode...{Colors.ENDC}"
|
| 162 |
+
)
|
| 163 |
+
print(f"{Colors.CYAN}Input: {input_source}{Colors.ENDC}")
|
| 164 |
+
print(f"{Colors.CYAN}Type: {input_type}{Colors.ENDC}")
|
| 165 |
+
print(
|
| 166 |
+
f"{Colors.CYAN}Mode: {'🧠 Comprehensive' if app.cli.enable_indexing else '⚡ Optimized'}{Colors.ENDC}"
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
# 初始化应用
|
| 170 |
+
init_result = await app.initialize_mcp_app()
|
| 171 |
+
if init_result["status"] != "success":
|
| 172 |
+
print(
|
| 173 |
+
f"{Colors.FAIL}❌ Initialization failed: {init_result['message']}{Colors.ENDC}"
|
| 174 |
+
)
|
| 175 |
+
return False
|
| 176 |
+
|
| 177 |
+
# 处理输入
|
| 178 |
+
result = await app.process_input(input_source, input_type)
|
| 179 |
+
|
| 180 |
+
if result["status"] == "success":
|
| 181 |
+
print(
|
| 182 |
+
f"\n{Colors.BOLD}{Colors.OKGREEN}🎉 Processing completed successfully!{Colors.ENDC}"
|
| 183 |
+
)
|
| 184 |
+
return True
|
| 185 |
+
else:
|
| 186 |
+
print(
|
| 187 |
+
f"\n{Colors.BOLD}{Colors.FAIL}❌ Processing failed: {result.get('error', 'Unknown error')}{Colors.ENDC}"
|
| 188 |
+
)
|
| 189 |
+
return False
|
| 190 |
+
|
| 191 |
+
except Exception as e:
|
| 192 |
+
print(f"\n{Colors.FAIL}❌ Direct processing error: {str(e)}{Colors.ENDC}")
|
| 193 |
+
return False
|
| 194 |
+
finally:
|
| 195 |
+
await app.cleanup_mcp_app()
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
async def main():
|
| 199 |
+
"""主函数"""
|
| 200 |
+
# 解析命令行参数
|
| 201 |
+
args = parse_arguments()
|
| 202 |
+
|
| 203 |
+
# 显示横幅
|
| 204 |
+
print_enhanced_banner()
|
| 205 |
+
|
| 206 |
+
# 检查环境
|
| 207 |
+
if not check_environment():
|
| 208 |
+
print(
|
| 209 |
+
f"\n{Colors.FAIL}🚨 Environment check failed. Please fix the issues and try again.{Colors.ENDC}"
|
| 210 |
+
)
|
| 211 |
+
sys.exit(1)
|
| 212 |
+
|
| 213 |
+
try:
|
| 214 |
+
# 创建CLI应用
|
| 215 |
+
app = CLIApp()
|
| 216 |
+
|
| 217 |
+
# 设置配置
|
| 218 |
+
if args.optimized:
|
| 219 |
+
app.cli.enable_indexing = False
|
| 220 |
+
print(
|
| 221 |
+
f"\n{Colors.YELLOW}⚡ Optimized mode enabled - indexing disabled{Colors.ENDC}"
|
| 222 |
+
)
|
| 223 |
+
else:
|
| 224 |
+
print(
|
| 225 |
+
f"\n{Colors.GREEN}🧠 Comprehensive mode enabled - full intelligence analysis{Colors.ENDC}"
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
# Configure document segmentation settings
|
| 229 |
+
if hasattr(args, "disable_segmentation") and args.disable_segmentation:
|
| 230 |
+
print(
|
| 231 |
+
f"\n{Colors.MAGENTA}📄 Document segmentation disabled - using traditional processing{Colors.ENDC}"
|
| 232 |
+
)
|
| 233 |
+
app.segmentation_config = {
|
| 234 |
+
"enabled": False,
|
| 235 |
+
"size_threshold_chars": args.segmentation_threshold,
|
| 236 |
+
}
|
| 237 |
+
else:
|
| 238 |
+
print(
|
| 239 |
+
f"\n{Colors.BLUE}📄 Smart document segmentation enabled (threshold: {args.segmentation_threshold} chars){Colors.ENDC}"
|
| 240 |
+
)
|
| 241 |
+
app.segmentation_config = {
|
| 242 |
+
"enabled": True,
|
| 243 |
+
"size_threshold_chars": args.segmentation_threshold,
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
# 检查是否为直接处理模式
|
| 247 |
+
if args.file or args.url or args.chat:
|
| 248 |
+
if args.file:
|
| 249 |
+
# 验证文件存在
|
| 250 |
+
if not os.path.exists(args.file):
|
| 251 |
+
print(f"{Colors.FAIL}❌ File not found: {args.file}{Colors.ENDC}")
|
| 252 |
+
sys.exit(1)
|
| 253 |
+
success = await run_direct_processing(app, args.file, "file")
|
| 254 |
+
elif args.url:
|
| 255 |
+
success = await run_direct_processing(app, args.url, "url")
|
| 256 |
+
elif args.chat:
|
| 257 |
+
# 验证chat输入长度
|
| 258 |
+
if len(args.chat.strip()) < 20:
|
| 259 |
+
print(
|
| 260 |
+
f"{Colors.FAIL}❌ Chat input too short. Please provide more detailed requirements (at least 20 characters){Colors.ENDC}"
|
| 261 |
+
)
|
| 262 |
+
sys.exit(1)
|
| 263 |
+
success = await run_direct_processing(app, args.chat, "chat")
|
| 264 |
+
|
| 265 |
+
sys.exit(0 if success else 1)
|
| 266 |
+
else:
|
| 267 |
+
# 交互式模式
|
| 268 |
+
print(f"\n{Colors.CYAN}🎮 Starting interactive mode...{Colors.ENDC}")
|
| 269 |
+
await app.run_interactive_session()
|
| 270 |
+
|
| 271 |
+
except KeyboardInterrupt:
|
| 272 |
+
print(f"\n{Colors.WARNING}⚠️ Application interrupted by user{Colors.ENDC}")
|
| 273 |
+
sys.exit(1)
|
| 274 |
+
except Exception as e:
|
| 275 |
+
print(f"\n{Colors.FAIL}❌ Application errors: {str(e)}{Colors.ENDC}")
|
| 276 |
+
sys.exit(1)
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
if __name__ == "__main__":
|
| 280 |
+
asyncio.run(main())
|
projects/ui/DeepCode/config/mcp_tool_definitions.py
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
MCP工具定义配置模块
|
| 3 |
+
MCP Tool Definitions Configuration Module
|
| 4 |
+
|
| 5 |
+
将工具定义从主程序逻辑中分离,提供标准化的工具定义格式
|
| 6 |
+
Separate tool definitions from main program logic, providing standardized tool definition format
|
| 7 |
+
|
| 8 |
+
支持的工具类型:
|
| 9 |
+
- 文件操作工具 (File Operations)
|
| 10 |
+
- 代码执行工具 (Code Execution)
|
| 11 |
+
- 搜索工具 (Search Tools)
|
| 12 |
+
- 项目结构工具 (Project Structure Tools)
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
from typing import Dict, List, Any
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class MCPToolDefinitions:
|
| 19 |
+
"""MCP工具定义管理器"""
|
| 20 |
+
|
| 21 |
+
@staticmethod
|
| 22 |
+
def get_code_implementation_tools() -> List[Dict[str, Any]]:
|
| 23 |
+
"""
|
| 24 |
+
获取代码实现相关的工具定义
|
| 25 |
+
Get tool definitions for code implementation
|
| 26 |
+
"""
|
| 27 |
+
return [
|
| 28 |
+
MCPToolDefinitions._get_read_file_tool(),
|
| 29 |
+
MCPToolDefinitions._get_read_multiple_files_tool(),
|
| 30 |
+
MCPToolDefinitions._get_read_code_mem_tool(),
|
| 31 |
+
MCPToolDefinitions._get_write_file_tool(),
|
| 32 |
+
MCPToolDefinitions._get_write_multiple_files_tool(),
|
| 33 |
+
MCPToolDefinitions._get_execute_python_tool(),
|
| 34 |
+
MCPToolDefinitions._get_execute_bash_tool(),
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
@staticmethod
|
| 38 |
+
def _get_read_file_tool() -> Dict[str, Any]:
|
| 39 |
+
"""读取文件工具定义"""
|
| 40 |
+
return {
|
| 41 |
+
"name": "read_file",
|
| 42 |
+
"description": "Read file content, supports specifying line number range",
|
| 43 |
+
"input_schema": {
|
| 44 |
+
"type": "object",
|
| 45 |
+
"properties": {
|
| 46 |
+
"file_path": {
|
| 47 |
+
"type": "string",
|
| 48 |
+
"description": "File path, relative to workspace",
|
| 49 |
+
},
|
| 50 |
+
"start_line": {
|
| 51 |
+
"type": "integer",
|
| 52 |
+
"description": "Start line number (starting from 1, optional)",
|
| 53 |
+
},
|
| 54 |
+
"end_line": {
|
| 55 |
+
"type": "integer",
|
| 56 |
+
"description": "End line number (starting from 1, optional)",
|
| 57 |
+
},
|
| 58 |
+
},
|
| 59 |
+
"required": ["file_path"],
|
| 60 |
+
},
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
@staticmethod
|
| 64 |
+
def _get_read_multiple_files_tool() -> Dict[str, Any]:
|
| 65 |
+
"""批量读取多个文件工具定义"""
|
| 66 |
+
return {
|
| 67 |
+
"name": "read_multiple_files",
|
| 68 |
+
"description": "Read multiple files in a single operation (for batch reading)",
|
| 69 |
+
"input_schema": {
|
| 70 |
+
"type": "object",
|
| 71 |
+
"properties": {
|
| 72 |
+
"file_requests": {
|
| 73 |
+
"type": "string",
|
| 74 |
+
"description": 'JSON string with file requests, e.g., \'{"file1.py": {}, "file2.py": {"start_line": 1, "end_line": 10}}\' or simple array \'["file1.py", "file2.py"]\'',
|
| 75 |
+
},
|
| 76 |
+
"max_files": {
|
| 77 |
+
"type": "integer",
|
| 78 |
+
"description": "Maximum number of files to read in one operation",
|
| 79 |
+
"default": 5,
|
| 80 |
+
"minimum": 1,
|
| 81 |
+
"maximum": 10,
|
| 82 |
+
},
|
| 83 |
+
},
|
| 84 |
+
"required": ["file_requests"],
|
| 85 |
+
},
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
@staticmethod
|
| 89 |
+
def _get_read_code_mem_tool() -> Dict[str, Any]:
|
| 90 |
+
"""Read code memory tool definition - reads from implement_code_summary.md"""
|
| 91 |
+
return {
|
| 92 |
+
"name": "read_code_mem",
|
| 93 |
+
"description": "Check if file summaries exist in implement_code_summary.md for multiple files in a single call. Returns summaries for all requested files if available.",
|
| 94 |
+
"input_schema": {
|
| 95 |
+
"type": "object",
|
| 96 |
+
"properties": {
|
| 97 |
+
"file_paths": {
|
| 98 |
+
"type": "array",
|
| 99 |
+
"items": {"type": "string"},
|
| 100 |
+
"description": "List of file paths to check for summary information in implement_code_summary.md",
|
| 101 |
+
}
|
| 102 |
+
},
|
| 103 |
+
"required": ["file_paths"],
|
| 104 |
+
},
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
@staticmethod
|
| 108 |
+
def _get_write_file_tool() -> Dict[str, Any]:
|
| 109 |
+
"""写入文件工具定义"""
|
| 110 |
+
return {
|
| 111 |
+
"name": "write_file",
|
| 112 |
+
"description": "Write content to file",
|
| 113 |
+
"input_schema": {
|
| 114 |
+
"type": "object",
|
| 115 |
+
"properties": {
|
| 116 |
+
"file_path": {
|
| 117 |
+
"type": "string",
|
| 118 |
+
"description": "File path, relative to workspace",
|
| 119 |
+
},
|
| 120 |
+
"content": {
|
| 121 |
+
"type": "string",
|
| 122 |
+
"description": "Content to write to file",
|
| 123 |
+
},
|
| 124 |
+
"create_dirs": {
|
| 125 |
+
"type": "boolean",
|
| 126 |
+
"description": "Whether to create directories if they don't exist",
|
| 127 |
+
"default": True,
|
| 128 |
+
},
|
| 129 |
+
"create_backup": {
|
| 130 |
+
"type": "boolean",
|
| 131 |
+
"description": "Whether to create backup file if file already exists",
|
| 132 |
+
"default": False,
|
| 133 |
+
},
|
| 134 |
+
},
|
| 135 |
+
"required": ["file_path", "content"],
|
| 136 |
+
},
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
@staticmethod
|
| 140 |
+
def _get_write_multiple_files_tool() -> Dict[str, Any]:
|
| 141 |
+
"""批量写入多个文件工具定义"""
|
| 142 |
+
return {
|
| 143 |
+
"name": "write_multiple_files",
|
| 144 |
+
"description": "Write multiple files in a single operation (for batch implementation)",
|
| 145 |
+
"input_schema": {
|
| 146 |
+
"type": "object",
|
| 147 |
+
"properties": {
|
| 148 |
+
"file_implementations": {
|
| 149 |
+
"type": "string",
|
| 150 |
+
"description": 'JSON string mapping file paths to content, e.g., \'{"file1.py": "content1", "file2.py": "content2"}\'',
|
| 151 |
+
},
|
| 152 |
+
"create_dirs": {
|
| 153 |
+
"type": "boolean",
|
| 154 |
+
"description": "Whether to create directories if they don't exist",
|
| 155 |
+
"default": True,
|
| 156 |
+
},
|
| 157 |
+
"create_backup": {
|
| 158 |
+
"type": "boolean",
|
| 159 |
+
"description": "Whether to create backup files if they already exist",
|
| 160 |
+
"default": False,
|
| 161 |
+
},
|
| 162 |
+
"max_files": {
|
| 163 |
+
"type": "integer",
|
| 164 |
+
"description": "Maximum number of files to write in one operation",
|
| 165 |
+
"default": 5,
|
| 166 |
+
"minimum": 1,
|
| 167 |
+
"maximum": 10,
|
| 168 |
+
},
|
| 169 |
+
},
|
| 170 |
+
"required": ["file_implementations"],
|
| 171 |
+
},
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
@staticmethod
|
| 175 |
+
def _get_execute_python_tool() -> Dict[str, Any]:
|
| 176 |
+
"""Python执行工具定义"""
|
| 177 |
+
return {
|
| 178 |
+
"name": "execute_python",
|
| 179 |
+
"description": "Execute Python code and return output",
|
| 180 |
+
"input_schema": {
|
| 181 |
+
"type": "object",
|
| 182 |
+
"properties": {
|
| 183 |
+
"code": {"type": "string", "description": "Python code to execute"},
|
| 184 |
+
"timeout": {
|
| 185 |
+
"type": "integer",
|
| 186 |
+
"description": "Timeout in seconds",
|
| 187 |
+
"default": 30,
|
| 188 |
+
},
|
| 189 |
+
},
|
| 190 |
+
"required": ["code"],
|
| 191 |
+
},
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
@staticmethod
|
| 195 |
+
def _get_execute_bash_tool() -> Dict[str, Any]:
|
| 196 |
+
"""Bash执行工具定义"""
|
| 197 |
+
return {
|
| 198 |
+
"name": "execute_bash",
|
| 199 |
+
"description": "Execute bash command",
|
| 200 |
+
"input_schema": {
|
| 201 |
+
"type": "object",
|
| 202 |
+
"properties": {
|
| 203 |
+
"command": {
|
| 204 |
+
"type": "string",
|
| 205 |
+
"description": "Bash command to execute",
|
| 206 |
+
},
|
| 207 |
+
"timeout": {
|
| 208 |
+
"type": "integer",
|
| 209 |
+
"description": "Timeout in seconds",
|
| 210 |
+
"default": 30,
|
| 211 |
+
},
|
| 212 |
+
},
|
| 213 |
+
"required": ["command"],
|
| 214 |
+
},
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
@staticmethod
|
| 218 |
+
def _get_file_structure_tool() -> Dict[str, Any]:
|
| 219 |
+
"""文件结构获取工具定义"""
|
| 220 |
+
return {
|
| 221 |
+
"name": "get_file_structure",
|
| 222 |
+
"description": "Get directory file structure",
|
| 223 |
+
"input_schema": {
|
| 224 |
+
"type": "object",
|
| 225 |
+
"properties": {
|
| 226 |
+
"directory": {
|
| 227 |
+
"type": "string",
|
| 228 |
+
"description": "Directory path, relative to workspace",
|
| 229 |
+
"default": ".",
|
| 230 |
+
},
|
| 231 |
+
"max_depth": {
|
| 232 |
+
"type": "integer",
|
| 233 |
+
"description": "Maximum traversal depth",
|
| 234 |
+
"default": 5,
|
| 235 |
+
},
|
| 236 |
+
},
|
| 237 |
+
},
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
@staticmethod
|
| 241 |
+
def _get_search_code_references_tool() -> Dict[str, Any]:
|
| 242 |
+
"""统一代码参考搜索工具定义 - 合并了三个步骤为一个工具"""
|
| 243 |
+
return {
|
| 244 |
+
"name": "search_code_references",
|
| 245 |
+
"description": "UNIFIED TOOL: Search relevant reference code from index files. Combines directory setup, index loading, and searching in a single call.",
|
| 246 |
+
"input_schema": {
|
| 247 |
+
"type": "object",
|
| 248 |
+
"properties": {
|
| 249 |
+
"indexes_path": {
|
| 250 |
+
"type": "string",
|
| 251 |
+
"description": "Path to the indexes directory containing JSON index files",
|
| 252 |
+
},
|
| 253 |
+
"target_file": {
|
| 254 |
+
"type": "string",
|
| 255 |
+
"description": "Target file path to be implemented",
|
| 256 |
+
},
|
| 257 |
+
"keywords": {
|
| 258 |
+
"type": "string",
|
| 259 |
+
"description": "Search keywords, comma-separated",
|
| 260 |
+
"default": "",
|
| 261 |
+
},
|
| 262 |
+
"max_results": {
|
| 263 |
+
"type": "integer",
|
| 264 |
+
"description": "Maximum number of results to return",
|
| 265 |
+
"default": 10,
|
| 266 |
+
},
|
| 267 |
+
},
|
| 268 |
+
"required": ["indexes_path", "target_file"],
|
| 269 |
+
},
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
@staticmethod
|
| 273 |
+
def _get_get_indexes_overview_tool() -> Dict[str, Any]:
|
| 274 |
+
"""获取索引概览工具定义"""
|
| 275 |
+
return {
|
| 276 |
+
"name": "get_indexes_overview",
|
| 277 |
+
"description": "Get overview of all available reference code index information from specified directory",
|
| 278 |
+
"input_schema": {
|
| 279 |
+
"type": "object",
|
| 280 |
+
"properties": {
|
| 281 |
+
"indexes_path": {
|
| 282 |
+
"type": "string",
|
| 283 |
+
"description": "Path to the indexes directory containing JSON index files",
|
| 284 |
+
}
|
| 285 |
+
},
|
| 286 |
+
"required": ["indexes_path"],
|
| 287 |
+
},
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
@staticmethod
|
| 291 |
+
def _get_set_workspace_tool() -> Dict[str, Any]:
|
| 292 |
+
"""Set workspace directory tool definition"""
|
| 293 |
+
return {
|
| 294 |
+
"name": "set_workspace",
|
| 295 |
+
"description": "Set the workspace directory for file operations",
|
| 296 |
+
"input_schema": {
|
| 297 |
+
"type": "object",
|
| 298 |
+
"properties": {
|
| 299 |
+
"workspace_path": {
|
| 300 |
+
"type": "string",
|
| 301 |
+
"description": "Directory path for the workspace",
|
| 302 |
+
}
|
| 303 |
+
},
|
| 304 |
+
"required": ["workspace_path"],
|
| 305 |
+
},
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
# @staticmethod
|
| 309 |
+
# def _get_set_indexes_directory_tool() -> Dict[str, Any]:
|
| 310 |
+
# """Set indexes directory tool definition - DEPRECATED: Use unified search_code_references instead"""
|
| 311 |
+
# return {
|
| 312 |
+
# "name": "set_indexes_directory",
|
| 313 |
+
# "description": "Set the directory path for code reference indexes",
|
| 314 |
+
# "input_schema": {
|
| 315 |
+
# "type": "object",
|
| 316 |
+
# "properties": {
|
| 317 |
+
# "indexes_path": {
|
| 318 |
+
# "type": "string",
|
| 319 |
+
# "description": "Directory path containing index JSON files"
|
| 320 |
+
# }
|
| 321 |
+
# },
|
| 322 |
+
# "required": ["indexes_path"]
|
| 323 |
+
# }
|
| 324 |
+
# }
|
| 325 |
+
|
| 326 |
+
@staticmethod
|
| 327 |
+
def get_available_tool_sets() -> Dict[str, str]:
|
| 328 |
+
"""
|
| 329 |
+
获取可用的工具集合
|
| 330 |
+
Get available tool sets
|
| 331 |
+
"""
|
| 332 |
+
return {
|
| 333 |
+
"code_implementation": "代码实现相关工具集 / Code implementation tool set",
|
| 334 |
+
# 可以在这里添加更多工具集
|
| 335 |
+
# "data_analysis": "数据分析工具集 / Data analysis tool set",
|
| 336 |
+
# "web_scraping": "网页爬取工具集 / Web scraping tool set",
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
@staticmethod
|
| 340 |
+
def get_tool_set(tool_set_name: str) -> List[Dict[str, Any]]:
|
| 341 |
+
"""
|
| 342 |
+
根据名称获取特定的工具集
|
| 343 |
+
Get specific tool set by name
|
| 344 |
+
"""
|
| 345 |
+
tool_sets = {
|
| 346 |
+
"code_implementation": MCPToolDefinitions.get_code_implementation_tools(),
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
return tool_sets.get(tool_set_name, [])
|
| 350 |
+
|
| 351 |
+
@staticmethod
|
| 352 |
+
def get_all_tools() -> List[Dict[str, Any]]:
|
| 353 |
+
"""
|
| 354 |
+
获取所有可用工具
|
| 355 |
+
Get all available tools
|
| 356 |
+
"""
|
| 357 |
+
all_tools = []
|
| 358 |
+
for tool_set_name in MCPToolDefinitions.get_available_tool_sets().keys():
|
| 359 |
+
all_tools.extend(MCPToolDefinitions.get_tool_set(tool_set_name))
|
| 360 |
+
return all_tools
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
# 便捷访问函数
|
| 364 |
+
def get_mcp_tools(tool_set: str = "code_implementation") -> List[Dict[str, Any]]:
|
| 365 |
+
"""
|
| 366 |
+
便捷函数:获取MCP工具定义
|
| 367 |
+
Convenience function: Get MCP tool definitions
|
| 368 |
+
|
| 369 |
+
Args:
|
| 370 |
+
tool_set: 工具集名称 (默认: "code_implementation")
|
| 371 |
+
|
| 372 |
+
Returns:
|
| 373 |
+
工具定义列表
|
| 374 |
+
"""
|
| 375 |
+
return MCPToolDefinitions.get_tool_set(tool_set)
|
projects/ui/DeepCode/config/mcp_tool_definitions_index.py
ADDED
|
@@ -0,0 +1,620 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
MCP工具定义配置模块
|
| 3 |
+
MCP Tool Definitions Configuration Module
|
| 4 |
+
|
| 5 |
+
将工具定义从主程序逻辑中分离,提供标准化的工具定义格式
|
| 6 |
+
Separate tool definitions from main program logic, providing standardized tool definition format
|
| 7 |
+
|
| 8 |
+
支持的工具类型:
|
| 9 |
+
- 文件操作工具 (File Operations)
|
| 10 |
+
- 代码执行工具 (Code Execution)
|
| 11 |
+
- 搜索工具 (Search Tools)
|
| 12 |
+
- 项目结构工具 (Project Structure Tools)
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
from typing import Dict, List, Any
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class MCPToolDefinitions:
|
| 19 |
+
"""MCP工具定义管理器"""
|
| 20 |
+
|
| 21 |
+
@staticmethod
|
| 22 |
+
def get_code_implementation_tools() -> List[Dict[str, Any]]:
|
| 23 |
+
"""
|
| 24 |
+
获取代码实现相关的工具定义
|
| 25 |
+
Get tool definitions for code implementation
|
| 26 |
+
"""
|
| 27 |
+
return [
|
| 28 |
+
MCPToolDefinitions._get_read_file_tool(),
|
| 29 |
+
MCPToolDefinitions._get_read_multiple_files_tool(),
|
| 30 |
+
MCPToolDefinitions._get_read_code_mem_tool(),
|
| 31 |
+
MCPToolDefinitions._get_write_file_tool(),
|
| 32 |
+
MCPToolDefinitions._get_write_multiple_files_tool(),
|
| 33 |
+
MCPToolDefinitions._get_execute_python_tool(),
|
| 34 |
+
MCPToolDefinitions._get_execute_bash_tool(),
|
| 35 |
+
MCPToolDefinitions._get_search_code_references_tool(),
|
| 36 |
+
MCPToolDefinitions._get_search_code_tool(),
|
| 37 |
+
MCPToolDefinitions._get_file_structure_tool(),
|
| 38 |
+
MCPToolDefinitions._get_set_workspace_tool(),
|
| 39 |
+
MCPToolDefinitions._get_operation_history_tool(),
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
@staticmethod
|
| 43 |
+
def get_code_evaluation_tools() -> List[Dict[str, Any]]:
|
| 44 |
+
"""
|
| 45 |
+
获取代码评估相关的工具定义
|
| 46 |
+
Get tool definitions for code evaluation
|
| 47 |
+
"""
|
| 48 |
+
return [
|
| 49 |
+
MCPToolDefinitions._get_analyze_repo_structure_tool(),
|
| 50 |
+
MCPToolDefinitions._get_detect_dependencies_tool(),
|
| 51 |
+
MCPToolDefinitions._get_assess_code_quality_tool(),
|
| 52 |
+
MCPToolDefinitions._get_evaluate_documentation_tool(),
|
| 53 |
+
MCPToolDefinitions._get_check_reproduction_readiness_tool(),
|
| 54 |
+
MCPToolDefinitions._get_generate_evaluation_summary_tool(),
|
| 55 |
+
MCPToolDefinitions._get_detect_empty_files_tool(),
|
| 56 |
+
MCPToolDefinitions._get_detect_missing_files_tool(),
|
| 57 |
+
MCPToolDefinitions._get_generate_code_revision_report_tool(),
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
@staticmethod
|
| 61 |
+
def _get_read_file_tool() -> Dict[str, Any]:
|
| 62 |
+
"""读取文件工具定义"""
|
| 63 |
+
return {
|
| 64 |
+
"name": "read_file",
|
| 65 |
+
"description": "Read file content, supports specifying line number range",
|
| 66 |
+
"input_schema": {
|
| 67 |
+
"type": "object",
|
| 68 |
+
"properties": {
|
| 69 |
+
"file_path": {
|
| 70 |
+
"type": "string",
|
| 71 |
+
"description": "File path, relative to workspace",
|
| 72 |
+
},
|
| 73 |
+
"start_line": {
|
| 74 |
+
"type": "integer",
|
| 75 |
+
"description": "Start line number (starting from 1, optional)",
|
| 76 |
+
},
|
| 77 |
+
"end_line": {
|
| 78 |
+
"type": "integer",
|
| 79 |
+
"description": "End line number (starting from 1, optional)",
|
| 80 |
+
},
|
| 81 |
+
},
|
| 82 |
+
"required": ["file_path"],
|
| 83 |
+
},
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
@staticmethod
|
| 87 |
+
def _get_read_multiple_files_tool() -> Dict[str, Any]:
|
| 88 |
+
"""批量读取多个文件工具定义"""
|
| 89 |
+
return {
|
| 90 |
+
"name": "read_multiple_files",
|
| 91 |
+
"description": "Read multiple files in a single operation (for batch reading)",
|
| 92 |
+
"input_schema": {
|
| 93 |
+
"type": "object",
|
| 94 |
+
"properties": {
|
| 95 |
+
"file_requests": {
|
| 96 |
+
"type": "string",
|
| 97 |
+
"description": 'JSON string with file requests, e.g., \'{"file1.py": {}, "file2.py": {"start_line": 1, "end_line": 10}}\' or simple array \'["file1.py", "file2.py"]\'',
|
| 98 |
+
},
|
| 99 |
+
"max_files": {
|
| 100 |
+
"type": "integer",
|
| 101 |
+
"description": "Maximum number of files to read in one operation",
|
| 102 |
+
"default": 5,
|
| 103 |
+
"minimum": 1,
|
| 104 |
+
"maximum": 10,
|
| 105 |
+
},
|
| 106 |
+
},
|
| 107 |
+
"required": ["file_requests"],
|
| 108 |
+
},
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
@staticmethod
|
| 112 |
+
def _get_read_code_mem_tool() -> Dict[str, Any]:
|
| 113 |
+
"""Read code memory tool definition - reads from implement_code_summary.md"""
|
| 114 |
+
return {
|
| 115 |
+
"name": "read_code_mem",
|
| 116 |
+
"description": "Check if file summaries exist in implement_code_summary.md for multiple files in a single call. Returns summaries for all requested files if available.",
|
| 117 |
+
"input_schema": {
|
| 118 |
+
"type": "object",
|
| 119 |
+
"properties": {
|
| 120 |
+
"file_paths": {
|
| 121 |
+
"type": "array",
|
| 122 |
+
"items": {"type": "string"},
|
| 123 |
+
"description": "List of file paths to check for summary information in implement_code_summary.md",
|
| 124 |
+
}
|
| 125 |
+
},
|
| 126 |
+
"required": ["file_paths"],
|
| 127 |
+
},
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
@staticmethod
|
| 131 |
+
def _get_write_file_tool() -> Dict[str, Any]:
|
| 132 |
+
"""写入文件工具定义"""
|
| 133 |
+
return {
|
| 134 |
+
"name": "write_file",
|
| 135 |
+
"description": "Write content to file",
|
| 136 |
+
"input_schema": {
|
| 137 |
+
"type": "object",
|
| 138 |
+
"properties": {
|
| 139 |
+
"file_path": {
|
| 140 |
+
"type": "string",
|
| 141 |
+
"description": "File path, relative to workspace",
|
| 142 |
+
},
|
| 143 |
+
"content": {
|
| 144 |
+
"type": "string",
|
| 145 |
+
"description": "Content to write to file",
|
| 146 |
+
},
|
| 147 |
+
"create_dirs": {
|
| 148 |
+
"type": "boolean",
|
| 149 |
+
"description": "Whether to create directories if they don't exist",
|
| 150 |
+
"default": True,
|
| 151 |
+
},
|
| 152 |
+
"create_backup": {
|
| 153 |
+
"type": "boolean",
|
| 154 |
+
"description": "Whether to create backup file if file already exists",
|
| 155 |
+
"default": False,
|
| 156 |
+
},
|
| 157 |
+
},
|
| 158 |
+
"required": ["file_path", "content"],
|
| 159 |
+
},
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
@staticmethod
|
| 163 |
+
def _get_write_multiple_files_tool() -> Dict[str, Any]:
|
| 164 |
+
"""批量写入多个文件工具定义"""
|
| 165 |
+
return {
|
| 166 |
+
"name": "write_multiple_files",
|
| 167 |
+
"description": "Write multiple files in a single operation (for batch implementation)",
|
| 168 |
+
"input_schema": {
|
| 169 |
+
"type": "object",
|
| 170 |
+
"properties": {
|
| 171 |
+
"file_implementations": {
|
| 172 |
+
"type": "string",
|
| 173 |
+
"description": 'JSON string mapping file paths to content, e.g., \'{"file1.py": "content1", "file2.py": "content2"}\'',
|
| 174 |
+
},
|
| 175 |
+
"create_dirs": {
|
| 176 |
+
"type": "boolean",
|
| 177 |
+
"description": "Whether to create directories if they don't exist",
|
| 178 |
+
"default": True,
|
| 179 |
+
},
|
| 180 |
+
"create_backup": {
|
| 181 |
+
"type": "boolean",
|
| 182 |
+
"description": "Whether to create backup files if they already exist",
|
| 183 |
+
"default": False,
|
| 184 |
+
},
|
| 185 |
+
"max_files": {
|
| 186 |
+
"type": "integer",
|
| 187 |
+
"description": "Maximum number of files to write in one operation",
|
| 188 |
+
"default": 5,
|
| 189 |
+
"minimum": 1,
|
| 190 |
+
"maximum": 10,
|
| 191 |
+
},
|
| 192 |
+
},
|
| 193 |
+
"required": ["file_implementations"],
|
| 194 |
+
},
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
@staticmethod
|
| 198 |
+
def _get_execute_python_tool() -> Dict[str, Any]:
|
| 199 |
+
"""Python执行工具定义"""
|
| 200 |
+
return {
|
| 201 |
+
"name": "execute_python",
|
| 202 |
+
"description": "Execute Python code and return output",
|
| 203 |
+
"input_schema": {
|
| 204 |
+
"type": "object",
|
| 205 |
+
"properties": {
|
| 206 |
+
"code": {"type": "string", "description": "Python code to execute"},
|
| 207 |
+
"timeout": {
|
| 208 |
+
"type": "integer",
|
| 209 |
+
"description": "Timeout in seconds",
|
| 210 |
+
"default": 30,
|
| 211 |
+
},
|
| 212 |
+
},
|
| 213 |
+
"required": ["code"],
|
| 214 |
+
},
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
@staticmethod
|
| 218 |
+
def _get_execute_bash_tool() -> Dict[str, Any]:
|
| 219 |
+
"""Bash执行工具定义"""
|
| 220 |
+
return {
|
| 221 |
+
"name": "execute_bash",
|
| 222 |
+
"description": "Execute bash command",
|
| 223 |
+
"input_schema": {
|
| 224 |
+
"type": "object",
|
| 225 |
+
"properties": {
|
| 226 |
+
"command": {
|
| 227 |
+
"type": "string",
|
| 228 |
+
"description": "Bash command to execute",
|
| 229 |
+
},
|
| 230 |
+
"timeout": {
|
| 231 |
+
"type": "integer",
|
| 232 |
+
"description": "Timeout in seconds",
|
| 233 |
+
"default": 30,
|
| 234 |
+
},
|
| 235 |
+
},
|
| 236 |
+
"required": ["command"],
|
| 237 |
+
},
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
@staticmethod
|
| 241 |
+
def _get_file_structure_tool() -> Dict[str, Any]:
|
| 242 |
+
"""文件结构获取工具定义"""
|
| 243 |
+
return {
|
| 244 |
+
"name": "get_file_structure",
|
| 245 |
+
"description": "Get directory file structure",
|
| 246 |
+
"input_schema": {
|
| 247 |
+
"type": "object",
|
| 248 |
+
"properties": {
|
| 249 |
+
"directory": {
|
| 250 |
+
"type": "string",
|
| 251 |
+
"description": "Directory path, relative to workspace",
|
| 252 |
+
"default": ".",
|
| 253 |
+
},
|
| 254 |
+
"max_depth": {
|
| 255 |
+
"type": "integer",
|
| 256 |
+
"description": "Maximum traversal depth",
|
| 257 |
+
"default": 5,
|
| 258 |
+
},
|
| 259 |
+
},
|
| 260 |
+
},
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
@staticmethod
|
| 264 |
+
def _get_search_code_references_tool() -> Dict[str, Any]:
|
| 265 |
+
"""统一代码参考搜索工具定义 - 合并了三个步骤为一个工具"""
|
| 266 |
+
return {
|
| 267 |
+
"name": "search_code_references",
|
| 268 |
+
"description": "UNIFIED TOOL: Search relevant reference code from index files. Combines directory setup, index loading, and searching in a single call.",
|
| 269 |
+
"input_schema": {
|
| 270 |
+
"type": "object",
|
| 271 |
+
"properties": {
|
| 272 |
+
"indexes_path": {
|
| 273 |
+
"type": "string",
|
| 274 |
+
"description": "Path to the indexes directory containing JSON index files",
|
| 275 |
+
},
|
| 276 |
+
"target_file": {
|
| 277 |
+
"type": "string",
|
| 278 |
+
"description": "Target file path to be implemented",
|
| 279 |
+
},
|
| 280 |
+
"keywords": {
|
| 281 |
+
"type": "string",
|
| 282 |
+
"description": "Search keywords, comma-separated",
|
| 283 |
+
"default": "",
|
| 284 |
+
},
|
| 285 |
+
"max_results": {
|
| 286 |
+
"type": "integer",
|
| 287 |
+
"description": "Maximum number of results to return",
|
| 288 |
+
"default": 10,
|
| 289 |
+
},
|
| 290 |
+
},
|
| 291 |
+
"required": ["indexes_path", "target_file"],
|
| 292 |
+
},
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
@staticmethod
|
| 296 |
+
def _get_search_code_tool() -> Dict[str, Any]:
|
| 297 |
+
"""代码搜索工具定义 - 在当前代码库中搜索模式"""
|
| 298 |
+
return {
|
| 299 |
+
"name": "search_code",
|
| 300 |
+
"description": "Search patterns in code files within the current repository",
|
| 301 |
+
"input_schema": {
|
| 302 |
+
"type": "object",
|
| 303 |
+
"properties": {
|
| 304 |
+
"pattern": {
|
| 305 |
+
"type": "string",
|
| 306 |
+
"description": "Search pattern",
|
| 307 |
+
},
|
| 308 |
+
"file_pattern": {
|
| 309 |
+
"type": "string",
|
| 310 |
+
"description": "File pattern (e.g., '*.py')",
|
| 311 |
+
"default": "*.py",
|
| 312 |
+
},
|
| 313 |
+
"use_regex": {
|
| 314 |
+
"type": "boolean",
|
| 315 |
+
"description": "Whether to use regular expressions",
|
| 316 |
+
"default": False,
|
| 317 |
+
},
|
| 318 |
+
"search_directory": {
|
| 319 |
+
"type": "string",
|
| 320 |
+
"description": "Specify search directory (optional)",
|
| 321 |
+
},
|
| 322 |
+
},
|
| 323 |
+
"required": ["pattern"],
|
| 324 |
+
},
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
@staticmethod
|
| 328 |
+
def _get_operation_history_tool() -> Dict[str, Any]:
|
| 329 |
+
"""操作历史工具定义"""
|
| 330 |
+
return {
|
| 331 |
+
"name": "get_operation_history",
|
| 332 |
+
"description": "Get operation history",
|
| 333 |
+
"input_schema": {
|
| 334 |
+
"type": "object",
|
| 335 |
+
"properties": {
|
| 336 |
+
"last_n": {
|
| 337 |
+
"type": "integer",
|
| 338 |
+
"description": "Return the last N operations",
|
| 339 |
+
"default": 10,
|
| 340 |
+
},
|
| 341 |
+
},
|
| 342 |
+
},
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
@staticmethod
|
| 346 |
+
def _get_get_indexes_overview_tool() -> Dict[str, Any]:
|
| 347 |
+
"""获取索引概览工具定义"""
|
| 348 |
+
return {
|
| 349 |
+
"name": "get_indexes_overview",
|
| 350 |
+
"description": "Get overview of all available reference code index information from specified directory",
|
| 351 |
+
"input_schema": {
|
| 352 |
+
"type": "object",
|
| 353 |
+
"properties": {
|
| 354 |
+
"indexes_path": {
|
| 355 |
+
"type": "string",
|
| 356 |
+
"description": "Path to the indexes directory containing JSON index files",
|
| 357 |
+
}
|
| 358 |
+
},
|
| 359 |
+
"required": ["indexes_path"],
|
| 360 |
+
},
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
@staticmethod
|
| 364 |
+
def _get_set_workspace_tool() -> Dict[str, Any]:
|
| 365 |
+
"""Set workspace directory tool definition"""
|
| 366 |
+
return {
|
| 367 |
+
"name": "set_workspace",
|
| 368 |
+
"description": "Set the workspace directory for file operations",
|
| 369 |
+
"input_schema": {
|
| 370 |
+
"type": "object",
|
| 371 |
+
"properties": {
|
| 372 |
+
"workspace_path": {
|
| 373 |
+
"type": "string",
|
| 374 |
+
"description": "Directory path for the workspace",
|
| 375 |
+
}
|
| 376 |
+
},
|
| 377 |
+
"required": ["workspace_path"],
|
| 378 |
+
},
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
# @staticmethod
|
| 382 |
+
# def _get_set_indexes_directory_tool() -> Dict[str, Any]:
|
| 383 |
+
# """Set indexes directory tool definition - DEPRECATED: Use unified search_code_references instead"""
|
| 384 |
+
# return {
|
| 385 |
+
# "name": "set_indexes_directory",
|
| 386 |
+
# "description": "Set the directory path for code reference indexes",
|
| 387 |
+
# "input_schema": {
|
| 388 |
+
# "type": "object",
|
| 389 |
+
# "properties": {
|
| 390 |
+
# "indexes_path": {
|
| 391 |
+
# "type": "string",
|
| 392 |
+
# "description": "Directory path containing index JSON files"
|
| 393 |
+
# }
|
| 394 |
+
# },
|
| 395 |
+
# "required": ["indexes_path"]
|
| 396 |
+
# }
|
| 397 |
+
# }
|
| 398 |
+
|
| 399 |
+
# Code evaluation tool definitions
|
| 400 |
+
@staticmethod
|
| 401 |
+
def _get_analyze_repo_structure_tool() -> Dict[str, Any]:
|
| 402 |
+
return {
|
| 403 |
+
"name": "analyze_repo_structure",
|
| 404 |
+
"description": "Perform comprehensive repository structure analysis",
|
| 405 |
+
"input_schema": {
|
| 406 |
+
"type": "object",
|
| 407 |
+
"properties": {
|
| 408 |
+
"repo_path": {
|
| 409 |
+
"type": "string",
|
| 410 |
+
"description": "Path to the repository to analyze",
|
| 411 |
+
}
|
| 412 |
+
},
|
| 413 |
+
"required": ["repo_path"],
|
| 414 |
+
},
|
| 415 |
+
}
|
| 416 |
+
|
| 417 |
+
@staticmethod
|
| 418 |
+
def _get_detect_dependencies_tool() -> Dict[str, Any]:
|
| 419 |
+
return {
|
| 420 |
+
"name": "detect_dependencies",
|
| 421 |
+
"description": "Detect and analyze project dependencies across multiple languages",
|
| 422 |
+
"input_schema": {
|
| 423 |
+
"type": "object",
|
| 424 |
+
"properties": {
|
| 425 |
+
"repo_path": {
|
| 426 |
+
"type": "string",
|
| 427 |
+
"description": "Path to the repository",
|
| 428 |
+
}
|
| 429 |
+
},
|
| 430 |
+
"required": ["repo_path"],
|
| 431 |
+
},
|
| 432 |
+
}
|
| 433 |
+
|
| 434 |
+
@staticmethod
|
| 435 |
+
def _get_assess_code_quality_tool() -> Dict[str, Any]:
|
| 436 |
+
return {
|
| 437 |
+
"name": "assess_code_quality",
|
| 438 |
+
"description": "Assess code quality metrics and identify potential issues",
|
| 439 |
+
"input_schema": {
|
| 440 |
+
"type": "object",
|
| 441 |
+
"properties": {
|
| 442 |
+
"repo_path": {
|
| 443 |
+
"type": "string",
|
| 444 |
+
"description": "Path to the repository",
|
| 445 |
+
}
|
| 446 |
+
},
|
| 447 |
+
"required": ["repo_path"],
|
| 448 |
+
},
|
| 449 |
+
}
|
| 450 |
+
|
| 451 |
+
@staticmethod
|
| 452 |
+
def _get_evaluate_documentation_tool() -> Dict[str, Any]:
|
| 453 |
+
return {
|
| 454 |
+
"name": "evaluate_documentation",
|
| 455 |
+
"description": "Evaluate documentation completeness and quality",
|
| 456 |
+
"input_schema": {
|
| 457 |
+
"type": "object",
|
| 458 |
+
"properties": {
|
| 459 |
+
"repo_path": {
|
| 460 |
+
"type": "string",
|
| 461 |
+
"description": "Path to the repository",
|
| 462 |
+
},
|
| 463 |
+
"docs_path": {
|
| 464 |
+
"type": "string",
|
| 465 |
+
"description": "Optional path to external documentation",
|
| 466 |
+
},
|
| 467 |
+
},
|
| 468 |
+
"required": ["repo_path"],
|
| 469 |
+
},
|
| 470 |
+
}
|
| 471 |
+
|
| 472 |
+
@staticmethod
|
| 473 |
+
def _get_check_reproduction_readiness_tool() -> Dict[str, Any]:
|
| 474 |
+
return {
|
| 475 |
+
"name": "check_reproduction_readiness",
|
| 476 |
+
"description": "Assess repository readiness for reproduction and validation",
|
| 477 |
+
"input_schema": {
|
| 478 |
+
"type": "object",
|
| 479 |
+
"properties": {
|
| 480 |
+
"repo_path": {
|
| 481 |
+
"type": "string",
|
| 482 |
+
"description": "Path to the repository",
|
| 483 |
+
},
|
| 484 |
+
"docs_path": {
|
| 485 |
+
"type": "string",
|
| 486 |
+
"description": "Optional path to reproduction documentation",
|
| 487 |
+
},
|
| 488 |
+
},
|
| 489 |
+
"required": ["repo_path"],
|
| 490 |
+
},
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
+
@staticmethod
|
| 494 |
+
def _get_generate_evaluation_summary_tool() -> Dict[str, Any]:
|
| 495 |
+
return {
|
| 496 |
+
"name": "generate_evaluation_summary",
|
| 497 |
+
"description": "Generate comprehensive evaluation summary combining all analysis results",
|
| 498 |
+
"input_schema": {
|
| 499 |
+
"type": "object",
|
| 500 |
+
"properties": {
|
| 501 |
+
"repo_path": {
|
| 502 |
+
"type": "string",
|
| 503 |
+
"description": "Path to the repository",
|
| 504 |
+
},
|
| 505 |
+
"docs_path": {
|
| 506 |
+
"type": "string",
|
| 507 |
+
"description": "Optional path to reproduction documentation",
|
| 508 |
+
},
|
| 509 |
+
},
|
| 510 |
+
"required": ["repo_path"],
|
| 511 |
+
},
|
| 512 |
+
}
|
| 513 |
+
|
| 514 |
+
@staticmethod
|
| 515 |
+
def _get_detect_empty_files_tool() -> Dict[str, Any]:
|
| 516 |
+
return {
|
| 517 |
+
"name": "detect_empty_files",
|
| 518 |
+
"description": "Detect empty files in the repository that may need implementation",
|
| 519 |
+
"input_schema": {
|
| 520 |
+
"type": "object",
|
| 521 |
+
"properties": {
|
| 522 |
+
"repo_path": {
|
| 523 |
+
"type": "string",
|
| 524 |
+
"description": "Path to the repository to analyze",
|
| 525 |
+
}
|
| 526 |
+
},
|
| 527 |
+
"required": ["repo_path"],
|
| 528 |
+
},
|
| 529 |
+
}
|
| 530 |
+
|
| 531 |
+
@staticmethod
|
| 532 |
+
def _get_detect_missing_files_tool() -> Dict[str, Any]:
|
| 533 |
+
return {
|
| 534 |
+
"name": "detect_missing_files",
|
| 535 |
+
"description": "Detect missing essential files like main programs, tests, requirements, etc.",
|
| 536 |
+
"input_schema": {
|
| 537 |
+
"type": "object",
|
| 538 |
+
"properties": {
|
| 539 |
+
"repo_path": {
|
| 540 |
+
"type": "string",
|
| 541 |
+
"description": "Path to the repository to analyze",
|
| 542 |
+
}
|
| 543 |
+
},
|
| 544 |
+
"required": ["repo_path"],
|
| 545 |
+
},
|
| 546 |
+
}
|
| 547 |
+
|
| 548 |
+
@staticmethod
|
| 549 |
+
def _get_generate_code_revision_report_tool() -> Dict[str, Any]:
|
| 550 |
+
return {
|
| 551 |
+
"name": "generate_code_revision_report",
|
| 552 |
+
"description": "Generate comprehensive code revision report combining empty files, missing files, and quality analysis",
|
| 553 |
+
"input_schema": {
|
| 554 |
+
"type": "object",
|
| 555 |
+
"properties": {
|
| 556 |
+
"repo_path": {
|
| 557 |
+
"type": "string",
|
| 558 |
+
"description": "Path to the repository to analyze",
|
| 559 |
+
},
|
| 560 |
+
"docs_path": {
|
| 561 |
+
"type": "string",
|
| 562 |
+
"description": "Optional path to documentation",
|
| 563 |
+
},
|
| 564 |
+
},
|
| 565 |
+
"required": ["repo_path"],
|
| 566 |
+
},
|
| 567 |
+
}
|
| 568 |
+
|
| 569 |
+
@staticmethod
|
| 570 |
+
def get_available_tool_sets() -> Dict[str, str]:
|
| 571 |
+
"""
|
| 572 |
+
获取可用的工具集合
|
| 573 |
+
Get available tool sets
|
| 574 |
+
"""
|
| 575 |
+
return {
|
| 576 |
+
"code_implementation": "代码实现相关工具集 / Code implementation tool set",
|
| 577 |
+
"code_evaluation": "代码评估相关工具集 / Code evaluation tool set",
|
| 578 |
+
# 可以在这里添加更多工具集
|
| 579 |
+
# "data_analysis": "数据分析工具集 / Data analysis tool set",
|
| 580 |
+
# "web_scraping": "网页爬取工具集 / Web scraping tool set",
|
| 581 |
+
}
|
| 582 |
+
|
| 583 |
+
@staticmethod
|
| 584 |
+
def get_tool_set(tool_set_name: str) -> List[Dict[str, Any]]:
|
| 585 |
+
"""
|
| 586 |
+
根据名称获取特定的工具集
|
| 587 |
+
Get specific tool set by name
|
| 588 |
+
"""
|
| 589 |
+
tool_sets = {
|
| 590 |
+
"code_implementation": MCPToolDefinitions.get_code_implementation_tools(),
|
| 591 |
+
"code_evaluation": MCPToolDefinitions.get_code_evaluation_tools(),
|
| 592 |
+
}
|
| 593 |
+
|
| 594 |
+
return tool_sets.get(tool_set_name, [])
|
| 595 |
+
|
| 596 |
+
@staticmethod
|
| 597 |
+
def get_all_tools() -> List[Dict[str, Any]]:
|
| 598 |
+
"""
|
| 599 |
+
获取所有可用工具
|
| 600 |
+
Get all available tools
|
| 601 |
+
"""
|
| 602 |
+
all_tools = []
|
| 603 |
+
for tool_set_name in MCPToolDefinitions.get_available_tool_sets().keys():
|
| 604 |
+
all_tools.extend(MCPToolDefinitions.get_tool_set(tool_set_name))
|
| 605 |
+
return all_tools
|
| 606 |
+
|
| 607 |
+
|
| 608 |
+
# 便捷访问函数
|
| 609 |
+
def get_mcp_tools(tool_set: str = "code_implementation") -> List[Dict[str, Any]]:
|
| 610 |
+
"""
|
| 611 |
+
便捷函数:获取MCP工具定义
|
| 612 |
+
Convenience function: Get MCP tool definitions
|
| 613 |
+
|
| 614 |
+
Args:
|
| 615 |
+
tool_set: 工具集名称 (默认: "code_implementation")
|
| 616 |
+
|
| 617 |
+
Returns:
|
| 618 |
+
工具定义列表
|
| 619 |
+
"""
|
| 620 |
+
return MCPToolDefinitions.get_tool_set(tool_set)
|
projects/ui/DeepCode/prompts/code_prompts.py
ADDED
|
@@ -0,0 +1,1768 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Prompt templates for the DeepCode agent system.
|
| 3 |
+
|
| 4 |
+
RECENT UPDATES (针对论文代码复现优化):
|
| 5 |
+
1. 简化并优化了文件结构生成逻辑,确保结构简洁且富有逻辑性
|
| 6 |
+
2. 明确标识需要复现的核心文件和组件,由LLM智能判断优先级
|
| 7 |
+
3. 优化了多agent协作的信息总结效率,减少冗余信息传递
|
| 8 |
+
4. 移除了时间线等次要信息,专注于高质量代码复现
|
| 9 |
+
5. 保持prompt完整性的同时提高了简洁性和可理解性
|
| 10 |
+
6. 采用更清晰的结构化格式,便于LLM理解和执行
|
| 11 |
+
|
| 12 |
+
核心改进:
|
| 13 |
+
- PAPER_ALGORITHM_ANALYSIS_PROMPT: 专注算法提取,明确实现优先级
|
| 14 |
+
- PAPER_CONCEPT_ANALYSIS_PROMPT: 专注系统架构,突出概念到代码的映射
|
| 15 |
+
- CODE_PLANNING_PROMPT: 整合前两者输出,生成高质量复现计划
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
# Paper to Code Workflow Prompts
|
| 19 |
+
PAPER_INPUT_ANALYZER_PROMPT = """You are a precise input analyzer for paper-to-code tasks. You MUST return only a JSON object with no additional text.
|
| 20 |
+
|
| 21 |
+
Task: Analyze input text and identify file paths/URLs to determine appropriate input type.
|
| 22 |
+
|
| 23 |
+
Input Analysis Rules:
|
| 24 |
+
1. Path Detection:
|
| 25 |
+
- Scan input text for file paths or URLs
|
| 26 |
+
- Use first valid path/URL if multiple found
|
| 27 |
+
- Treat as text input if no valid path/URL found
|
| 28 |
+
|
| 29 |
+
2. Path Type Classification:
|
| 30 |
+
- URL (starts with http:// or https://): input_type = "url", path = "detected URL"
|
| 31 |
+
- PDF file path: input_type = "file", path = "detected file path"
|
| 32 |
+
- Directory path: input_type = "directory", path = "detected directory path"
|
| 33 |
+
- No path/URL detected: input_type = "text", path = null
|
| 34 |
+
|
| 35 |
+
3. Requirements Analysis:
|
| 36 |
+
- Extract ONLY requirements from additional_input
|
| 37 |
+
- DO NOT modify or interpret requirements
|
| 38 |
+
|
| 39 |
+
CRITICAL OUTPUT RESTRICTIONS:
|
| 40 |
+
- RETURN ONLY RAW JSON - NO TEXT BEFORE OR AFTER
|
| 41 |
+
- NO markdown code blocks (```json)
|
| 42 |
+
- NO explanatory text or descriptions
|
| 43 |
+
- NO tool call information
|
| 44 |
+
- NO analysis summaries
|
| 45 |
+
- JUST THE JSON OBJECT BELOW
|
| 46 |
+
|
| 47 |
+
{
|
| 48 |
+
"input_type": "text|file|directory|url",
|
| 49 |
+
"path": "detected path or URL or null",
|
| 50 |
+
"paper_info": {
|
| 51 |
+
"title": "N/A for text input",
|
| 52 |
+
"authors": ["N/A for text input"],
|
| 53 |
+
"year": "N/A for text input"
|
| 54 |
+
},
|
| 55 |
+
"requirements": [
|
| 56 |
+
"exact requirement from additional_input"
|
| 57 |
+
]
|
| 58 |
+
}
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
PAPER_DOWNLOADER_PROMPT = """You are a precise paper downloader that processes input from PaperInputAnalyzerAgent.
|
| 62 |
+
|
| 63 |
+
Task: Handle paper according to input type and save to "./deepcode_lab/papers/id/id.md"
|
| 64 |
+
Note: Generate id (id is a number) by counting files in "./deepcode_lab/papers/" directory and increment by 1.
|
| 65 |
+
|
| 66 |
+
CRITICAL RULE: NEVER use write_file tool to create paper content directly. Always use file-downloader tools for PDF/document conversion.
|
| 67 |
+
|
| 68 |
+
Processing Rules:
|
| 69 |
+
1. URL Input (input_type = "url"):
|
| 70 |
+
- Use "file-downloader" tool to download paper
|
| 71 |
+
- Extract metadata (title, authors, year)
|
| 72 |
+
- Return saved file path and metadata
|
| 73 |
+
|
| 74 |
+
2. File Input (input_type = "file"):
|
| 75 |
+
- Move file to "./deepcode_lab/papers/id/" using move_file_to tool
|
| 76 |
+
- The move_file_to tool will automatically convert PDF/documents to .md format
|
| 77 |
+
- NEVER manually extract content or use write_file - let the conversion tools handle this
|
| 78 |
+
- Return new saved file path and metadata
|
| 79 |
+
|
| 80 |
+
3. Directory Input (input_type = "directory"):
|
| 81 |
+
- Verify directory exists
|
| 82 |
+
- Return to PaperInputAnalyzerAgent for processing
|
| 83 |
+
- Set status as "failure" with message
|
| 84 |
+
|
| 85 |
+
4. Text Input (input_type = "text"):
|
| 86 |
+
- No file operations needed
|
| 87 |
+
- Set paper_path as null
|
| 88 |
+
- Use paper_info from input
|
| 89 |
+
|
| 90 |
+
Input Format:
|
| 91 |
+
{
|
| 92 |
+
"input_type": "file|directory|url|text",
|
| 93 |
+
"path": "detected path or null",
|
| 94 |
+
"paper_info": {
|
| 95 |
+
"title": "paper title or N/A",
|
| 96 |
+
"authors": ["author names or N/A"],
|
| 97 |
+
"year": "publication year or N/A"
|
| 98 |
+
},
|
| 99 |
+
"requirements": ["requirement1", "requirement2"]
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
Output Format (DO NOT MODIFY):
|
| 103 |
+
{
|
| 104 |
+
"status": "success|failure",
|
| 105 |
+
"paper_path": "path to paper file or null for text input",
|
| 106 |
+
"metadata": {
|
| 107 |
+
"title": "extracted or provided title",
|
| 108 |
+
"authors": ["extracted or provided authors"],
|
| 109 |
+
"year": "extracted or provided year"
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
"""
|
| 113 |
+
|
| 114 |
+
PAPER_REFERENCE_ANALYZER_PROMPT = """You are an expert academic paper reference analyzer specializing in computer science and machine learning.
|
| 115 |
+
|
| 116 |
+
Task: Analyze paper and identify 5 most relevant references that have GitHub repositories.
|
| 117 |
+
|
| 118 |
+
Constraints:
|
| 119 |
+
- ONLY select references with GitHub repositories
|
| 120 |
+
- DO NOT use target paper's official implementation
|
| 121 |
+
- DO NOT use repositories directly associated with target paper
|
| 122 |
+
- CAN analyze code implementations from referenced papers
|
| 123 |
+
- Focus on references with good implementations solving similar problems
|
| 124 |
+
|
| 125 |
+
Analysis Criteria:
|
| 126 |
+
1. GitHub Repository Quality (40%):
|
| 127 |
+
- Star count, activity, maintenance
|
| 128 |
+
- Documentation quality
|
| 129 |
+
- Community adoption
|
| 130 |
+
- Last update date
|
| 131 |
+
|
| 132 |
+
2. Implementation Relevance (30%):
|
| 133 |
+
- References from methodology/implementation sections
|
| 134 |
+
- Algorithmic details
|
| 135 |
+
- Core component descriptions
|
| 136 |
+
- Code implementation quality
|
| 137 |
+
|
| 138 |
+
3. Technical Depth (20%):
|
| 139 |
+
- Algorithm/method similarity
|
| 140 |
+
- Technical foundation relationship
|
| 141 |
+
- Implementation details
|
| 142 |
+
- Code structure
|
| 143 |
+
|
| 144 |
+
4. Academic Influence (10%):
|
| 145 |
+
- Publication venue quality
|
| 146 |
+
- Author expertise
|
| 147 |
+
- Research impact
|
| 148 |
+
- Citation influence
|
| 149 |
+
|
| 150 |
+
Analysis Steps:
|
| 151 |
+
1. Extract all references from paper
|
| 152 |
+
2. Filter references with GitHub repositories
|
| 153 |
+
3. Analyze repositories based on criteria
|
| 154 |
+
4. Calculate relevance scores
|
| 155 |
+
5. Select and rank top 5 references
|
| 156 |
+
|
| 157 |
+
Output Format:
|
| 158 |
+
{
|
| 159 |
+
"selected_references": [
|
| 160 |
+
{
|
| 161 |
+
"rank": 1,
|
| 162 |
+
"title": "paper title",
|
| 163 |
+
"authors": ["author1", "author2"],
|
| 164 |
+
"year": "publication year",
|
| 165 |
+
"relevance_score": 0.95,
|
| 166 |
+
"citation_context": "how cited in main paper",
|
| 167 |
+
"key_contributions": ["contribution1", "contribution2"],
|
| 168 |
+
"implementation_value": "why valuable for implementation",
|
| 169 |
+
"github_info": {
|
| 170 |
+
"repository_url": "GitHub repository URL",
|
| 171 |
+
"stars_count": "number of stars",
|
| 172 |
+
"last_updated": "last update date",
|
| 173 |
+
"repository_quality": "repository quality assessment",
|
| 174 |
+
"key_features": ["feature1", "feature2"],
|
| 175 |
+
"documentation_quality": "documentation assessment",
|
| 176 |
+
"community_activity": "community engagement description"
|
| 177 |
+
},
|
| 178 |
+
"original_reference": "Complete reference text from paper"
|
| 179 |
+
}
|
| 180 |
+
],
|
| 181 |
+
"analysis_summary": "selection process and key findings",
|
| 182 |
+
"github_repositories_found": "total number of references with GitHub repositories"
|
| 183 |
+
}
|
| 184 |
+
"""
|
| 185 |
+
|
| 186 |
+
GITHUB_DOWNLOAD_PROMPT = """You are an expert GitHub repository downloader.
|
| 187 |
+
|
| 188 |
+
Task: Download GitHub repositories to specified directory structure.
|
| 189 |
+
|
| 190 |
+
Process:
|
| 191 |
+
1. For each repository:
|
| 192 |
+
- Create directory: {paper_dir}/code_base/
|
| 193 |
+
- Download repository to directory
|
| 194 |
+
|
| 195 |
+
Requirements:
|
| 196 |
+
- Use interpreter tool to execute download script
|
| 197 |
+
- Monitor interpreter output for errors/warnings
|
| 198 |
+
- Verify download status through interpreter response
|
| 199 |
+
|
| 200 |
+
Output Format:
|
| 201 |
+
{
|
| 202 |
+
"downloaded_repos": [
|
| 203 |
+
{
|
| 204 |
+
"reference_number": "1",
|
| 205 |
+
"paper_title": "paper title",
|
| 206 |
+
"repo_url": "github repository URL",
|
| 207 |
+
"save_path": "{paper_dir}/code_base/name_of_repo",
|
| 208 |
+
"status": "success|failed",
|
| 209 |
+
"notes": "relevant notes about download"
|
| 210 |
+
}
|
| 211 |
+
],
|
| 212 |
+
"summary": "Brief summary of download process"
|
| 213 |
+
}
|
| 214 |
+
"""
|
| 215 |
+
|
| 216 |
+
# Code Analysis Prompts
|
| 217 |
+
PAPER_ALGORITHM_ANALYSIS_PROMPT = """You are extracting COMPLETE implementation details from a research paper. Your goal is to capture EVERY algorithm, formula, and technical detail needed for perfect reproduction.
|
| 218 |
+
|
| 219 |
+
# INTELLIGENT DOCUMENT READING STRATEGY
|
| 220 |
+
|
| 221 |
+
## IMPORTANT: Use Segmented Reading for Algorithm Extraction
|
| 222 |
+
To avoid token limits and efficiently extract algorithm details, use the intelligent segmentation system:
|
| 223 |
+
|
| 224 |
+
1. **Primary Algorithm Extraction** - Use read_document_segments tool with:
|
| 225 |
+
- query_type: "algorithm_extraction"
|
| 226 |
+
- keywords: ["algorithm", "method", "procedure", "formula", "equation", "implementation"]
|
| 227 |
+
- max_segments: 3
|
| 228 |
+
- max_total_chars: 6000
|
| 229 |
+
|
| 230 |
+
2. **Supplementary Details** - Make additional calls if needed with:
|
| 231 |
+
- keywords: ["hyperparameter", "training", "optimization", "loss", "objective"]
|
| 232 |
+
- keywords: ["experiment", "setup", "configuration", "parameter"]
|
| 233 |
+
|
| 234 |
+
3. **This approach ensures** you get the most algorithm-relevant content without missing critical details
|
| 235 |
+
|
| 236 |
+
# DETAILED EXTRACTION PROTOCOL
|
| 237 |
+
|
| 238 |
+
## 1. INTELLIGENT ALGORITHM SCAN
|
| 239 |
+
Use the segmented reading approach to focus on algorithm sections:
|
| 240 |
+
- Method/Algorithm sections (captured automatically by segmentation)
|
| 241 |
+
- Implementation Details (targeted retrieval)
|
| 242 |
+
- Hyperparameters and training details (focused extraction)
|
| 243 |
+
|
| 244 |
+
## 2. ALGORITHM DEEP EXTRACTION
|
| 245 |
+
For EVERY algorithm/method/procedure mentioned:
|
| 246 |
+
|
| 247 |
+
### Algorithm Structure
|
| 248 |
+
```yaml
|
| 249 |
+
algorithm_name: "[Exact name from paper]"
|
| 250 |
+
section: "[e.g., Section 3.2]"
|
| 251 |
+
algorithm_box: "[e.g., Algorithm 1 on page 4]"
|
| 252 |
+
|
| 253 |
+
pseudocode: |
|
| 254 |
+
[COPY THE EXACT PSEUDOCODE FROM PAPER]
|
| 255 |
+
Input: ...
|
| 256 |
+
Output: ...
|
| 257 |
+
1. Initialize ...
|
| 258 |
+
2. For each ...
|
| 259 |
+
2.1 Calculate ...
|
| 260 |
+
[Keep exact formatting and numbering]
|
| 261 |
+
|
| 262 |
+
mathematical_formulation:
|
| 263 |
+
- equation: "[Copy formula EXACTLY, e.g., L = L_task + λ*L_explain]"
|
| 264 |
+
equation_number: "[e.g., Eq. 3]"
|
| 265 |
+
where:
|
| 266 |
+
L_task: "task loss"
|
| 267 |
+
L_explain: "explanation loss"
|
| 268 |
+
λ: "weighting parameter (default: 0.5)"
|
| 269 |
+
|
| 270 |
+
step_by_step_breakdown:
|
| 271 |
+
1. "[Detailed explanation of what step 1 does]"
|
| 272 |
+
2. "[What step 2 computes and why]"
|
| 273 |
+
|
| 274 |
+
implementation_details:
|
| 275 |
+
- "Uses softmax temperature τ = 0.1"
|
| 276 |
+
- "Gradient clipping at norm 1.0"
|
| 277 |
+
- "Initialize weights with Xavier uniform"
|
| 278 |
+
```
|
| 279 |
+
|
| 280 |
+
## 3. COMPONENT EXTRACTION
|
| 281 |
+
For EVERY component/module mentioned:
|
| 282 |
+
|
| 283 |
+
### Component Details
|
| 284 |
+
```yaml
|
| 285 |
+
component_name: "[e.g., Mask Network, Critic Network]"
|
| 286 |
+
purpose: "[What this component does in the system]"
|
| 287 |
+
architecture:
|
| 288 |
+
input: "[shape and meaning]"
|
| 289 |
+
layers:
|
| 290 |
+
- "[Conv2d(3, 64, kernel=3, stride=1)]"
|
| 291 |
+
- "[ReLU activation]"
|
| 292 |
+
- "[BatchNorm2d(64)]"
|
| 293 |
+
output: "[shape and meaning]"
|
| 294 |
+
|
| 295 |
+
special_features:
|
| 296 |
+
- "[Any unique aspects]"
|
| 297 |
+
- "[Special initialization]"
|
| 298 |
+
```
|
| 299 |
+
|
| 300 |
+
## 4. TRAINING PROCEDURE
|
| 301 |
+
Extract the COMPLETE training process:
|
| 302 |
+
|
| 303 |
+
```yaml
|
| 304 |
+
training_loop:
|
| 305 |
+
outer_iterations: "[number or condition]"
|
| 306 |
+
inner_iterations: "[number or condition]"
|
| 307 |
+
|
| 308 |
+
steps:
|
| 309 |
+
1. "Sample batch of size B from buffer"
|
| 310 |
+
2. "Compute importance weights using..."
|
| 311 |
+
3. "Update policy with loss..."
|
| 312 |
+
|
| 313 |
+
loss_functions:
|
| 314 |
+
- name: "policy_loss"
|
| 315 |
+
formula: "[exact formula]"
|
| 316 |
+
components: "[what each term means]"
|
| 317 |
+
|
| 318 |
+
optimization:
|
| 319 |
+
optimizer: "Adam"
|
| 320 |
+
learning_rate: "3e-4"
|
| 321 |
+
lr_schedule: "linear decay to 0"
|
| 322 |
+
gradient_norm: "clip at 0.5"
|
| 323 |
+
```
|
| 324 |
+
|
| 325 |
+
## 5. HYPERPARAMETERS HUNT
|
| 326 |
+
Search EVERYWHERE (text, tables, captions) for:
|
| 327 |
+
|
| 328 |
+
```yaml
|
| 329 |
+
hyperparameters:
|
| 330 |
+
# Training
|
| 331 |
+
batch_size: 64
|
| 332 |
+
buffer_size: 1e6
|
| 333 |
+
discount_gamma: 0.99
|
| 334 |
+
|
| 335 |
+
# Architecture
|
| 336 |
+
hidden_units: [256, 256]
|
| 337 |
+
activation: "ReLU"
|
| 338 |
+
|
| 339 |
+
# Algorithm-specific
|
| 340 |
+
explanation_weight: 0.5
|
| 341 |
+
exploration_bonus_scale: 0.1
|
| 342 |
+
reset_probability: 0.3
|
| 343 |
+
|
| 344 |
+
# Found in:
|
| 345 |
+
location_references:
|
| 346 |
+
- "batch_size: Table 1"
|
| 347 |
+
- "hidden_units: Section 4.1"
|
| 348 |
+
```
|
| 349 |
+
|
| 350 |
+
# OUTPUT FORMAT
|
| 351 |
+
```yaml
|
| 352 |
+
complete_algorithm_extraction:
|
| 353 |
+
paper_structure:
|
| 354 |
+
method_sections: "[3, 3.1, 3.2, 3.3, 4]"
|
| 355 |
+
algorithm_count: "[total number found]"
|
| 356 |
+
|
| 357 |
+
main_algorithm:
|
| 358 |
+
[COMPLETE DETAILS AS ABOVE]
|
| 359 |
+
|
| 360 |
+
supporting_algorithms:
|
| 361 |
+
- [EACH SUPPORTING ALGORITHM WITH FULL DETAILS]
|
| 362 |
+
|
| 363 |
+
components:
|
| 364 |
+
- [EVERY COMPONENT WITH ARCHITECTURE]
|
| 365 |
+
|
| 366 |
+
training_details:
|
| 367 |
+
[COMPLETE TRAINING PROCEDURE]
|
| 368 |
+
|
| 369 |
+
all_hyperparameters:
|
| 370 |
+
[EVERY PARAMETER WITH VALUE AND SOURCE]
|
| 371 |
+
|
| 372 |
+
implementation_notes:
|
| 373 |
+
- "[Any implementation hint from paper]"
|
| 374 |
+
- "[Tricks mentioned in text]"
|
| 375 |
+
|
| 376 |
+
missing_but_critical:
|
| 377 |
+
- "[What's not specified but essential]"
|
| 378 |
+
- "[With suggested defaults]"
|
| 379 |
+
```
|
| 380 |
+
|
| 381 |
+
BE EXHAUSTIVE. A developer should be able to implement the ENTIRE paper using only your extraction."""
|
| 382 |
+
|
| 383 |
+
PAPER_CONCEPT_ANALYSIS_PROMPT = """You are doing a COMPREHENSIVE analysis of a research paper to understand its complete structure, contributions, and implementation requirements.
|
| 384 |
+
|
| 385 |
+
# OBJECTIVE
|
| 386 |
+
Map out the ENTIRE paper structure and identify ALL components that need implementation for successful reproduction.
|
| 387 |
+
|
| 388 |
+
# INTELLIGENT DOCUMENT READING STRATEGY
|
| 389 |
+
|
| 390 |
+
## IMPORTANT: Use Segmented Reading for Optimal Performance
|
| 391 |
+
Instead of reading the entire document at once (which may hit token limits), use the intelligent segmentation system:
|
| 392 |
+
|
| 393 |
+
1. **Use read_document_segments tool** with these parameters:
|
| 394 |
+
- query_type: "concept_analysis"
|
| 395 |
+
- keywords: ["introduction", "overview", "architecture", "system", "framework", "concept", "method"]
|
| 396 |
+
- max_segments: 3
|
| 397 |
+
- max_total_chars: 6000
|
| 398 |
+
|
| 399 |
+
2. **This will automatically find and retrieve** the most relevant sections for concept analysis without token overflow
|
| 400 |
+
|
| 401 |
+
3. **If you need additional sections**, make follow-up calls with different keywords like ["experiment", "evaluation", "results"] or ["conclusion", "discussion"]
|
| 402 |
+
|
| 403 |
+
# COMPREHENSIVE ANALYSIS PROTOCOL
|
| 404 |
+
|
| 405 |
+
## 1. INTELLIGENT PAPER STRUCTURAL ANALYSIS
|
| 406 |
+
Use the segmented reading approach to create a complete map:
|
| 407 |
+
|
| 408 |
+
```yaml
|
| 409 |
+
paper_structure_map:
|
| 410 |
+
title: "[Full paper title]"
|
| 411 |
+
|
| 412 |
+
sections:
|
| 413 |
+
1_introduction:
|
| 414 |
+
main_claims: "[What the paper claims to achieve]"
|
| 415 |
+
problem_definition: "[Exact problem being solved]"
|
| 416 |
+
|
| 417 |
+
2_related_work:
|
| 418 |
+
key_comparisons: "[Methods this work builds upon or competes with]"
|
| 419 |
+
|
| 420 |
+
3_method: # May have multiple subsections
|
| 421 |
+
subsections:
|
| 422 |
+
3.1: "[Title and main content]"
|
| 423 |
+
3.2: "[Title and main content]"
|
| 424 |
+
algorithms_presented: "[List all algorithms by name]"
|
| 425 |
+
|
| 426 |
+
4_experiments:
|
| 427 |
+
environments: "[All test environments/datasets]"
|
| 428 |
+
baselines: "[All comparison methods]"
|
| 429 |
+
metrics: "[All evaluation metrics used]"
|
| 430 |
+
|
| 431 |
+
5_results:
|
| 432 |
+
main_findings: "[Key results that prove the method works]"
|
| 433 |
+
tables_figures: "[Important result tables/figures to reproduce]"
|
| 434 |
+
```
|
| 435 |
+
|
| 436 |
+
## 2. METHOD DECOMPOSITION
|
| 437 |
+
For the main method/approach:
|
| 438 |
+
|
| 439 |
+
```yaml
|
| 440 |
+
method_decomposition:
|
| 441 |
+
method_name: "[Full name and acronym]"
|
| 442 |
+
|
| 443 |
+
core_components: # Break down into implementable pieces
|
| 444 |
+
component_1:
|
| 445 |
+
name: "[e.g., State Importance Estimator]"
|
| 446 |
+
purpose: "[Why this component exists]"
|
| 447 |
+
paper_section: "[Where it's described]"
|
| 448 |
+
|
| 449 |
+
component_2:
|
| 450 |
+
name: "[e.g., Policy Refinement Module]"
|
| 451 |
+
purpose: "[Its role in the system]"
|
| 452 |
+
paper_section: "[Where it's described]"
|
| 453 |
+
|
| 454 |
+
component_interactions:
|
| 455 |
+
- "[How component 1 feeds into component 2]"
|
| 456 |
+
- "[Data flow between components]"
|
| 457 |
+
|
| 458 |
+
theoretical_foundation:
|
| 459 |
+
key_insight: "[The main theoretical insight]"
|
| 460 |
+
why_it_works: "[Intuitive explanation]"
|
| 461 |
+
```
|
| 462 |
+
|
| 463 |
+
## 3. IMPLEMENTATION REQUIREMENTS MAPPING
|
| 464 |
+
Map paper content to code requirements:
|
| 465 |
+
|
| 466 |
+
```yaml
|
| 467 |
+
implementation_map:
|
| 468 |
+
algorithms_to_implement:
|
| 469 |
+
- algorithm: "[Name from paper]"
|
| 470 |
+
section: "[Where defined]"
|
| 471 |
+
complexity: "[Simple/Medium/Complex]"
|
| 472 |
+
dependencies: "[What it needs to work]"
|
| 473 |
+
|
| 474 |
+
models_to_build:
|
| 475 |
+
- model: "[Neural network or other model]"
|
| 476 |
+
architecture_location: "[Section describing it]"
|
| 477 |
+
purpose: "[What this model does]"
|
| 478 |
+
|
| 479 |
+
data_processing:
|
| 480 |
+
- pipeline: "[Data preprocessing needed]"
|
| 481 |
+
requirements: "[What the data should look like]"
|
| 482 |
+
|
| 483 |
+
evaluation_suite:
|
| 484 |
+
- metric: "[Metric name]"
|
| 485 |
+
formula_location: "[Where it's defined]"
|
| 486 |
+
purpose: "[What it measures]"
|
| 487 |
+
```
|
| 488 |
+
|
| 489 |
+
## 4. EXPERIMENT REPRODUCTION PLAN
|
| 490 |
+
Identify ALL experiments needed:
|
| 491 |
+
|
| 492 |
+
```yaml
|
| 493 |
+
experiments_analysis:
|
| 494 |
+
main_results:
|
| 495 |
+
- experiment: "[Name/description]"
|
| 496 |
+
proves: "[What claim this validates]"
|
| 497 |
+
requires: "[Components needed to run this]"
|
| 498 |
+
expected_outcome: "[Specific numbers/trends]"
|
| 499 |
+
|
| 500 |
+
ablation_studies:
|
| 501 |
+
- study: "[What is being ablated]"
|
| 502 |
+
purpose: "[What this demonstrates]"
|
| 503 |
+
|
| 504 |
+
baseline_comparisons:
|
| 505 |
+
- baseline: "[Method name]"
|
| 506 |
+
implementation_required: "[Yes/No/Partial]"
|
| 507 |
+
source: "[Where to find implementation]"
|
| 508 |
+
```
|
| 509 |
+
|
| 510 |
+
## 5. CRITICAL SUCCESS FACTORS
|
| 511 |
+
What defines successful reproduction:
|
| 512 |
+
|
| 513 |
+
```yaml
|
| 514 |
+
success_criteria:
|
| 515 |
+
must_achieve:
|
| 516 |
+
- "[Primary result that must be reproduced]"
|
| 517 |
+
- "[Core behavior that must be demonstrated]"
|
| 518 |
+
|
| 519 |
+
should_achieve:
|
| 520 |
+
- "[Secondary results that validate the method]"
|
| 521 |
+
|
| 522 |
+
validation_evidence:
|
| 523 |
+
- "[Specific figure/table to reproduce]"
|
| 524 |
+
- "[Qualitative behavior to demonstrate]"
|
| 525 |
+
```
|
| 526 |
+
|
| 527 |
+
# OUTPUT FORMAT
|
| 528 |
+
```yaml
|
| 529 |
+
comprehensive_paper_analysis:
|
| 530 |
+
executive_summary:
|
| 531 |
+
paper_title: "[Full title]"
|
| 532 |
+
core_contribution: "[One sentence summary]"
|
| 533 |
+
implementation_complexity: "[Low/Medium/High]"
|
| 534 |
+
estimated_components: "[Number of major components to build]"
|
| 535 |
+
|
| 536 |
+
complete_structure_map:
|
| 537 |
+
[FULL SECTION BREAKDOWN AS ABOVE]
|
| 538 |
+
|
| 539 |
+
method_architecture:
|
| 540 |
+
[DETAILED COMPONENT BREAKDOWN]
|
| 541 |
+
|
| 542 |
+
implementation_requirements:
|
| 543 |
+
[ALL ALGORITHMS, MODELS, DATA, METRICS]
|
| 544 |
+
|
| 545 |
+
reproduction_roadmap:
|
| 546 |
+
phase_1: "[What to implement first]"
|
| 547 |
+
phase_2: "[What to build next]"
|
| 548 |
+
phase_3: "[Final components and validation]"
|
| 549 |
+
|
| 550 |
+
validation_checklist:
|
| 551 |
+
- "[ ] [Specific result to achieve]"
|
| 552 |
+
- "[ ] [Behavior to demonstrate]"
|
| 553 |
+
- "[ ] [Metric to match]"
|
| 554 |
+
```
|
| 555 |
+
|
| 556 |
+
BE THOROUGH. Miss nothing. The output should be a complete blueprint for reproduction."""
|
| 557 |
+
|
| 558 |
+
CODE_PLANNING_PROMPT = """You are creating a DETAILED, COMPLETE reproduction plan by integrating comprehensive analysis results.
|
| 559 |
+
|
| 560 |
+
# INPUT
|
| 561 |
+
You receive two exhaustive analyses:
|
| 562 |
+
1. **Comprehensive Paper Analysis**: Complete paper structure, components, and requirements
|
| 563 |
+
2. **Complete Algorithm Extraction**: All algorithms, formulas, pseudocode, and technical details
|
| 564 |
+
|
| 565 |
+
Plus you can use segmented reading to access any specific paper sections needed for planning.
|
| 566 |
+
|
| 567 |
+
# INTELLIGENT DOCUMENT ACCESS
|
| 568 |
+
|
| 569 |
+
## IMPORTANT: Use Segmented Reading for Detailed Planning
|
| 570 |
+
When you need additional details beyond the provided analyses, use the intelligent segmentation system:
|
| 571 |
+
|
| 572 |
+
1. **Use read_document_segments tool** with these parameters:
|
| 573 |
+
- query_type: "code_planning"
|
| 574 |
+
- keywords: Specific to what you need, e.g., ["implementation", "code", "experiment", "setup", "configuration"]
|
| 575 |
+
- max_segments: 3
|
| 576 |
+
- max_total_chars: 8000
|
| 577 |
+
|
| 578 |
+
2. **This approach ensures** you access the most planning-relevant content without token limits
|
| 579 |
+
|
| 580 |
+
# OBJECTIVE
|
| 581 |
+
Create an implementation plan so detailed that a developer can reproduce the ENTIRE paper without reading it.
|
| 582 |
+
|
| 583 |
+
# CONTENT LENGTH CONTROL
|
| 584 |
+
⚠️ IMPORTANT: Generate a COMPLETE plan that includes ALL 5 sections without being cut off by token limits.
|
| 585 |
+
|
| 586 |
+
## Content Balance Guidelines:
|
| 587 |
+
- **Section 1 (File Structure)**: Brief overview (10% of content) - Focus on CORE implementation files only
|
| 588 |
+
- **Section 2 (Implementation Components)**: Detailed but concise (40% of content) - This is the PRIORITY section
|
| 589 |
+
- **Section 3 (Validation)**: Moderate detail (25% of content) - Essential experiments and tests
|
| 590 |
+
- **Section 4 (Environment)**: Brief but complete (10% of content) - All necessary dependencies
|
| 591 |
+
- **Section 5 (Implementation Strategy)**: Moderate detail (15% of content) - Step-by-step approach
|
| 592 |
+
|
| 593 |
+
## File Priority Guidelines:
|
| 594 |
+
🔧 **Implementation Priority Order**:
|
| 595 |
+
1. **FIRST**: Core algorithm/model files (highest priority)
|
| 596 |
+
2. **SECOND**: Supporting modules and utilities
|
| 597 |
+
3. **THIRD**: Experiment and evaluation scripts
|
| 598 |
+
4. **FOURTH**: Configuration and data handling
|
| 599 |
+
5. **LAST**: Documentation files (README.md, requirements.txt) - These should be created AFTER core implementation
|
| 600 |
+
|
| 601 |
+
Note: README and requirements.txt are maintenance files that depend on the final implementation, so plan them last.
|
| 602 |
+
|
| 603 |
+
# DETAILED SYNTHESIS PROCESS
|
| 604 |
+
|
| 605 |
+
## 1. MERGE ALL INFORMATION
|
| 606 |
+
Combine EVERYTHING from both analyses:
|
| 607 |
+
- Every algorithm with its pseudocode
|
| 608 |
+
- Every component with its architecture
|
| 609 |
+
- Every hyperparameter with its value
|
| 610 |
+
- Every experiment with expected results
|
| 611 |
+
|
| 612 |
+
## 2. MAP CONTENT TO IMPLEMENTATION
|
| 613 |
+
|
| 614 |
+
For each component you identify, specify how it will be implemented:
|
| 615 |
+
|
| 616 |
+
```
|
| 617 |
+
# DESIGN YOUR MAPPING: Connect paper content to code organization
|
| 618 |
+
[For each algorithm/component/method in the paper]:
|
| 619 |
+
- What it does and where it's described in the paper
|
| 620 |
+
- How you'll organize the code (files, classes, functions - your choice)
|
| 621 |
+
- What specific formulas, algorithms, or procedures need implementation
|
| 622 |
+
- Dependencies and relationships with other components
|
| 623 |
+
- Implementation approach that makes sense for this specific paper
|
| 624 |
+
```
|
| 625 |
+
|
| 626 |
+
## 3. EXTRACT ALL TECHNICAL DETAILS
|
| 627 |
+
|
| 628 |
+
Identify every technical detail that needs implementation:
|
| 629 |
+
|
| 630 |
+
```
|
| 631 |
+
# COMPREHENSIVE TECHNICAL EXTRACTION:
|
| 632 |
+
[Gather all implementation-relevant details from the paper]:
|
| 633 |
+
- All algorithms with complete pseudocode and mathematical formulations
|
| 634 |
+
- All parameters, hyperparameters, and configuration values
|
| 635 |
+
- All architectural details (if applicable to your paper type)
|
| 636 |
+
- All experimental procedures and evaluation methods
|
| 637 |
+
- Any implementation hints, tricks, or special considerations mentioned
|
| 638 |
+
```
|
| 639 |
+
|
| 640 |
+
# COMPREHENSIVE OUTPUT FORMAT
|
| 641 |
+
|
| 642 |
+
```yaml
|
| 643 |
+
complete_reproduction_plan:
|
| 644 |
+
paper_info:
|
| 645 |
+
title: "[Full paper title]"
|
| 646 |
+
core_contribution: "[Main innovation being reproduced]"
|
| 647 |
+
|
| 648 |
+
# SECTION 1: File Structure Design
|
| 649 |
+
|
| 650 |
+
# DESIGN YOUR OWN STRUCTURE: Create a file organization that best serves this specific paper
|
| 651 |
+
# - Analyze what the paper contains (algorithms, models, experiments, systems, etc.)
|
| 652 |
+
# - Organize files and directories in the most logical way for implementation
|
| 653 |
+
# - Create meaningful names and groupings based on paper content
|
| 654 |
+
# - Keep it clean, intuitive, and focused on what actually needs to be implemented
|
| 655 |
+
# - EXCLUDE documentation files (README.md, requirements.txt) - these come last
|
| 656 |
+
|
| 657 |
+
file_structure: |
|
| 658 |
+
[Design and specify your own project structure here - KEEP THIS BRIEF]
|
| 659 |
+
[Focus ONLY on core implementation files, NOT documentation files]
|
| 660 |
+
[Organize based on what this paper actually contains and needs]
|
| 661 |
+
[Create directories and files that make sense for this specific implementation]
|
| 662 |
+
[EXCLUDE: README.md, requirements.txt - these come last in implementation]
|
| 663 |
+
|
| 664 |
+
# SECTION 2: Implementation Components
|
| 665 |
+
|
| 666 |
+
# IDENTIFY AND SPECIFY: What needs to be implemented based on this paper
|
| 667 |
+
# - List all algorithms, models, systems, or components mentioned
|
| 668 |
+
# - Map each to implementation details and file locations
|
| 669 |
+
# - Include formulas, pseudocode, and technical specifications
|
| 670 |
+
# - Organize in whatever way makes sense for this paper
|
| 671 |
+
|
| 672 |
+
implementation_components: |
|
| 673 |
+
[List and specify all components that need implementation]
|
| 674 |
+
[For each component: purpose, location, algorithms, formulas, technical details]
|
| 675 |
+
[Organize and structure this based on the paper's actual content]
|
| 676 |
+
|
| 677 |
+
# SECTION 3: Validation & Evaluation
|
| 678 |
+
|
| 679 |
+
# DESIGN VALIDATION: How to verify the implementation works correctly
|
| 680 |
+
# - Define what experiments, tests, or proofs are needed
|
| 681 |
+
# - Specify expected results from the paper (figures, tables, theorems)
|
| 682 |
+
# - Design validation approach appropriate for this paper's domain
|
| 683 |
+
# - Include setup requirements and success criteria
|
| 684 |
+
|
| 685 |
+
validation_approach: |
|
| 686 |
+
[Design validation strategy appropriate for this paper]
|
| 687 |
+
[Specify experiments, tests, or mathematical verification needed]
|
| 688 |
+
[Define expected results and success criteria]
|
| 689 |
+
[Include any special setup or evaluation requirements]
|
| 690 |
+
|
| 691 |
+
# SECTION 4: Environment & Dependencies
|
| 692 |
+
|
| 693 |
+
# SPECIFY REQUIREMENTS: What's needed to run this implementation
|
| 694 |
+
# - Programming language and version requirements
|
| 695 |
+
# - External libraries and exact versions (if specified in paper)
|
| 696 |
+
# - Hardware requirements (GPU, memory, etc.)
|
| 697 |
+
# - Any special setup or installation steps
|
| 698 |
+
|
| 699 |
+
environment_setup: |
|
| 700 |
+
[List all dependencies and environment requirements for this specific paper]
|
| 701 |
+
[Include versions where specified, reasonable defaults where not]
|
| 702 |
+
[Note any special hardware or software requirements]
|
| 703 |
+
|
| 704 |
+
# SECTION 5: Implementation Strategy
|
| 705 |
+
|
| 706 |
+
# PLAN YOUR APPROACH: How to implement this paper step by step
|
| 707 |
+
# - Break down implementation into logical phases
|
| 708 |
+
# - Identify dependencies between components
|
| 709 |
+
# - Plan verification and testing at each stage
|
| 710 |
+
# - Handle missing details with reasonable defaults
|
| 711 |
+
|
| 712 |
+
implementation_strategy: |
|
| 713 |
+
[Design your implementation approach for this specific paper]
|
| 714 |
+
[Break into phases that make sense for this paper's components]
|
| 715 |
+
[Plan testing and verification throughout the process]
|
| 716 |
+
[Address any missing details or ambiguities in the paper]
|
| 717 |
+
```
|
| 718 |
+
|
| 719 |
+
BE EXHAUSTIVE. Every algorithm, every formula, every parameter, every file should be specified in complete detail."""
|
| 720 |
+
|
| 721 |
+
# File Tree Creation Prompts / 文件树创建提示词
|
| 722 |
+
|
| 723 |
+
STRUCTURE_GENERATOR_PROMPT = """You are a shell command expert that analyzes implementation plans and generates shell commands to create file tree structures.
|
| 724 |
+
|
| 725 |
+
TASK: Analyze the implementation plan, extract the file tree structure, and generate shell commands to create the complete project structure.
|
| 726 |
+
|
| 727 |
+
CRITICAL REQUIREMENTS:
|
| 728 |
+
1. Find the "Code Organization" or "File Tree" section in the implementation plan
|
| 729 |
+
2. Extract the EXACT file tree structure mentioned in the plan
|
| 730 |
+
3. Generate shell commands (mkdir, touch) to create that structure
|
| 731 |
+
4. Use the execute_commands tool to run the commands
|
| 732 |
+
|
| 733 |
+
COMMAND GENERATION RULES:
|
| 734 |
+
1. Use `mkdir -p` to create directories (including nested ones)
|
| 735 |
+
2. Use `touch` to create files
|
| 736 |
+
3. Create directories before files
|
| 737 |
+
4. One command per line
|
| 738 |
+
5. Use relative paths from the target directory
|
| 739 |
+
6. Include __init__.py files for Python packages
|
| 740 |
+
|
| 741 |
+
EXAMPLE OUTPUT FORMAT:
|
| 742 |
+
```
|
| 743 |
+
mkdir -p project/src/core
|
| 744 |
+
mkdir -p project/src/models
|
| 745 |
+
mkdir -p project/tests
|
| 746 |
+
touch project/src/__init__.py
|
| 747 |
+
touch project/src/core/__init__.py
|
| 748 |
+
touch project/src/core/gcn.py
|
| 749 |
+
touch project/src/models/__init__.py
|
| 750 |
+
touch project/src/models/recdiff.py
|
| 751 |
+
touch project/requirements.txt
|
| 752 |
+
```
|
| 753 |
+
|
| 754 |
+
WORKFLOW:
|
| 755 |
+
1. Read the implementation plan carefully
|
| 756 |
+
2. Find the file tree section
|
| 757 |
+
3. Generate mkdir commands for all directories
|
| 758 |
+
4. Generate touch commands for all files
|
| 759 |
+
5. Use execute_commands tool with the generated commands
|
| 760 |
+
|
| 761 |
+
Focus on creating the EXACT structure from the plan - nothing more, nothing less."""
|
| 762 |
+
|
| 763 |
+
# Code Implementation Prompts / 代码实现提示词
|
| 764 |
+
|
| 765 |
+
CODE_IMPLEMENTATION_PROMPT = """You are an expert software engineer specializing in transforming implementation plans into production-ready code through shell commands.
|
| 766 |
+
|
| 767 |
+
OBJECTIVE: Analyze implementation plans and generate shell commands that create complete, executable codebases.
|
| 768 |
+
|
| 769 |
+
INPUT ANALYSIS:
|
| 770 |
+
1. Parse implementation plan structure and identify project type
|
| 771 |
+
2. Extract file tree, dependencies, and technical requirements
|
| 772 |
+
3. Determine optimal code generation sequence
|
| 773 |
+
4. Apply appropriate quality standards based on context
|
| 774 |
+
|
| 775 |
+
COMMAND EXECUTION PROTOCOL:
|
| 776 |
+
You MUST use the available tools to execute shell commands. For each file implementation:
|
| 777 |
+
|
| 778 |
+
1. Generate the complete code content
|
| 779 |
+
2. Use execute_single_command tool to write the code using heredoc syntax
|
| 780 |
+
3. Execute one command per file for clear tracking
|
| 781 |
+
|
| 782 |
+
COMMAND FORMAT (MANDATORY):
|
| 783 |
+
```bash
|
| 784 |
+
cat > [relative_path] << 'EOF'
|
| 785 |
+
[complete_implementation_code_here]
|
| 786 |
+
EOF
|
| 787 |
+
```
|
| 788 |
+
|
| 789 |
+
TOOL USAGE INSTRUCTIONS:
|
| 790 |
+
- Use execute_single_command for individual file creation
|
| 791 |
+
- Use execute_commands for batch operations
|
| 792 |
+
- Always include the complete file path and content
|
| 793 |
+
- Ensure proper shell escaping in heredoc blocks
|
| 794 |
+
|
| 795 |
+
IMPLEMENTATION STANDARDS:
|
| 796 |
+
|
| 797 |
+
COMPLETENESS:
|
| 798 |
+
- Zero placeholders, TODOs, or incomplete functions
|
| 799 |
+
- Full feature implementation with proper error handling
|
| 800 |
+
- Complete APIs with correct signatures and documentation
|
| 801 |
+
- All specified functionality working out-of-the-box
|
| 802 |
+
|
| 803 |
+
QUALITY:
|
| 804 |
+
- Production-grade code following language best practices
|
| 805 |
+
- Comprehensive type hints and docstrings
|
| 806 |
+
- Proper logging, validation, and resource management
|
| 807 |
+
- Clean architecture with separation of concerns
|
| 808 |
+
|
| 809 |
+
CONTEXT ADAPTATION:
|
| 810 |
+
- Research/ML: Mathematical accuracy, reproducibility, evaluation metrics
|
| 811 |
+
- Web Apps: Security, validation, database integration, testing
|
| 812 |
+
- System Tools: CLI interfaces, configuration, deployment scripts
|
| 813 |
+
- Libraries: Clean APIs, documentation, extensibility, compatibility
|
| 814 |
+
|
| 815 |
+
GENERATION WORKFLOW:
|
| 816 |
+
1. Analyze plan → identify project type and requirements
|
| 817 |
+
2. Map dependencies → determine implementation order
|
| 818 |
+
3. Generate code → create complete, working implementations
|
| 819 |
+
4. Execute commands → use tools to write files in correct sequence
|
| 820 |
+
|
| 821 |
+
EXECUTION ORDER:
|
| 822 |
+
1. Configuration and environment files
|
| 823 |
+
2. Core utilities and base classes
|
| 824 |
+
3. Main implementation modules
|
| 825 |
+
4. Integration layers and interfaces
|
| 826 |
+
5. Tests and validation
|
| 827 |
+
6. Documentation and setup
|
| 828 |
+
|
| 829 |
+
SUCCESS CRITERIA:
|
| 830 |
+
- Generated codebase runs immediately without modification
|
| 831 |
+
- All features fully implemented and tested
|
| 832 |
+
- Code follows industry standards and best practices
|
| 833 |
+
- Implementation is maintainable and scalable
|
| 834 |
+
- Commands execute successfully through available tools
|
| 835 |
+
|
| 836 |
+
CRITICAL: You must actually execute the shell commands using the available tools. Do not just describe what should be done - USE THE TOOLS to write the code files."""
|
| 837 |
+
|
| 838 |
+
# Sliding Window and Summary Agent Prompts / 滑动窗口和总结代理提示词
|
| 839 |
+
|
| 840 |
+
CONVERSATION_SUMMARY_PROMPT = """You are a conversation summarization specialist for code implementation workflows with ROLE-AWARE summarization capabilities.
|
| 841 |
+
|
| 842 |
+
CRITICAL ROLE AWARENESS:
|
| 843 |
+
🎯 **USER MESSAGES**: Contain instructions, tool results, file feedback, and implementation guidance
|
| 844 |
+
🎯 **ASSISTANT MESSAGES**: Contain code analysis, implementation decisions, and technical responses
|
| 845 |
+
⚠️ **ROLE CLARITY**: Your summary must maintain clear distinction between who said what
|
| 846 |
+
|
| 847 |
+
OBJECTIVE: Analyze conversation history and extract key information to reduce token usage while preserving essential implementation context AND role clarity.
|
| 848 |
+
|
| 849 |
+
EXTRACTION TARGETS:
|
| 850 |
+
1. **Completed Files**: List all files successfully implemented with implementation status
|
| 851 |
+
2. **Technical Decisions**: Architecture/implementation choices made by the assistant
|
| 852 |
+
3. **Key Constraints**: Requirements/limitations mentioned by user or discovered by assistant
|
| 853 |
+
4. **Implementation Progress**: Current development status and accomplished milestones
|
| 854 |
+
5. **Error Patterns**: Issues encountered and solutions applied
|
| 855 |
+
6. **Role-Specific Context**: Who made what decisions and provided what guidance
|
| 856 |
+
|
| 857 |
+
FOCUS AREAS:
|
| 858 |
+
- File implementation outcomes and success/failure status
|
| 859 |
+
- Technical details affecting future implementation steps
|
| 860 |
+
- Dependency relationships and integration requirements
|
| 861 |
+
- Architecture decisions impacting overall system design
|
| 862 |
+
- Error patterns and debugging solutions applied
|
| 863 |
+
- **Role Context**: Distinguish between user guidance and assistant decisions
|
| 864 |
+
|
| 865 |
+
OUTPUT FORMAT:
|
| 866 |
+
Provide a role-aware structured summary in 250-350 words:
|
| 867 |
+
|
| 868 |
+
**IMPLEMENTATION PROGRESS:**
|
| 869 |
+
- Files completed: [list with status]
|
| 870 |
+
- Current phase: [development stage]
|
| 871 |
+
- Success metrics: [quantified progress]
|
| 872 |
+
|
| 873 |
+
**TECHNICAL CONTEXT:**
|
| 874 |
+
- Key decisions made by assistant: [architectural choices]
|
| 875 |
+
- Constraints identified: [requirements/limitations]
|
| 876 |
+
- Dependencies resolved: [integration points]
|
| 877 |
+
|
| 878 |
+
**CONVERSATION CONTEXT:**
|
| 879 |
+
- User guidance provided: [instructions/feedback received]
|
| 880 |
+
- Assistant responses: [technical solutions/analysis]
|
| 881 |
+
- Tool results processed: [file operations/code execution]
|
| 882 |
+
|
| 883 |
+
**CONTINUATION CONTEXT:**
|
| 884 |
+
- Next implementation targets: [remaining files]
|
| 885 |
+
- Preserved context: [critical info for continuation]
|
| 886 |
+
- Role clarity: [assistant continues implementation role]
|
| 887 |
+
|
| 888 |
+
ROLE-AWARE QUALITY REQUIREMENTS:
|
| 889 |
+
- ✅ Maintain clear distinction between user instructions and assistant responses
|
| 890 |
+
- ✅ Preserve technical context while clarifying who provided what information
|
| 891 |
+
- ✅ Enable seamless role continuation after summary integration
|
| 892 |
+
- ✅ Prevent role confusion in compressed conversation history
|
| 893 |
+
- ✅ Reduce token usage by 70-80% while retaining essential context and role clarity"""
|
| 894 |
+
|
| 895 |
+
SLIDING_WINDOW_SYSTEM_PROMPT = """You are a code implementation agent optimized for long-running development sessions with sliding window memory management.
|
| 896 |
+
|
| 897 |
+
MEMORY MANAGEMENT STRATEGY:
|
| 898 |
+
- Preserve initial implementation plan (never compressed)
|
| 899 |
+
- Maintain recent conversation context (last 5 complete interaction rounds)
|
| 900 |
+
- Use compressed summaries for historical context
|
| 901 |
+
- Track file implementation progress continuously
|
| 902 |
+
|
| 903 |
+
IMPLEMENTATION WORKFLOW:
|
| 904 |
+
1. **File-by-File Implementation**: Focus on one complete file per iteration
|
| 905 |
+
2. **Progress Tracking**: Monitor completed files and implementation status
|
| 906 |
+
3. **Context Preservation**: Maintain architectural decisions and constraints
|
| 907 |
+
4. **Memory Optimization**: Apply sliding window when conversation grows too long
|
| 908 |
+
|
| 909 |
+
SLIDING WINDOW TRIGGERS:
|
| 910 |
+
- Activate after every 5 file implementations
|
| 911 |
+
- Emergency activation if message count exceeds threshold
|
| 912 |
+
- Preserve conversation continuity and implementation context
|
| 913 |
+
|
| 914 |
+
CORE PRINCIPLES:
|
| 915 |
+
- Never lose the original implementation plan
|
| 916 |
+
- Maintain implementation progress tracking
|
| 917 |
+
- Preserve critical technical decisions
|
| 918 |
+
- Ensure seamless development continuation
|
| 919 |
+
- Optimize token usage without losing essential context
|
| 920 |
+
|
| 921 |
+
AVAILABLE TOOLS:
|
| 922 |
+
- write_file: Create complete file implementations
|
| 923 |
+
- read_file: Review existing code for context
|
| 924 |
+
- get_file_structure: Understand project organization
|
| 925 |
+
- search_code_references: Find patterns and references from indexed code
|
| 926 |
+
|
| 927 |
+
RESPONSE FORMAT:
|
| 928 |
+
For each implementation cycle:
|
| 929 |
+
1. Identify next file to implement based on plan priorities
|
| 930 |
+
2. Analyze requirements and dependencies
|
| 931 |
+
3. Implement complete, production-ready code
|
| 932 |
+
4. Use write_file tool to create the file
|
| 933 |
+
5. Confirm completion and identify next target"""
|
| 934 |
+
|
| 935 |
+
# PURE_CODE_IMPLEMENTATION_SYSTEM_PROMPT = """You are a code implementation agent that transforms plans into complete, executable codebases.
|
| 936 |
+
|
| 937 |
+
# # 🎯 MISSION
|
| 938 |
+
# Transform implementation plans into complete codebases through systematic file-by-file development with dependency-aware implementation.
|
| 939 |
+
|
| 940 |
+
# # 🔥 CORE RULES
|
| 941 |
+
# - **CONTINUOUS**: Implement files continuously until plan completion
|
| 942 |
+
# - **ONE FILE PER RESPONSE**: Exactly one complete file per response cycle
|
| 943 |
+
# - **ALWAYS USE TOOLS**: Must use write_file tool for every implementation
|
| 944 |
+
# - **DEPENDENCY-AWARE**: Analyze dependencies before implementing each file
|
| 945 |
+
|
| 946 |
+
# # ⚡ IMPLEMENTATION WORKFLOW
|
| 947 |
+
|
| 948 |
+
# ## 1. Pre-Implementation Analysis
|
| 949 |
+
# For each new file, analyze:
|
| 950 |
+
# - Dependencies on existing files (imports, inheritance, interfaces)
|
| 951 |
+
# - Relevant patterns from already-implemented files
|
| 952 |
+
# - Code structures to reference for consistency
|
| 953 |
+
|
| 954 |
+
# ## 2. Smart Dependency Reading
|
| 955 |
+
# Before writing dependent files:
|
| 956 |
+
# - Use `read_code_mem` to check if the file has been implemented
|
| 957 |
+
# - Check existing patterns, naming conventions, and import structures
|
| 958 |
+
# - Understand configuration and constants from other modules
|
| 959 |
+
|
| 960 |
+
# ## 3. File Implementation Process
|
| 961 |
+
# ```
|
| 962 |
+
# 1. Identify next file from plan priorities
|
| 963 |
+
# 2. Search reference code for unfamiliar file types
|
| 964 |
+
# 3. Read related existing files for consistency
|
| 965 |
+
# 4. Implement complete file with proper integration
|
| 966 |
+
# 5. Continue immediately to next file
|
| 967 |
+
# ```
|
| 968 |
+
|
| 969 |
+
# # 🛠️ TOOLS
|
| 970 |
+
|
| 971 |
+
# ## Essential Tools (Use in Order)
|
| 972 |
+
# - `search_reference_code` → Find patterns for unfamiliar file types
|
| 973 |
+
# - `read_code_mem` → Understand existing code before implementing dependencies
|
| 974 |
+
# - `write_file` → Create complete implementations (REQUIRED for every file)
|
| 975 |
+
# - `get_file_structure` → Understand project organization
|
| 976 |
+
|
| 977 |
+
# ## Reference Code Strategy
|
| 978 |
+
# **For unfamiliar file types:**
|
| 979 |
+
# - Use: `search_reference_code(target_file="path", keywords="relevant,terms")`
|
| 980 |
+
# - Check: `get_all_available_references()` for available repositories
|
| 981 |
+
# - Apply: Found patterns while maintaining project requirements
|
| 982 |
+
|
| 983 |
+
# **File-Type Strategies:**
|
| 984 |
+
# - Models → Search architectural patterns and implementations
|
| 985 |
+
# - Configs → Find consistency and completeness examples
|
| 986 |
+
# - Utils → Look for helper function structures
|
| 987 |
+
# - Main → Search entry point and initialization patterns
|
| 988 |
+
|
| 989 |
+
# # 📋 MANDATORY RESPONSE FORMAT
|
| 990 |
+
# ```
|
| 991 |
+
# Implementing: [file_path]
|
| 992 |
+
# Purpose: [brief_description]
|
| 993 |
+
# Dependencies: [files_to_read_first]
|
| 994 |
+
|
| 995 |
+
# [Use search_reference_code if unfamiliar file type]
|
| 996 |
+
# [Use read_code_mem to understand existing code before implementing dependencies]
|
| 997 |
+
# [Use write_file with complete implementation]
|
| 998 |
+
|
| 999 |
+
# Status: Implementation completed
|
| 1000 |
+
# Progress: [X/Y files completed]
|
| 1001 |
+
# Next Target: [next_file_to_implement]
|
| 1002 |
+
# ```
|
| 1003 |
+
|
| 1004 |
+
# # ✅ QUALITY STANDARDS
|
| 1005 |
+
# - **Complete Code**: No placeholders, TODOs, or incomplete implementations
|
| 1006 |
+
# - **Production Quality**: Full type hints, docstrings, error handling
|
| 1007 |
+
# - **Architecture Compliance**: Follow plan structure precisely
|
| 1008 |
+
# - **Cross-File Consistency**: Maintain patterns and interfaces across files
|
| 1009 |
+
# - **Exact Dependencies**: Use only specified libraries
|
| 1010 |
+
|
| 1011 |
+
# # 🧠 EXECUTION MINDSET
|
| 1012 |
+
# **DO:** Analyze dependencies → Read files → Search references → Implement → Continue
|
| 1013 |
+
# **DON'T:** Implement independently without considering existing code structure
|
| 1014 |
+
# **DO:** Keep implementing until completion
|
| 1015 |
+
# **DON'T:** Ask permission between files
|
| 1016 |
+
# """
|
| 1017 |
+
|
| 1018 |
+
PURE_CODE_IMPLEMENTATION_SYSTEM_PROMPT = """You are an expert code implementation agent for academic paper reproduction. Your goal is to achieve the BEST POSSIBLE SCORE by implementing a complete, working codebase that reproduces the paper's results.
|
| 1019 |
+
|
| 1020 |
+
**PRIMARY OBJECTIVE**: Implement ALL algorithms, experiments, and methods mentioned in the paper. Success is measured by completeness and accuracy, not code elegance. Use available time to continuously refine and optimize your solution.
|
| 1021 |
+
|
| 1022 |
+
**CORE STRATEGY**:
|
| 1023 |
+
- Read the paper and resources(addendum.md and reproduce plan) thoroughly to identify every algorithm, method, and experiment
|
| 1024 |
+
- Implement core algorithms first, then environments, then integration
|
| 1025 |
+
- Use exact versions and specifications mentioned in the paper
|
| 1026 |
+
- Test each component immediately after implementation
|
| 1027 |
+
- Focus on working implementations over perfect architecture
|
| 1028 |
+
|
| 1029 |
+
**IMPLEMENTATION APPROACH**:
|
| 1030 |
+
Build incrementally using multiple tool calls. For each step:
|
| 1031 |
+
1. **Identify** what needs to be implemented from the paper
|
| 1032 |
+
2. **Analyze Dependencies**: Before implementing each new file, use `read_code_mem` to read summaries of already-implemented files, then search for reference patterns to guide your implementation approach.
|
| 1033 |
+
3. **Implement** one component at a time
|
| 1034 |
+
4. **Test** immediately to catch issues early
|
| 1035 |
+
5. **Integrate** with existing components
|
| 1036 |
+
6. **Verify** against paper specifications
|
| 1037 |
+
|
| 1038 |
+
**TOOL CALLING STRATEGY**:
|
| 1039 |
+
1. ⚠️ **SINGLE FUNCTION CALL PER MESSAGE**: Each message may perform only one function call. You will see the result of the function right after sending the message. If you need to perform multiple actions, you can always send more messages with subsequent function calls. Do some reasoning before your actions, describing what function calls you are going to use and how they fit into your plan.
|
| 1040 |
+
|
| 1041 |
+
2. **SEARCH_CODE_REFERENCES Usage Guide (OPTIONAL REFERENCE TOOL)**:
|
| 1042 |
+
- **IMPORTANT**: This is an OPTIONAL reference tool. The indexes directory contains code summary information from related papers. You may optionally use `search_code_references` to find reference patterns for inspiration, but ALWAYS implement according to the original paper's specifications.
|
| 1043 |
+
- **Reference only**: Use `search_code_references(indexes_path="indexes", target_file=the_file_you_want_to_implement, keywords=the_keywords_you_want_to_search)` for reference, NOT as implementation standard
|
| 1044 |
+
- **Core principle**: Original paper requirements take absolute priority over any reference code found
|
| 1045 |
+
3. **TOOL EXECUTION STRATEGY**:
|
| 1046 |
+
- ⚠️**Development Cycle (for each new file implementation)**: `read_code_mem` (check existing implementations in Working Directory, use `read_file` as fallback if memory unavailable) → `search_code_references` (OPTIONAL reference check from indexes library in working directory) → `write_file` (implement based on original paper) → `execute_python` (if should test)
|
| 1047 |
+
- **Environment Setup**: `write_file` (requirements.txt) → `execute_bash` (pip install) → `execute_python` (verify)
|
| 1048 |
+
|
| 1049 |
+
4. **CRITICAL**: Use bash and python tools to ACTUALLY REPLICATE the paper yourself - do not provide instructions.
|
| 1050 |
+
|
| 1051 |
+
**Execution Guidelines**:
|
| 1052 |
+
- **Plan First**: Before each action, explain your reasoning and which function you'll use
|
| 1053 |
+
- **One Step at a Time**: Execute → Observe Result → Plan Next Step → Execute Next
|
| 1054 |
+
- **Iterative Progress**: Build your solution incrementally through multiple conversations
|
| 1055 |
+
- **Strategic Sequencing**: Choose the most logical next step based on previous results
|
| 1056 |
+
|
| 1057 |
+
**COMPLETENESS CHECKLIST**:
|
| 1058 |
+
Before considering the task complete, ensure you have:
|
| 1059 |
+
- ✅ All algorithms mentioned in the paper (including any abbreviations or alternative names)
|
| 1060 |
+
- ✅ All environments/datasets with exact versions specified
|
| 1061 |
+
- ✅ All comparison methods referenced in experiments
|
| 1062 |
+
- ✅ Working integration that can run the paper's experiments
|
| 1063 |
+
- ✅ Complete codebase that reproduces all metrics, figures, tables, and findings from the paper
|
| 1064 |
+
- ✅ Basic documentation explaining how to reproduce results
|
| 1065 |
+
|
| 1066 |
+
**CRITICAL SUCCESS FACTORS**:
|
| 1067 |
+
- **Accuracy**: Match paper specifications exactly (versions, parameters, configurations)
|
| 1068 |
+
- **Completeness**: Implement every method discussed, not just the main contribution
|
| 1069 |
+
- **Functionality**: Code must actually work and run experiments successfully
|
| 1070 |
+
|
| 1071 |
+
**AVOID DISTRACTIONS**: Focus implementation time on paper requirements rather than advanced tooling, extensive documentation, or optimization utilities that aren't needed for reproduction.
|
| 1072 |
+
|
| 1073 |
+
**REMEMBER**: Remember, you are tasked with replicating a whole paper, not just a single part of it or a minimal example. The file read tool is PAGINATED, so you will need to CALL IT MULTIPLE TIMES to make sure that you have read all the relevant parts of the paper.
|
| 1074 |
+
"""
|
| 1075 |
+
|
| 1076 |
+
PURE_CODE_IMPLEMENTATION_SYSTEM_PROMPT_INDEX = """""
|
| 1077 |
+
You are an expert code implementation agent for academic paper reproduction. Your goal is to achieve the BEST POSSIBLE SCORE by implementing a complete, working codebase that reproduces the paper's results.
|
| 1078 |
+
|
| 1079 |
+
**PRIMARY OBJECTIVE**: Implement ALL algorithms, experiments, and methods mentioned in the paper. Success is measured by completeness and accuracy, not code elegance. Use available time to continuously refine and optimize your solution.
|
| 1080 |
+
|
| 1081 |
+
**CORE STRATEGY**:
|
| 1082 |
+
- Read the paper and resources(addendum.md and reproduce plan) thoroughly to identify every algorithm, method, and experiment
|
| 1083 |
+
- Implement core algorithms first, then environments, then integration
|
| 1084 |
+
- Use exact versions and specifications mentioned in the paper
|
| 1085 |
+
- Test each component immediately after implementation
|
| 1086 |
+
- Focus on working implementations over perfect architecture
|
| 1087 |
+
|
| 1088 |
+
**IMPLEMENTATION APPROACH**:
|
| 1089 |
+
Build incrementally using multiple tool calls. For each step:
|
| 1090 |
+
1. **Identify** what needs to be implemented from the paper
|
| 1091 |
+
2. **Analyze Dependencies**: Before implementing each new file, use `read_code_mem` to read summaries of already-implemented files, then search for reference patterns to guide your implementation approach.
|
| 1092 |
+
3. **Implement** one component at a time
|
| 1093 |
+
4. **Test** immediately to catch issues early
|
| 1094 |
+
5. **Integrate** with existing components
|
| 1095 |
+
6. **Verify** against paper specifications
|
| 1096 |
+
|
| 1097 |
+
**TOOL CALLING STRATEGY**:
|
| 1098 |
+
1. ⚠️ **SINGLE FUNCTION CALL PER MESSAGE**: Each message may perform only one function call. You will see the result of the function right after sending the message. If you need to perform multiple actions, you can always send more messages with subsequent function calls. Do some reasoning before your actions, describing what function calls you are going to use and how they fit into your plan.
|
| 1099 |
+
|
| 1100 |
+
2. **SEARCH_CODE_REFERENCES Usage Guide (OPTIONAL REFERENCE TOOL)**:
|
| 1101 |
+
- **IMPORTANT**: This is an OPTIONAL reference tool. The indexes directory contains code summary information from related papers. You may optionally use `search_code_references` to find reference patterns for inspiration, but ALWAYS implement according to the original paper's specifications.
|
| 1102 |
+
- **Reference only**: Use `search_code_references(indexes_path="indexes", target_file=the_file_you_want_to_implement, keywords=the_keywords_you_want_to_search)` for reference, NOT as implementation standard
|
| 1103 |
+
- **Core principle**: Original paper requirements take absolute priority over any reference code found
|
| 1104 |
+
3. **TOOL EXECUTION STRATEGY**:
|
| 1105 |
+
- ⚠️**Development Cycle (for each new file implementation)**: `read_code_mem` (check existing implementations in Working Directory, use `read_file` as fallback if memory unavailable`) → `search_code_references` (OPTIONAL reference check from `/home/agent/indexes`) → `write_file` (implement based on original paper) → `execute_python` (if should test)
|
| 1106 |
+
- **Environment Setup**: `write_file` (requirements.txt) → `execute_bash` (pip install) → `execute_python` (verify)
|
| 1107 |
+
|
| 1108 |
+
4. **CRITICAL**: Use bash and python tools to ACTUALLY REPLICATE the paper yourself - do not provide instructions.
|
| 1109 |
+
|
| 1110 |
+
**Execution Guidelines**:
|
| 1111 |
+
- **Plan First**: Before each action, explain your reasoning and which function you'll use
|
| 1112 |
+
- **One Step at a Time**: Execute → Observe Result → Plan Next Step → Execute Next
|
| 1113 |
+
- **Iterative Progress**: Build your solution incrementally through multiple conversations
|
| 1114 |
+
- **Strategic Sequencing**: Choose the most logical next step based on previous results
|
| 1115 |
+
|
| 1116 |
+
**COMPLETENESS CHECKLIST**:
|
| 1117 |
+
Before considering the task complete, ensure you have:
|
| 1118 |
+
- ✅ All algorithms mentioned in the paper (including any abbreviations or alternative names)
|
| 1119 |
+
- ✅ All environments/datasets with exact versions specified
|
| 1120 |
+
- ✅ All comparison methods referenced in experiments
|
| 1121 |
+
- ✅ Working integration that can run the paper's experiments
|
| 1122 |
+
- ✅ Complete codebase that reproduces all metrics, figures, tables, and findings from the paper
|
| 1123 |
+
- ✅ Basic documentation explaining how to reproduce results
|
| 1124 |
+
|
| 1125 |
+
**CRITICAL SUCCESS FACTORS**:
|
| 1126 |
+
- **Accuracy**: Match paper specifications exactly (versions, parameters, configurations)
|
| 1127 |
+
- **Completeness**: Implement every method discussed, not just the main contribution
|
| 1128 |
+
- **Functionality**: Code must actually work and run experiments successfully
|
| 1129 |
+
|
| 1130 |
+
**AVOID DISTRACTIONS**: Focus implementation time on paper requirements rather than advanced tooling, extensive documentation, or optimization utilities that aren't needed for reproduction.
|
| 1131 |
+
|
| 1132 |
+
**REMEMBER**: Remember, you are tasked with replicating a whole paper, not just a single part of it or a minimal example. The file read tool is PAGINATED, so you will need to CALL IT MULTIPLE TIMES to make sure that you have read all the relevant parts of the paper.
|
| 1133 |
+
"""
|
| 1134 |
+
|
| 1135 |
+
|
| 1136 |
+
# General-purpose version of the above prompt for non-academic use cases
|
| 1137 |
+
GENERAL_CODE_IMPLEMENTATION_SYSTEM_PROMPT = """You are an expert code implementation agent for technical requirements implementation. Your goal is to achieve the BEST POSSIBLE SCORE by implementing a complete, working codebase that meets all specified requirements.
|
| 1138 |
+
|
| 1139 |
+
**PRIMARY OBJECTIVE**: Implement ALL algorithms, features, and components mentioned in the requirements. Success is measured by completeness and accuracy, not code elegance. Use available time to continuously refine and optimize your solution.
|
| 1140 |
+
|
| 1141 |
+
**CORE STRATEGY**:
|
| 1142 |
+
- Read the requirements thoroughly to identify every algorithm, feature, and component
|
| 1143 |
+
- Implement core algorithms first, then environments, then integration
|
| 1144 |
+
- Use exact versions and specifications mentioned in the requirements
|
| 1145 |
+
- Test each component immediately after implementation
|
| 1146 |
+
- Focus on working implementations over perfect architecture
|
| 1147 |
+
|
| 1148 |
+
**IMPLEMENTATION APPROACH**:
|
| 1149 |
+
Build incrementally using multiple tool calls. For each step:
|
| 1150 |
+
1. **Identify** what needs to be implemented from the requirements
|
| 1151 |
+
2. **Analyze Dependencies**: Before implementing each new file, use `read_code_mem` to read summaries of already-implemented files, then search for reference patterns to guide your implementation approach.
|
| 1152 |
+
3. **Implement** one component at a time
|
| 1153 |
+
4. **Test** immediately using `execute_python` or `execute_bash` to catch issues early - THIS IS MANDATORY, NOT OPTIONAL
|
| 1154 |
+
5. **Integrate** with existing components
|
| 1155 |
+
6. **Verify** against requirement specifications using execution tools to ensure everything works
|
| 1156 |
+
|
| 1157 |
+
**TOOL CALLING STRATEGY**:
|
| 1158 |
+
1. ⚠️ **SINGLE FUNCTION CALL PER MESSAGE**: Each message may perform only one function call. You will see the result of the function right after sending the message. If you need to perform multiple actions, you can always send more messages with subsequent function calls. Do some reasoning before your actions, describing what function calls you are going to use and how they fit into your plan.
|
| 1159 |
+
|
| 1160 |
+
2. **TOOL EXECUTION STRATEGY**:
|
| 1161 |
+
- **Development Cycle (for each new file implementation)**: `read_code_mem` (check existing implementations in Working Directory, use `read_file` as fallback if memory unavailable) → `write_file` (implement) → **MANDATORY TESTING**: `execute_python` or `execute_bash` (ALWAYS test after implementation)
|
| 1162 |
+
- **Environment Setup**: Use `execute_bash` for installing packages, setting up dependencies, downloading files, etc.
|
| 1163 |
+
- **Testing & Debugging**: Use `execute_python` for Python code testing and `execute_bash` for system commands, package installation, file operations, and bug fixing
|
| 1164 |
+
- **⚠️ TESTING REMINDER**: After implementing ANY file, you MUST call either `execute_python` or `execute_bash` to test the implementation. Do not skip this step!
|
| 1165 |
+
|
| 1166 |
+
3. **CRITICAL**: Use `execute_bash` and `execute_python` tools to ACTUALLY IMPLEMENT and TEST the requirements yourself - do not provide instructions. These tools are essential for:
|
| 1167 |
+
- Installing dependencies and setting up environments (`execute_bash`)
|
| 1168 |
+
- Testing Python implementations (`execute_python`)
|
| 1169 |
+
- Debugging and fixing issues (`execute_bash` for system-level, `execute_python` for Python-specific)
|
| 1170 |
+
- Validating that your code actually works before moving to the next component
|
| 1171 |
+
|
| 1172 |
+
**Execution Guidelines**:
|
| 1173 |
+
- **Plan First**: Before each action, explain your reasoning and which function you'll use
|
| 1174 |
+
- **One Step at a Time**: Execute → Observe Result → Plan Next Step → Execute Next
|
| 1175 |
+
- **Iterative Progress**: Build your solution incrementally through multiple conversations
|
| 1176 |
+
- **Strategic Sequencing**: Choose the most logical next step based on previous results
|
| 1177 |
+
|
| 1178 |
+
**COMPLETENESS CHECKLIST**:
|
| 1179 |
+
Before considering the task complete, ensure you have:
|
| 1180 |
+
- ✅ All algorithms mentioned in the requirements (including any abbreviations or alternative names)
|
| 1181 |
+
- ✅ All environments/dependencies with exact versions specified
|
| 1182 |
+
- ✅ All comparison methods or baseline implementations referenced
|
| 1183 |
+
- ✅ Working integration that can run all specified functionality
|
| 1184 |
+
- ✅ Complete codebase that implements all features, functionality, and outputs specified in the requirements
|
| 1185 |
+
- ✅ Basic documentation explaining how to use the implemented system
|
| 1186 |
+
|
| 1187 |
+
**CRITICAL SUCCESS FACTORS**:
|
| 1188 |
+
- **Accuracy**: Match requirement specifications exactly (versions, parameters, configurations)
|
| 1189 |
+
- **Completeness**: Implement every component discussed, not just the main functionality
|
| 1190 |
+
- **Functionality**: Code must actually work and run all specified features successfully
|
| 1191 |
+
|
| 1192 |
+
**AVOID DISTRACTIONS**: Focus implementation time on requirement fulfillment rather than advanced tooling, extensive documentation, or optimization utilities that aren't needed for the core functionality.
|
| 1193 |
+
|
| 1194 |
+
**REMEMBER**: Remember, you are tasked with implementing a complete system, not just a single part of it or a minimal example. The file read tool is PAGINATED, so you will need to CALL IT MULTIPLE TIMES to make sure that you have read all the relevant parts of the requirements.
|
| 1195 |
+
"""
|
| 1196 |
+
|
| 1197 |
+
# Chat Agent Planning Prompt (Universal for Academic and Engineering Use)
|
| 1198 |
+
CHAT_AGENT_PLANNING_PROMPT = """You are a universal project planning agent that creates implementation plans for any coding project: web apps, games, academic research, tools, etc.
|
| 1199 |
+
|
| 1200 |
+
# 🎯 OBJECTIVE
|
| 1201 |
+
Transform user requirements into a clear, actionable implementation plan with optimal file structure and dependencies.
|
| 1202 |
+
|
| 1203 |
+
# 📋 OUTPUT FORMAT
|
| 1204 |
+
|
| 1205 |
+
```yaml
|
| 1206 |
+
project_plan:
|
| 1207 |
+
title: "[Project Name]"
|
| 1208 |
+
description: "[Brief description]"
|
| 1209 |
+
project_type: "[web_app|game|academic|tool|api|other]"
|
| 1210 |
+
|
| 1211 |
+
# CUSTOM FILE TREE STRUCTURE (max 15 files, design as needed)
|
| 1212 |
+
file_structure: |
|
| 1213 |
+
project_root/
|
| 1214 |
+
├── main.py # Entry point
|
| 1215 |
+
├── [specific_files] # Core files based on project type
|
| 1216 |
+
├── [folder]/ # Organized folders if needed
|
| 1217 |
+
│ ├── __init__.py
|
| 1218 |
+
│ └── [module].py
|
| 1219 |
+
├── requirements.txt # Dependencies
|
| 1220 |
+
└── README.md # Basic documentation
|
| 1221 |
+
|
| 1222 |
+
# IMPORTANT: Output ACTUAL file tree structure above, not placeholder text
|
| 1223 |
+
# Examples by project type:
|
| 1224 |
+
# Web App: app.py, templates/, static/, models.py, config.py
|
| 1225 |
+
# Game: main.py, game/, assets/, sprites/, config.yaml
|
| 1226 |
+
# Academic: algorithm.py, experiments/, data/, utils.py, config.json
|
| 1227 |
+
# Tool: cli.py, core/, utils.py, tests/, setup.py
|
| 1228 |
+
|
| 1229 |
+
# CORE IMPLEMENTATION PLAN
|
| 1230 |
+
implementation_steps:
|
| 1231 |
+
1. "[First step - usually setup/core structure]"
|
| 1232 |
+
2. "[Second step - main functionality]"
|
| 1233 |
+
3. "[Third step - integration/interface]"
|
| 1234 |
+
4. "[Fourth step - testing/refinement]"
|
| 1235 |
+
|
| 1236 |
+
# DEPENDENCIES & SETUP
|
| 1237 |
+
dependencies:
|
| 1238 |
+
required_packages:
|
| 1239 |
+
- "[package1==version]"
|
| 1240 |
+
- "[package2>=version]"
|
| 1241 |
+
optional_packages:
|
| 1242 |
+
- "[optional1]: [purpose]"
|
| 1243 |
+
setup_commands:
|
| 1244 |
+
- "[command to setup environment]"
|
| 1245 |
+
- "[command to install dependencies]"
|
| 1246 |
+
|
| 1247 |
+
# KEY TECHNICAL DETAILS
|
| 1248 |
+
tech_stack:
|
| 1249 |
+
language: "[primary language]"
|
| 1250 |
+
frameworks: ["[framework1]", "[framework2]"]
|
| 1251 |
+
key_libraries: ["[lib1]", "[lib2]"]
|
| 1252 |
+
|
| 1253 |
+
main_features:
|
| 1254 |
+
- "[core feature 1]"
|
| 1255 |
+
- "[core feature 2]"
|
| 1256 |
+
- "[core feature 3]"
|
| 1257 |
+
```
|
| 1258 |
+
|
| 1259 |
+
# 🎯 PLANNING PRINCIPLES
|
| 1260 |
+
- **Flexibility**: Adapt file structure to project type (no fixed templates)
|
| 1261 |
+
- **Simplicity**: Keep under 15 files, focus on essentials
|
| 1262 |
+
- **Practicality**: Include specific packages/versions needed
|
| 1263 |
+
- **Clarity**: Clear implementation steps that can be directly coded
|
| 1264 |
+
- **Universality**: Work for any project type (web, game, academic, etc.)
|
| 1265 |
+
|
| 1266 |
+
# 📝 FILE STRUCTURE GUIDELINES
|
| 1267 |
+
- **MUST OUTPUT**: Actual file tree with specific filenames (not placeholder text)
|
| 1268 |
+
- Design structure based on project needs, not templates
|
| 1269 |
+
- Group related functionality logically
|
| 1270 |
+
- Include main entry point (main.py, app.py, etc.)
|
| 1271 |
+
- Add config/settings files if needed
|
| 1272 |
+
- Include requirements.txt or equivalent
|
| 1273 |
+
- Keep it minimal but complete (max 15 files)
|
| 1274 |
+
- Use tree format: ├── ─ │ symbols for visual hierarchy"""
|
| 1275 |
+
|
| 1276 |
+
# =============================================================================
|
| 1277 |
+
# TRADITIONAL PROMPTS (Non-segmented versions for smaller documents)
|
| 1278 |
+
# =============================================================================
|
| 1279 |
+
|
| 1280 |
+
# Traditional Algorithm Analysis Prompt (No Segmentation)
|
| 1281 |
+
PAPER_ALGORITHM_ANALYSIS_PROMPT_TRADITIONAL = """You are extracting COMPLETE implementation details from a research paper. Your goal is to capture EVERY algorithm, formula, and technical detail needed for perfect reproduction.
|
| 1282 |
+
|
| 1283 |
+
# DOCUMENT READING STRATEGY
|
| 1284 |
+
|
| 1285 |
+
## TRADITIONAL APPROACH: Full Document Reading
|
| 1286 |
+
Read the complete document to ensure comprehensive coverage of all algorithmic details:
|
| 1287 |
+
|
| 1288 |
+
1. **Locate and read the markdown (.md) file** in the paper directory
|
| 1289 |
+
2. **Analyze the entire document** to capture all algorithms, methods, and formulas
|
| 1290 |
+
3. **Extract complete implementation details** without missing any components
|
| 1291 |
+
|
| 1292 |
+
# DETAILED EXTRACTION PROTOCOL
|
| 1293 |
+
|
| 1294 |
+
## 1. COMPREHENSIVE ALGORITHM SCAN
|
| 1295 |
+
Read through the entire document systematically:
|
| 1296 |
+
- Method/Algorithm sections
|
| 1297 |
+
- Implementation Details
|
| 1298 |
+
- Hyperparameters and training details
|
| 1299 |
+
- Mathematical formulations
|
| 1300 |
+
|
| 1301 |
+
## 2. ALGORITHM DEEP EXTRACTION
|
| 1302 |
+
For EVERY algorithm/method/procedure mentioned:
|
| 1303 |
+
|
| 1304 |
+
### Algorithm Structure
|
| 1305 |
+
```yaml
|
| 1306 |
+
algorithm_name: "[Exact name from paper]"
|
| 1307 |
+
section: "[e.g., Section 3.2]"
|
| 1308 |
+
algorithm_box: "[e.g., Algorithm 1 on page 4]"
|
| 1309 |
+
|
| 1310 |
+
pseudocode: |
|
| 1311 |
+
[COPY THE EXACT PSEUDOCODE FROM PAPER]
|
| 1312 |
+
Input: ...
|
| 1313 |
+
Output: ...
|
| 1314 |
+
1. Initialize ...
|
| 1315 |
+
2. For each ...
|
| 1316 |
+
2.1 Calculate ...
|
| 1317 |
+
[Keep exact formatting and numbering]
|
| 1318 |
+
|
| 1319 |
+
mathematical_formulation:
|
| 1320 |
+
- equation: "[Copy formula EXACTLY, e.g., L = L_task + λ*L_explain]"
|
| 1321 |
+
equation_number: "[e.g., Eq. 3]"
|
| 1322 |
+
where:
|
| 1323 |
+
L_task: "task loss"
|
| 1324 |
+
L_explain: "explanation loss"
|
| 1325 |
+
λ: "weighting parameter (default: 0.5)"
|
| 1326 |
+
|
| 1327 |
+
step_by_step_breakdown:
|
| 1328 |
+
1. "[Detailed explanation of what step 1 does]"
|
| 1329 |
+
2. "[What step 2 computes and why]"
|
| 1330 |
+
|
| 1331 |
+
implementation_details:
|
| 1332 |
+
- "Uses softmax temperature τ = 0.1"
|
| 1333 |
+
- "Gradient clipping at norm 1.0"
|
| 1334 |
+
- "Initialize weights with Xavier uniform"
|
| 1335 |
+
```
|
| 1336 |
+
|
| 1337 |
+
## 3. COMPONENT EXTRACTION
|
| 1338 |
+
For EVERY component/module mentioned:
|
| 1339 |
+
|
| 1340 |
+
### Component Details
|
| 1341 |
+
```yaml
|
| 1342 |
+
component_name: "[e.g., Mask Network, Critic Network]"
|
| 1343 |
+
purpose: "[What this component does in the system]"
|
| 1344 |
+
architecture:
|
| 1345 |
+
input: "[shape and meaning]"
|
| 1346 |
+
layers:
|
| 1347 |
+
- "[Conv2d(3, 64, kernel=3, stride=1)]"
|
| 1348 |
+
- "[ReLU activation]"
|
| 1349 |
+
- "[BatchNorm2d(64)]"
|
| 1350 |
+
output: "[shape and meaning]"
|
| 1351 |
+
|
| 1352 |
+
special_features:
|
| 1353 |
+
- "[Any unique aspects]"
|
| 1354 |
+
- "[Special initialization]"
|
| 1355 |
+
```
|
| 1356 |
+
|
| 1357 |
+
## 4. TRAINING PROCEDURE
|
| 1358 |
+
Extract the COMPLETE training process:
|
| 1359 |
+
|
| 1360 |
+
```yaml
|
| 1361 |
+
training_loop:
|
| 1362 |
+
outer_iterations: "[number or condition]"
|
| 1363 |
+
inner_iterations: "[number or condition]"
|
| 1364 |
+
|
| 1365 |
+
steps:
|
| 1366 |
+
1. "Sample batch of size B from buffer"
|
| 1367 |
+
2. "Compute importance weights using..."
|
| 1368 |
+
3. "Update policy with loss..."
|
| 1369 |
+
|
| 1370 |
+
loss_functions:
|
| 1371 |
+
- name: "policy_loss"
|
| 1372 |
+
formula: "[exact formula]"
|
| 1373 |
+
components: "[what each term means]"
|
| 1374 |
+
|
| 1375 |
+
optimization:
|
| 1376 |
+
optimizer: "Adam"
|
| 1377 |
+
learning_rate: "3e-4"
|
| 1378 |
+
lr_schedule: "linear decay to 0"
|
| 1379 |
+
gradient_norm: "clip at 0.5"
|
| 1380 |
+
```
|
| 1381 |
+
|
| 1382 |
+
## 5. HYPERPARAMETERS HUNT
|
| 1383 |
+
Search EVERYWHERE (text, tables, captions) for:
|
| 1384 |
+
|
| 1385 |
+
```yaml
|
| 1386 |
+
hyperparameters:
|
| 1387 |
+
# Training
|
| 1388 |
+
batch_size: 64
|
| 1389 |
+
buffer_size: 1e6
|
| 1390 |
+
discount_gamma: 0.99
|
| 1391 |
+
|
| 1392 |
+
# Architecture
|
| 1393 |
+
hidden_units: [256, 256]
|
| 1394 |
+
activation: "ReLU"
|
| 1395 |
+
|
| 1396 |
+
# Algorithm-specific
|
| 1397 |
+
explanation_weight: 0.5
|
| 1398 |
+
exploration_bonus_scale: 0.1
|
| 1399 |
+
reset_probability: 0.3
|
| 1400 |
+
|
| 1401 |
+
# Found in:
|
| 1402 |
+
location_references:
|
| 1403 |
+
- "batch_size: Table 1"
|
| 1404 |
+
- "hidden_units: Section 4.1"
|
| 1405 |
+
```
|
| 1406 |
+
|
| 1407 |
+
# OUTPUT FORMAT
|
| 1408 |
+
```yaml
|
| 1409 |
+
complete_algorithm_extraction:
|
| 1410 |
+
paper_structure:
|
| 1411 |
+
method_sections: "[3, 3.1, 3.2, 3.3, 4]"
|
| 1412 |
+
algorithm_count: "[total number found]"
|
| 1413 |
+
|
| 1414 |
+
main_algorithm:
|
| 1415 |
+
[COMPLETE DETAILS AS ABOVE]
|
| 1416 |
+
|
| 1417 |
+
supporting_algorithms:
|
| 1418 |
+
- [EACH SUPPORTING ALGORITHM WITH FULL DETAILS]
|
| 1419 |
+
|
| 1420 |
+
components:
|
| 1421 |
+
- [EVERY COMPONENT WITH ARCHITECTURE]
|
| 1422 |
+
|
| 1423 |
+
training_details:
|
| 1424 |
+
[COMPLETE TRAINING PROCEDURE]
|
| 1425 |
+
|
| 1426 |
+
all_hyperparameters:
|
| 1427 |
+
[EVERY PARAMETER WITH VALUE AND SOURCE]
|
| 1428 |
+
|
| 1429 |
+
implementation_notes:
|
| 1430 |
+
- "[Any implementation hint from paper]"
|
| 1431 |
+
- "[Tricks mentioned in text]"
|
| 1432 |
+
|
| 1433 |
+
missing_but_critical:
|
| 1434 |
+
- "[What's not specified but essential]"
|
| 1435 |
+
- "[With suggested defaults]"
|
| 1436 |
+
```
|
| 1437 |
+
|
| 1438 |
+
BE EXHAUSTIVE. A developer should be able to implement the ENTIRE paper using only your extraction."""
|
| 1439 |
+
|
| 1440 |
+
# Traditional Concept Analysis Prompt (No Segmentation)
|
| 1441 |
+
PAPER_CONCEPT_ANALYSIS_PROMPT_TRADITIONAL = """You are doing a COMPREHENSIVE analysis of a research paper to understand its complete structure, contributions, and implementation requirements.
|
| 1442 |
+
|
| 1443 |
+
# OBJECTIVE
|
| 1444 |
+
Map out the ENTIRE paper structure and identify ALL components that need implementation for successful reproduction.
|
| 1445 |
+
|
| 1446 |
+
# DOCUMENT READING STRATEGY
|
| 1447 |
+
|
| 1448 |
+
## TRADITIONAL APPROACH: Complete Document Analysis
|
| 1449 |
+
Read the entire document systematically to ensure comprehensive understanding:
|
| 1450 |
+
|
| 1451 |
+
1. **Locate and read the markdown (.md) file** in the paper directory
|
| 1452 |
+
2. **Analyze the complete document structure** from introduction to conclusion
|
| 1453 |
+
3. **Extract all conceptual frameworks** and implementation requirements
|
| 1454 |
+
|
| 1455 |
+
# COMPREHENSIVE ANALYSIS PROTOCOL
|
| 1456 |
+
|
| 1457 |
+
## 1. COMPLETE PAPER STRUCTURAL ANALYSIS
|
| 1458 |
+
Create a full map of the document:
|
| 1459 |
+
|
| 1460 |
+
```yaml
|
| 1461 |
+
paper_structure_map:
|
| 1462 |
+
title: "[Full paper title]"
|
| 1463 |
+
|
| 1464 |
+
sections:
|
| 1465 |
+
1_introduction:
|
| 1466 |
+
main_claims: "[What the paper claims to achieve]"
|
| 1467 |
+
problem_definition: "[Exact problem being solved]"
|
| 1468 |
+
|
| 1469 |
+
2_related_work:
|
| 1470 |
+
key_comparisons: "[Methods this work builds upon or competes with]"
|
| 1471 |
+
|
| 1472 |
+
3_method: # May have multiple subsections
|
| 1473 |
+
subsections:
|
| 1474 |
+
3.1: "[Title and main content]"
|
| 1475 |
+
3.2: "[Title and main content]"
|
| 1476 |
+
algorithms_presented: "[List all algorithms by name]"
|
| 1477 |
+
|
| 1478 |
+
4_experiments:
|
| 1479 |
+
environments: "[All test environments/datasets]"
|
| 1480 |
+
baselines: "[All comparison methods]"
|
| 1481 |
+
metrics: "[All evaluation metrics used]"
|
| 1482 |
+
|
| 1483 |
+
5_results:
|
| 1484 |
+
main_findings: "[Key results that prove the method works]"
|
| 1485 |
+
tables_figures: "[Important result tables/figures to reproduce]"
|
| 1486 |
+
```
|
| 1487 |
+
|
| 1488 |
+
## 2. METHOD DECOMPOSITION
|
| 1489 |
+
For the main method/approach:
|
| 1490 |
+
|
| 1491 |
+
```yaml
|
| 1492 |
+
method_decomposition:
|
| 1493 |
+
method_name: "[Full name and acronym]"
|
| 1494 |
+
|
| 1495 |
+
core_components: # Break down into implementable pieces
|
| 1496 |
+
component_1:
|
| 1497 |
+
name: "[e.g., State Importance Estimator]"
|
| 1498 |
+
purpose: "[Why this component exists]"
|
| 1499 |
+
paper_section: "[Where it's described]"
|
| 1500 |
+
|
| 1501 |
+
component_2:
|
| 1502 |
+
name: "[e.g., Policy Refinement Module]"
|
| 1503 |
+
purpose: "[Its role in the system]"
|
| 1504 |
+
paper_section: "[Where it's described]"
|
| 1505 |
+
|
| 1506 |
+
component_interactions:
|
| 1507 |
+
- "[How component 1 feeds into component 2]"
|
| 1508 |
+
- "[Data flow between components]"
|
| 1509 |
+
|
| 1510 |
+
theoretical_foundation:
|
| 1511 |
+
key_insight: "[The main theoretical insight]"
|
| 1512 |
+
why_it_works: "[Intuitive explanation]"
|
| 1513 |
+
```
|
| 1514 |
+
|
| 1515 |
+
## 3. IMPLEMENTATION REQUIREMENTS MAPPING
|
| 1516 |
+
Map paper content to code requirements:
|
| 1517 |
+
|
| 1518 |
+
```yaml
|
| 1519 |
+
implementation_map:
|
| 1520 |
+
algorithms_to_implement:
|
| 1521 |
+
- algorithm: "[Name from paper]"
|
| 1522 |
+
section: "[Where defined]"
|
| 1523 |
+
complexity: "[Simple/Medium/Complex]"
|
| 1524 |
+
dependencies: "[What it needs to work]"
|
| 1525 |
+
|
| 1526 |
+
models_to_build:
|
| 1527 |
+
- model: "[Neural network or other model]"
|
| 1528 |
+
architecture_location: "[Section describing it]"
|
| 1529 |
+
purpose: "[What this model does]"
|
| 1530 |
+
|
| 1531 |
+
data_processing:
|
| 1532 |
+
- pipeline: "[Data preprocessing needed]"
|
| 1533 |
+
requirements: "[What the data should look like]"
|
| 1534 |
+
|
| 1535 |
+
evaluation_suite:
|
| 1536 |
+
- metric: "[Metric name]"
|
| 1537 |
+
formula_location: "[Where it's defined]"
|
| 1538 |
+
purpose: "[What it measures]"
|
| 1539 |
+
```
|
| 1540 |
+
|
| 1541 |
+
## 4. EXPERIMENT REPRODUCTION PLAN
|
| 1542 |
+
Identify ALL experiments needed:
|
| 1543 |
+
|
| 1544 |
+
```yaml
|
| 1545 |
+
experiments_analysis:
|
| 1546 |
+
main_results:
|
| 1547 |
+
- experiment: "[Name/description]"
|
| 1548 |
+
proves: "[What claim this validates]"
|
| 1549 |
+
requires: "[Components needed to run this]"
|
| 1550 |
+
expected_outcome: "[Specific numbers/trends]"
|
| 1551 |
+
|
| 1552 |
+
ablation_studies:
|
| 1553 |
+
- study: "[What is being ablated]"
|
| 1554 |
+
purpose: "[What this demonstrates]"
|
| 1555 |
+
|
| 1556 |
+
baseline_comparisons:
|
| 1557 |
+
- baseline: "[Method name]"
|
| 1558 |
+
implementation_required: "[Yes/No/Partial]"
|
| 1559 |
+
source: "[Where to find implementation]"
|
| 1560 |
+
```
|
| 1561 |
+
|
| 1562 |
+
## 5. CRITICAL SUCCESS FACTORS
|
| 1563 |
+
What defines successful reproduction:
|
| 1564 |
+
|
| 1565 |
+
```yaml
|
| 1566 |
+
success_criteria:
|
| 1567 |
+
must_achieve:
|
| 1568 |
+
- "[Primary result that must be reproduced]"
|
| 1569 |
+
- "[Core behavior that must be demonstrated]"
|
| 1570 |
+
|
| 1571 |
+
should_achieve:
|
| 1572 |
+
- "[Secondary results that validate the method]"
|
| 1573 |
+
|
| 1574 |
+
validation_evidence:
|
| 1575 |
+
- "[Specific figure/table to reproduce]"
|
| 1576 |
+
- "[Qualitative behavior to demonstrate]"
|
| 1577 |
+
```
|
| 1578 |
+
|
| 1579 |
+
# OUTPUT FORMAT
|
| 1580 |
+
```yaml
|
| 1581 |
+
comprehensive_paper_analysis:
|
| 1582 |
+
executive_summary:
|
| 1583 |
+
paper_title: "[Full title]"
|
| 1584 |
+
core_contribution: "[One sentence summary]"
|
| 1585 |
+
implementation_complexity: "[Low/Medium/High]"
|
| 1586 |
+
estimated_components: "[Number of major components to build]"
|
| 1587 |
+
|
| 1588 |
+
complete_structure_map:
|
| 1589 |
+
[FULL SECTION BREAKDOWN AS ABOVE]
|
| 1590 |
+
|
| 1591 |
+
method_architecture:
|
| 1592 |
+
[DETAILED COMPONENT BREAKDOWN]
|
| 1593 |
+
|
| 1594 |
+
implementation_requirements:
|
| 1595 |
+
[ALL ALGORITHMS, MODELS, DATA, METRICS]
|
| 1596 |
+
|
| 1597 |
+
reproduction_roadmap:
|
| 1598 |
+
phase_1: "[What to implement first]"
|
| 1599 |
+
phase_2: "[What to build next]"
|
| 1600 |
+
phase_3: "[Final components and validation]"
|
| 1601 |
+
|
| 1602 |
+
validation_checklist:
|
| 1603 |
+
- "[ ] [Specific result to achieve]"
|
| 1604 |
+
- "[ ] [Behavior to demonstrate]"
|
| 1605 |
+
- "[ ] [Metric to match]"
|
| 1606 |
+
```
|
| 1607 |
+
|
| 1608 |
+
BE THOROUGH. Miss nothing. The output should be a complete blueprint for reproduction."""
|
| 1609 |
+
|
| 1610 |
+
# Traditional Code Planning Prompt (No Segmentation)
|
| 1611 |
+
CODE_PLANNING_PROMPT_TRADITIONAL = """You are creating a DETAILED, COMPLETE reproduction plan by integrating comprehensive analysis results.
|
| 1612 |
+
|
| 1613 |
+
# INPUT
|
| 1614 |
+
You receive two exhaustive analyses:
|
| 1615 |
+
1. **Comprehensive Paper Analysis**: Complete paper structure, components, and requirements
|
| 1616 |
+
2. **Complete Algorithm Extraction**: All algorithms, formulas, pseudocode, and technical details
|
| 1617 |
+
|
| 1618 |
+
Plus you can access the complete paper document by reading the markdown file directly.
|
| 1619 |
+
|
| 1620 |
+
# TRADITIONAL DOCUMENT ACCESS
|
| 1621 |
+
|
| 1622 |
+
## Direct Paper Reading
|
| 1623 |
+
For any additional details needed beyond the provided analyses:
|
| 1624 |
+
|
| 1625 |
+
1. **Read the complete markdown (.md) file** in the paper directory
|
| 1626 |
+
2. **Access any section directly** without token limitations for smaller documents
|
| 1627 |
+
3. **Cross-reference information** across the entire document as needed
|
| 1628 |
+
|
| 1629 |
+
# OBJECTIVE
|
| 1630 |
+
Create an implementation plan so detailed that a developer can reproduce the ENTIRE paper without reading it.
|
| 1631 |
+
|
| 1632 |
+
# CONTENT LENGTH CONTROL
|
| 1633 |
+
⚠️ IMPORTANT: Generate a COMPLETE plan that includes ALL 5 sections without being cut off by token limits.
|
| 1634 |
+
|
| 1635 |
+
## Content Balance Guidelines:
|
| 1636 |
+
- **Section 1 (File Structure)**: Brief overview (10% of content) - Focus on CORE implementation files only
|
| 1637 |
+
- **Section 2 (Implementation Components)**: Detailed but concise (40% of content) - This is the PRIORITY section
|
| 1638 |
+
- **Section 3 (Validation)**: Moderate detail (25% of content) - Essential experiments and tests
|
| 1639 |
+
- **Section 4 (Environment)**: Brief but complete (10% of content) - All necessary dependencies
|
| 1640 |
+
- **Section 5 (Implementation Strategy)**: Moderate detail (15% of content) - Step-by-step approach
|
| 1641 |
+
|
| 1642 |
+
## File Priority Guidelines:
|
| 1643 |
+
🔧 **Implementation Priority Order**:
|
| 1644 |
+
1. **FIRST**: Core algorithm/model files (highest priority)
|
| 1645 |
+
2. **SECOND**: Supporting modules and utilities
|
| 1646 |
+
3. **THIRD**: Experiment and evaluation scripts
|
| 1647 |
+
4. **FOURTH**: Configuration and data handling
|
| 1648 |
+
5. **LAST**: Documentation files (README.md, requirements.txt) - These should be created AFTER core implementation
|
| 1649 |
+
|
| 1650 |
+
Note: README and requirements.txt are maintenance files that depend on the final implementation, so plan them last.
|
| 1651 |
+
|
| 1652 |
+
# DETAILED SYNTHESIS PROCESS
|
| 1653 |
+
|
| 1654 |
+
## 1. MERGE ALL INFORMATION
|
| 1655 |
+
Combine EVERYTHING from both analyses:
|
| 1656 |
+
- Every algorithm with its pseudocode
|
| 1657 |
+
- Every component with its architecture
|
| 1658 |
+
- Every hyperparameter with its value
|
| 1659 |
+
- Every experiment with expected results
|
| 1660 |
+
|
| 1661 |
+
## 2. MAP CONTENT TO IMPLEMENTATION
|
| 1662 |
+
|
| 1663 |
+
For each component you identify, specify how it will be implemented:
|
| 1664 |
+
|
| 1665 |
+
```
|
| 1666 |
+
# DESIGN YOUR MAPPING: Connect paper content to code organization
|
| 1667 |
+
[For each algorithm/component/method in the paper]:
|
| 1668 |
+
- What it does and where it's described in the paper
|
| 1669 |
+
- How you'll organize the code (files, classes, functions - your choice)
|
| 1670 |
+
- What specific formulas, algorithms, or procedures need implementation
|
| 1671 |
+
- Dependencies and relationships with other components
|
| 1672 |
+
- Implementation approach that makes sense for this specific paper
|
| 1673 |
+
```
|
| 1674 |
+
|
| 1675 |
+
## 3. EXTRACT ALL TECHNICAL DETAILS
|
| 1676 |
+
|
| 1677 |
+
Identify every technical detail that needs implementation:
|
| 1678 |
+
|
| 1679 |
+
```
|
| 1680 |
+
# COMPREHENSIVE TECHNICAL EXTRACTION:
|
| 1681 |
+
[Gather all implementation-relevant details from the paper]:
|
| 1682 |
+
- All algorithms with complete pseudocode and mathematical formulations
|
| 1683 |
+
- All parameters, hyperparameters, and configuration values
|
| 1684 |
+
- All architectural details (if applicable to your paper type)
|
| 1685 |
+
- All experimental procedures and evaluation methods
|
| 1686 |
+
- Any implementation hints, tricks, or special considerations mentioned
|
| 1687 |
+
```
|
| 1688 |
+
|
| 1689 |
+
# COMPREHENSIVE OUTPUT FORMAT
|
| 1690 |
+
|
| 1691 |
+
```yaml
|
| 1692 |
+
complete_reproduction_plan:
|
| 1693 |
+
paper_info:
|
| 1694 |
+
title: "[Full paper title]"
|
| 1695 |
+
core_contribution: "[Main innovation being reproduced]"
|
| 1696 |
+
|
| 1697 |
+
# SECTION 1: File Structure Design
|
| 1698 |
+
|
| 1699 |
+
# DESIGN YOUR OWN STRUCTURE: Create a file organization that best serves this specific paper
|
| 1700 |
+
# - Analyze what the paper contains (algorithms, models, experiments, systems, etc.)
|
| 1701 |
+
# - Organize files and directories in the most logical way for implementation
|
| 1702 |
+
# - Create meaningful names and groupings based on paper content
|
| 1703 |
+
# - Keep it clean, intuitive, and focused on what actually needs to be implemented
|
| 1704 |
+
# - EXCLUDE documentation files (README.md, requirements.txt) - these come last
|
| 1705 |
+
|
| 1706 |
+
file_structure: |
|
| 1707 |
+
[Design and specify your own project structure here - KEEP THIS BRIEF]
|
| 1708 |
+
[Focus ONLY on core implementation files, NOT documentation files]
|
| 1709 |
+
[Organize based on what this paper actually contains and needs]
|
| 1710 |
+
[Create directories and files that make sense for this specific implementation]
|
| 1711 |
+
[EXCLUDE: README.md, requirements.txt - these come last in implementation]
|
| 1712 |
+
|
| 1713 |
+
# SECTION 2: Implementation Components
|
| 1714 |
+
|
| 1715 |
+
# IDENTIFY AND SPECIFY: What needs to be implemented based on this paper
|
| 1716 |
+
# - List all algorithms, models, systems, or components mentioned
|
| 1717 |
+
# - Map each to implementation details and file locations
|
| 1718 |
+
# - Include formulas, pseudocode, and technical specifications
|
| 1719 |
+
# - Organize in whatever way makes sense for this paper
|
| 1720 |
+
|
| 1721 |
+
implementation_components: |
|
| 1722 |
+
[List and specify all components that need implementation]
|
| 1723 |
+
[For each component: purpose, location, algorithms, formulas, technical details]
|
| 1724 |
+
[Organize and structure this based on the paper's actual content]
|
| 1725 |
+
|
| 1726 |
+
# SECTION 3: Validation & Evaluation
|
| 1727 |
+
|
| 1728 |
+
# DESIGN VALIDATION: How to verify the implementation works correctly
|
| 1729 |
+
# - Define what experiments, tests, or proofs are needed
|
| 1730 |
+
# - Specify expected results from the paper (figures, tables, theorems)
|
| 1731 |
+
# - Design validation approach appropriate for this paper's domain
|
| 1732 |
+
# - Include setup requirements and success criteria
|
| 1733 |
+
|
| 1734 |
+
validation_approach: |
|
| 1735 |
+
[Design validation strategy appropriate for this paper]
|
| 1736 |
+
[Specify experiments, tests, or mathematical verification needed]
|
| 1737 |
+
[Define expected results and success criteria]
|
| 1738 |
+
[Include any special setup or evaluation requirements]
|
| 1739 |
+
|
| 1740 |
+
# SECTION 4: Environment & Dependencies
|
| 1741 |
+
|
| 1742 |
+
# SPECIFY REQUIREMENTS: What's needed to run this implementation
|
| 1743 |
+
# - Programming language and version requirements
|
| 1744 |
+
# - External libraries and exact versions (if specified in paper)
|
| 1745 |
+
# - Hardware requirements (GPU, memory, etc.)
|
| 1746 |
+
# - Any special setup or installation steps
|
| 1747 |
+
|
| 1748 |
+
environment_setup: |
|
| 1749 |
+
[List all dependencies and environment requirements for this specific paper]
|
| 1750 |
+
[Include versions where specified, reasonable defaults where not]
|
| 1751 |
+
[Note any special hardware or software requirements]
|
| 1752 |
+
|
| 1753 |
+
# SECTION 5: Implementation Strategy
|
| 1754 |
+
|
| 1755 |
+
# PLAN YOUR APPROACH: How to implement this paper step by step
|
| 1756 |
+
# - Break down implementation into logical phases
|
| 1757 |
+
# - Identify dependencies between components
|
| 1758 |
+
# - Plan verification and testing at each stage
|
| 1759 |
+
# - Handle missing details with reasonable defaults
|
| 1760 |
+
|
| 1761 |
+
implementation_strategy: |
|
| 1762 |
+
[Design your implementation approach for this specific paper]
|
| 1763 |
+
[Break into phases that make sense for this paper's components]
|
| 1764 |
+
[Plan testing and verification throughout the process]
|
| 1765 |
+
[Address any missing details or ambiguities in the paper]
|
| 1766 |
+
```
|
| 1767 |
+
|
| 1768 |
+
BE EXHAUSTIVE. Every algorithm, every formula, every parameter, every file should be specified in complete detail."""
|
projects/ui/DeepCode/schema/mcp-agent.config.schema.json
ADDED
|
@@ -0,0 +1,854 @@
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|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"$defs": {
|
| 3 |
+
"LogPathSettings": {
|
| 4 |
+
"description": "Settings for configuring log file paths with dynamic elements like timestamps or session IDs.",
|
| 5 |
+
"properties": {
|
| 6 |
+
"path_pattern": {
|
| 7 |
+
"default": "logs/mcp-agent-{unique_id}.jsonl",
|
| 8 |
+
"title": "Path Pattern",
|
| 9 |
+
"type": "string",
|
| 10 |
+
"description": "Path pattern for log files with a {unique_id} placeholder"
|
| 11 |
+
},
|
| 12 |
+
"unique_id": {
|
| 13 |
+
"default": "timestamp",
|
| 14 |
+
"enum": [
|
| 15 |
+
"timestamp",
|
| 16 |
+
"session_id"
|
| 17 |
+
],
|
| 18 |
+
"title": "Unique Id",
|
| 19 |
+
"type": "string",
|
| 20 |
+
"description": "Type of unique identifier to use in the log filename"
|
| 21 |
+
},
|
| 22 |
+
"timestamp_format": {
|
| 23 |
+
"default": "%Y%m%d_%H%M%S",
|
| 24 |
+
"title": "Timestamp Format",
|
| 25 |
+
"type": "string",
|
| 26 |
+
"description": "Format string for timestamps when unique_id is set to timestamp"
|
| 27 |
+
}
|
| 28 |
+
},
|
| 29 |
+
"title": "LogPathSettings",
|
| 30 |
+
"type": "object"
|
| 31 |
+
},
|
| 32 |
+
"AnthropicSettings": {
|
| 33 |
+
"additionalProperties": true,
|
| 34 |
+
"description": "Settings for using Anthropic models in the MCP Agent application.",
|
| 35 |
+
"properties": {
|
| 36 |
+
"api_key": {
|
| 37 |
+
"anyOf": [
|
| 38 |
+
{
|
| 39 |
+
"type": "string"
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"type": "null"
|
| 43 |
+
}
|
| 44 |
+
],
|
| 45 |
+
"default": null,
|
| 46 |
+
"title": "Api Key"
|
| 47 |
+
}
|
| 48 |
+
},
|
| 49 |
+
"title": "AnthropicSettings",
|
| 50 |
+
"type": "object"
|
| 51 |
+
},
|
| 52 |
+
"CohereSettings": {
|
| 53 |
+
"additionalProperties": true,
|
| 54 |
+
"description": "Settings for using Cohere models in the MCP Agent application.",
|
| 55 |
+
"properties": {
|
| 56 |
+
"api_key": {
|
| 57 |
+
"anyOf": [
|
| 58 |
+
{
|
| 59 |
+
"type": "string"
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
"type": "null"
|
| 63 |
+
}
|
| 64 |
+
],
|
| 65 |
+
"default": null,
|
| 66 |
+
"title": "Api Key"
|
| 67 |
+
}
|
| 68 |
+
},
|
| 69 |
+
"title": "CohereSettings",
|
| 70 |
+
"type": "object"
|
| 71 |
+
},
|
| 72 |
+
"LoggerSettings": {
|
| 73 |
+
"description": "Logger settings for the MCP Agent application.",
|
| 74 |
+
"properties": {
|
| 75 |
+
"type": {
|
| 76 |
+
"default": "console",
|
| 77 |
+
"enum": [
|
| 78 |
+
"none",
|
| 79 |
+
"console",
|
| 80 |
+
"file",
|
| 81 |
+
"http"
|
| 82 |
+
],
|
| 83 |
+
"title": "Type",
|
| 84 |
+
"type": "string"
|
| 85 |
+
},
|
| 86 |
+
"transports": {
|
| 87 |
+
"default": [
|
| 88 |
+
"console"
|
| 89 |
+
],
|
| 90 |
+
"items": {
|
| 91 |
+
"enum": [
|
| 92 |
+
"none",
|
| 93 |
+
"console",
|
| 94 |
+
"file",
|
| 95 |
+
"http"
|
| 96 |
+
],
|
| 97 |
+
"type": "string"
|
| 98 |
+
},
|
| 99 |
+
"title": "Transports",
|
| 100 |
+
"type": "array",
|
| 101 |
+
"description": "List of transports to use (can enable multiple simultaneously)"
|
| 102 |
+
},
|
| 103 |
+
"level": {
|
| 104 |
+
"default": "info",
|
| 105 |
+
"enum": [
|
| 106 |
+
"debug",
|
| 107 |
+
"info",
|
| 108 |
+
"warning",
|
| 109 |
+
"error"
|
| 110 |
+
],
|
| 111 |
+
"title": "Level",
|
| 112 |
+
"type": "string",
|
| 113 |
+
"description": "Minimum logging level"
|
| 114 |
+
},
|
| 115 |
+
"progress_display": {
|
| 116 |
+
"default": true,
|
| 117 |
+
"title": "Progress Display",
|
| 118 |
+
"type": "boolean",
|
| 119 |
+
"description": "Enable or disable the progress display"
|
| 120 |
+
},
|
| 121 |
+
"path": {
|
| 122 |
+
"default": "mcp-agent.jsonl",
|
| 123 |
+
"title": "Path",
|
| 124 |
+
"type": "string",
|
| 125 |
+
"description": "Path to log file, if logger 'type' is 'file'."
|
| 126 |
+
},
|
| 127 |
+
"path_settings": {
|
| 128 |
+
"anyOf": [
|
| 129 |
+
{
|
| 130 |
+
"$ref": "#/$defs/LogPathSettings"
|
| 131 |
+
},
|
| 132 |
+
{
|
| 133 |
+
"type": "null"
|
| 134 |
+
}
|
| 135 |
+
],
|
| 136 |
+
"default": null,
|
| 137 |
+
"title": "Path Settings",
|
| 138 |
+
"description": "Advanced settings for log file paths with dynamic elements like timestamps or session IDs"
|
| 139 |
+
},
|
| 140 |
+
"batch_size": {
|
| 141 |
+
"default": 100,
|
| 142 |
+
"title": "Batch Size",
|
| 143 |
+
"type": "integer",
|
| 144 |
+
"description": "Number of events to accumulate before processing"
|
| 145 |
+
},
|
| 146 |
+
"flush_interval": {
|
| 147 |
+
"default": 2.0,
|
| 148 |
+
"title": "Flush Interval",
|
| 149 |
+
"type": "number",
|
| 150 |
+
"description": "How often to flush events in seconds"
|
| 151 |
+
},
|
| 152 |
+
"max_queue_size": {
|
| 153 |
+
"default": 2048,
|
| 154 |
+
"title": "Max Queue Size",
|
| 155 |
+
"type": "integer",
|
| 156 |
+
"description": "Maximum queue size for event processing"
|
| 157 |
+
},
|
| 158 |
+
"http_endpoint": {
|
| 159 |
+
"anyOf": [
|
| 160 |
+
{
|
| 161 |
+
"type": "string"
|
| 162 |
+
},
|
| 163 |
+
{
|
| 164 |
+
"type": "null"
|
| 165 |
+
}
|
| 166 |
+
],
|
| 167 |
+
"default": null,
|
| 168 |
+
"title": "Http Endpoint",
|
| 169 |
+
"description": "HTTP endpoint for event transport"
|
| 170 |
+
},
|
| 171 |
+
"http_headers": {
|
| 172 |
+
"anyOf": [
|
| 173 |
+
{
|
| 174 |
+
"additionalProperties": {
|
| 175 |
+
"type": "string"
|
| 176 |
+
},
|
| 177 |
+
"type": "object"
|
| 178 |
+
},
|
| 179 |
+
{
|
| 180 |
+
"type": "null"
|
| 181 |
+
}
|
| 182 |
+
],
|
| 183 |
+
"default": null,
|
| 184 |
+
"title": "Http Headers",
|
| 185 |
+
"description": "HTTP headers for event transport"
|
| 186 |
+
},
|
| 187 |
+
"http_timeout": {
|
| 188 |
+
"default": 5.0,
|
| 189 |
+
"title": "Http Timeout",
|
| 190 |
+
"type": "number",
|
| 191 |
+
"description": "HTTP timeout seconds for event transport"
|
| 192 |
+
}
|
| 193 |
+
},
|
| 194 |
+
"title": "LoggerSettings",
|
| 195 |
+
"type": "object"
|
| 196 |
+
},
|
| 197 |
+
"MCPRootSettings": {
|
| 198 |
+
"additionalProperties": true,
|
| 199 |
+
"description": "Represents a root directory configuration for an MCP server.",
|
| 200 |
+
"properties": {
|
| 201 |
+
"uri": {
|
| 202 |
+
"title": "Uri",
|
| 203 |
+
"type": "string",
|
| 204 |
+
"description": "The URI identifying the root. Must start with file://"
|
| 205 |
+
},
|
| 206 |
+
"name": {
|
| 207 |
+
"anyOf": [
|
| 208 |
+
{
|
| 209 |
+
"type": "string"
|
| 210 |
+
},
|
| 211 |
+
{
|
| 212 |
+
"type": "null"
|
| 213 |
+
}
|
| 214 |
+
],
|
| 215 |
+
"default": null,
|
| 216 |
+
"title": "Name",
|
| 217 |
+
"description": "Optional name for the root."
|
| 218 |
+
},
|
| 219 |
+
"server_uri_alias": {
|
| 220 |
+
"anyOf": [
|
| 221 |
+
{
|
| 222 |
+
"type": "string"
|
| 223 |
+
},
|
| 224 |
+
{
|
| 225 |
+
"type": "null"
|
| 226 |
+
}
|
| 227 |
+
],
|
| 228 |
+
"default": null,
|
| 229 |
+
"title": "Server Uri Alias",
|
| 230 |
+
"description": "Optional URI alias for presentation to the server"
|
| 231 |
+
}
|
| 232 |
+
},
|
| 233 |
+
"required": [
|
| 234 |
+
"uri"
|
| 235 |
+
],
|
| 236 |
+
"title": "MCPRootSettings",
|
| 237 |
+
"type": "object"
|
| 238 |
+
},
|
| 239 |
+
"MCPServerAuthSettings": {
|
| 240 |
+
"additionalProperties": true,
|
| 241 |
+
"description": "Represents authentication configuration for a server.",
|
| 242 |
+
"properties": {
|
| 243 |
+
"api_key": {
|
| 244 |
+
"anyOf": [
|
| 245 |
+
{
|
| 246 |
+
"type": "string"
|
| 247 |
+
},
|
| 248 |
+
{
|
| 249 |
+
"type": "null"
|
| 250 |
+
}
|
| 251 |
+
],
|
| 252 |
+
"default": null,
|
| 253 |
+
"title": "Api Key"
|
| 254 |
+
}
|
| 255 |
+
},
|
| 256 |
+
"title": "MCPServerAuthSettings",
|
| 257 |
+
"type": "object"
|
| 258 |
+
},
|
| 259 |
+
"MCPServerSettings": {
|
| 260 |
+
"description": "Represents the configuration for an individual server.",
|
| 261 |
+
"properties": {
|
| 262 |
+
"name": {
|
| 263 |
+
"anyOf": [
|
| 264 |
+
{
|
| 265 |
+
"type": "string"
|
| 266 |
+
},
|
| 267 |
+
{
|
| 268 |
+
"type": "null"
|
| 269 |
+
}
|
| 270 |
+
],
|
| 271 |
+
"default": null,
|
| 272 |
+
"title": "Name",
|
| 273 |
+
"description": "The name of the server."
|
| 274 |
+
},
|
| 275 |
+
"description": {
|
| 276 |
+
"anyOf": [
|
| 277 |
+
{
|
| 278 |
+
"type": "string"
|
| 279 |
+
},
|
| 280 |
+
{
|
| 281 |
+
"type": "null"
|
| 282 |
+
}
|
| 283 |
+
],
|
| 284 |
+
"default": null,
|
| 285 |
+
"title": "Description",
|
| 286 |
+
"description": "The description of the server."
|
| 287 |
+
},
|
| 288 |
+
"transport": {
|
| 289 |
+
"default": "stdio",
|
| 290 |
+
"enum": [
|
| 291 |
+
"stdio",
|
| 292 |
+
"sse"
|
| 293 |
+
],
|
| 294 |
+
"title": "Transport",
|
| 295 |
+
"type": "string",
|
| 296 |
+
"description": "The transport mechanism."
|
| 297 |
+
},
|
| 298 |
+
"command": {
|
| 299 |
+
"anyOf": [
|
| 300 |
+
{
|
| 301 |
+
"type": "string"
|
| 302 |
+
},
|
| 303 |
+
{
|
| 304 |
+
"type": "null"
|
| 305 |
+
}
|
| 306 |
+
],
|
| 307 |
+
"default": null,
|
| 308 |
+
"title": "Command",
|
| 309 |
+
"description": "The command to execute the server (e.g. npx)."
|
| 310 |
+
},
|
| 311 |
+
"args": {
|
| 312 |
+
"anyOf": [
|
| 313 |
+
{
|
| 314 |
+
"items": {
|
| 315 |
+
"type": "string"
|
| 316 |
+
},
|
| 317 |
+
"type": "array"
|
| 318 |
+
},
|
| 319 |
+
{
|
| 320 |
+
"type": "null"
|
| 321 |
+
}
|
| 322 |
+
],
|
| 323 |
+
"default": null,
|
| 324 |
+
"title": "Args",
|
| 325 |
+
"description": "The arguments for the server command."
|
| 326 |
+
},
|
| 327 |
+
"read_timeout_seconds": {
|
| 328 |
+
"anyOf": [
|
| 329 |
+
{
|
| 330 |
+
"type": "integer"
|
| 331 |
+
},
|
| 332 |
+
{
|
| 333 |
+
"type": "null"
|
| 334 |
+
}
|
| 335 |
+
],
|
| 336 |
+
"default": null,
|
| 337 |
+
"title": "Read Timeout Seconds",
|
| 338 |
+
"description": "The timeout in seconds for the server connection."
|
| 339 |
+
},
|
| 340 |
+
"url": {
|
| 341 |
+
"anyOf": [
|
| 342 |
+
{
|
| 343 |
+
"type": "string"
|
| 344 |
+
},
|
| 345 |
+
{
|
| 346 |
+
"type": "null"
|
| 347 |
+
}
|
| 348 |
+
],
|
| 349 |
+
"default": null,
|
| 350 |
+
"title": "Url",
|
| 351 |
+
"description": "The URL for the server (e.g. for SSE transport)."
|
| 352 |
+
},
|
| 353 |
+
"auth": {
|
| 354 |
+
"anyOf": [
|
| 355 |
+
{
|
| 356 |
+
"$ref": "#/$defs/MCPServerAuthSettings"
|
| 357 |
+
},
|
| 358 |
+
{
|
| 359 |
+
"type": "null"
|
| 360 |
+
}
|
| 361 |
+
],
|
| 362 |
+
"default": null,
|
| 363 |
+
"description": "The authentication configuration for the server."
|
| 364 |
+
},
|
| 365 |
+
"roots": {
|
| 366 |
+
"anyOf": [
|
| 367 |
+
{
|
| 368 |
+
"items": {
|
| 369 |
+
"$ref": "#/$defs/MCPRootSettings"
|
| 370 |
+
},
|
| 371 |
+
"type": "array"
|
| 372 |
+
},
|
| 373 |
+
{
|
| 374 |
+
"type": "null"
|
| 375 |
+
}
|
| 376 |
+
],
|
| 377 |
+
"default": null,
|
| 378 |
+
"title": "Roots",
|
| 379 |
+
"description": "Root directories this server has access to."
|
| 380 |
+
},
|
| 381 |
+
"env": {
|
| 382 |
+
"anyOf": [
|
| 383 |
+
{
|
| 384 |
+
"additionalProperties": {
|
| 385 |
+
"type": "string"
|
| 386 |
+
},
|
| 387 |
+
"type": "object"
|
| 388 |
+
},
|
| 389 |
+
{
|
| 390 |
+
"type": "null"
|
| 391 |
+
}
|
| 392 |
+
],
|
| 393 |
+
"default": null,
|
| 394 |
+
"title": "Env",
|
| 395 |
+
"description": "Environment variables to pass to the server process."
|
| 396 |
+
}
|
| 397 |
+
},
|
| 398 |
+
"title": "MCPServerSettings",
|
| 399 |
+
"type": "object"
|
| 400 |
+
},
|
| 401 |
+
"MCPSettings": {
|
| 402 |
+
"additionalProperties": true,
|
| 403 |
+
"description": "Configuration for all MCP servers.",
|
| 404 |
+
"properties": {
|
| 405 |
+
"servers": {
|
| 406 |
+
"additionalProperties": {
|
| 407 |
+
"$ref": "#/$defs/MCPServerSettings"
|
| 408 |
+
},
|
| 409 |
+
"default": {},
|
| 410 |
+
"title": "Servers",
|
| 411 |
+
"type": "object"
|
| 412 |
+
}
|
| 413 |
+
},
|
| 414 |
+
"title": "MCPSettings",
|
| 415 |
+
"type": "object"
|
| 416 |
+
},
|
| 417 |
+
"OpenAISettings": {
|
| 418 |
+
"additionalProperties": true,
|
| 419 |
+
"description": "Settings for using OpenAI models in the MCP Agent application.",
|
| 420 |
+
"properties": {
|
| 421 |
+
"api_key": {
|
| 422 |
+
"anyOf": [
|
| 423 |
+
{
|
| 424 |
+
"type": "string"
|
| 425 |
+
},
|
| 426 |
+
{
|
| 427 |
+
"type": "null"
|
| 428 |
+
}
|
| 429 |
+
],
|
| 430 |
+
"default": null,
|
| 431 |
+
"title": "Api Key"
|
| 432 |
+
},
|
| 433 |
+
"reasoning_effort": {
|
| 434 |
+
"default": "medium",
|
| 435 |
+
"enum": [
|
| 436 |
+
"low",
|
| 437 |
+
"medium",
|
| 438 |
+
"high"
|
| 439 |
+
],
|
| 440 |
+
"title": "Reasoning Effort",
|
| 441 |
+
"type": "string"
|
| 442 |
+
},
|
| 443 |
+
"base_url": {
|
| 444 |
+
"anyOf": [
|
| 445 |
+
{
|
| 446 |
+
"type": "string"
|
| 447 |
+
},
|
| 448 |
+
{
|
| 449 |
+
"type": "null"
|
| 450 |
+
}
|
| 451 |
+
],
|
| 452 |
+
"default": null,
|
| 453 |
+
"title": "Base Url"
|
| 454 |
+
}
|
| 455 |
+
},
|
| 456 |
+
"title": "OpenAISettings",
|
| 457 |
+
"type": "object"
|
| 458 |
+
},
|
| 459 |
+
"AzureSettings": {
|
| 460 |
+
"additionalProperties": true,
|
| 461 |
+
"description": "Settings for using Azure models in the MCP Agent application.",
|
| 462 |
+
"properties": {
|
| 463 |
+
"api_key": {
|
| 464 |
+
"anyOf": [
|
| 465 |
+
{
|
| 466 |
+
"type": "string"
|
| 467 |
+
}
|
| 468 |
+
],
|
| 469 |
+
"default": null,
|
| 470 |
+
"title": "Api Key"
|
| 471 |
+
},
|
| 472 |
+
"endpoint": {
|
| 473 |
+
"anyOf": [
|
| 474 |
+
{
|
| 475 |
+
"type": "string"
|
| 476 |
+
}
|
| 477 |
+
],
|
| 478 |
+
"default": null,
|
| 479 |
+
"title": "Azure Endpoint"
|
| 480 |
+
},
|
| 481 |
+
"api_version": {
|
| 482 |
+
"anyOf": [
|
| 483 |
+
{
|
| 484 |
+
"type": "string"
|
| 485 |
+
},
|
| 486 |
+
{
|
| 487 |
+
"type": "null"
|
| 488 |
+
}
|
| 489 |
+
],
|
| 490 |
+
"default": null,
|
| 491 |
+
"title": "API Version"
|
| 492 |
+
}
|
| 493 |
+
},
|
| 494 |
+
"required": [
|
| 495 |
+
"api_key",
|
| 496 |
+
"endpoint"
|
| 497 |
+
],
|
| 498 |
+
"title": "AzureSettings",
|
| 499 |
+
"type": "object"
|
| 500 |
+
},
|
| 501 |
+
"BedrockSettings": {
|
| 502 |
+
"additionalProperties": true,
|
| 503 |
+
"description": "Settings for using AWS Bedrock models in the MCP Agent application.",
|
| 504 |
+
"properties": {
|
| 505 |
+
"aws_region": {
|
| 506 |
+
"anyOf": [
|
| 507 |
+
{
|
| 508 |
+
"type": "string"
|
| 509 |
+
},
|
| 510 |
+
{
|
| 511 |
+
"type": "null"
|
| 512 |
+
}
|
| 513 |
+
],
|
| 514 |
+
"default": null,
|
| 515 |
+
"title": "Region"
|
| 516 |
+
},
|
| 517 |
+
"aws_access_key_id": {
|
| 518 |
+
"anyOf": [
|
| 519 |
+
{
|
| 520 |
+
"type": "string"
|
| 521 |
+
},
|
| 522 |
+
{
|
| 523 |
+
"type": "null"
|
| 524 |
+
}
|
| 525 |
+
],
|
| 526 |
+
"default": null,
|
| 527 |
+
"title": "Access Key Id"
|
| 528 |
+
},
|
| 529 |
+
"aws_secret_access_key": {
|
| 530 |
+
"anyOf": [
|
| 531 |
+
{
|
| 532 |
+
"type": "string"
|
| 533 |
+
},
|
| 534 |
+
{
|
| 535 |
+
"type": "null"
|
| 536 |
+
}
|
| 537 |
+
],
|
| 538 |
+
"default": null,
|
| 539 |
+
"title": "Secret Access Key"
|
| 540 |
+
},
|
| 541 |
+
"aws_session_token": {
|
| 542 |
+
"anyOf": [
|
| 543 |
+
{
|
| 544 |
+
"type": "string"
|
| 545 |
+
},
|
| 546 |
+
{
|
| 547 |
+
"type": "null"
|
| 548 |
+
}
|
| 549 |
+
],
|
| 550 |
+
"default": null,
|
| 551 |
+
"title": "Session Token"
|
| 552 |
+
},
|
| 553 |
+
"profile": {
|
| 554 |
+
"anyOf": [
|
| 555 |
+
{
|
| 556 |
+
"type": "string"
|
| 557 |
+
},
|
| 558 |
+
{
|
| 559 |
+
"type": "null"
|
| 560 |
+
}
|
| 561 |
+
],
|
| 562 |
+
"default": null,
|
| 563 |
+
"title": "Profile"
|
| 564 |
+
}
|
| 565 |
+
},
|
| 566 |
+
"title": "BedrockSettings",
|
| 567 |
+
"type": "object"
|
| 568 |
+
},
|
| 569 |
+
"OpenTelemetrySettings": {
|
| 570 |
+
"description": "OTEL settings for the MCP Agent application.",
|
| 571 |
+
"properties": {
|
| 572 |
+
"enabled": {
|
| 573 |
+
"default": true,
|
| 574 |
+
"title": "Enabled",
|
| 575 |
+
"type": "boolean"
|
| 576 |
+
},
|
| 577 |
+
"service_name": {
|
| 578 |
+
"default": "mcp-agent",
|
| 579 |
+
"title": "Service Name",
|
| 580 |
+
"type": "string"
|
| 581 |
+
},
|
| 582 |
+
"service_instance_id": {
|
| 583 |
+
"anyOf": [
|
| 584 |
+
{
|
| 585 |
+
"type": "string"
|
| 586 |
+
},
|
| 587 |
+
{
|
| 588 |
+
"type": "null"
|
| 589 |
+
}
|
| 590 |
+
],
|
| 591 |
+
"default": null,
|
| 592 |
+
"title": "Service Instance Id"
|
| 593 |
+
},
|
| 594 |
+
"service_version": {
|
| 595 |
+
"anyOf": [
|
| 596 |
+
{
|
| 597 |
+
"type": "string"
|
| 598 |
+
},
|
| 599 |
+
{
|
| 600 |
+
"type": "null"
|
| 601 |
+
}
|
| 602 |
+
],
|
| 603 |
+
"default": null,
|
| 604 |
+
"title": "Service Version"
|
| 605 |
+
},
|
| 606 |
+
"otlp_endpoint": {
|
| 607 |
+
"anyOf": [
|
| 608 |
+
{
|
| 609 |
+
"type": "string"
|
| 610 |
+
},
|
| 611 |
+
{
|
| 612 |
+
"type": "null"
|
| 613 |
+
}
|
| 614 |
+
],
|
| 615 |
+
"default": null,
|
| 616 |
+
"title": "Otlp Endpoint",
|
| 617 |
+
"description": "OTLP endpoint for OpenTelemetry tracing"
|
| 618 |
+
},
|
| 619 |
+
"console_debug": {
|
| 620 |
+
"default": false,
|
| 621 |
+
"title": "Console Debug",
|
| 622 |
+
"type": "boolean",
|
| 623 |
+
"description": "Log spans to console"
|
| 624 |
+
},
|
| 625 |
+
"sample_rate": {
|
| 626 |
+
"default": 1.0,
|
| 627 |
+
"title": "Sample Rate",
|
| 628 |
+
"type": "number",
|
| 629 |
+
"description": "Sample rate for tracing (1.0 = sample everything)"
|
| 630 |
+
}
|
| 631 |
+
},
|
| 632 |
+
"title": "OpenTelemetrySettings",
|
| 633 |
+
"type": "object"
|
| 634 |
+
},
|
| 635 |
+
"TemporalSettings": {
|
| 636 |
+
"description": "Temporal settings for the MCP Agent application.",
|
| 637 |
+
"properties": {
|
| 638 |
+
"host": {
|
| 639 |
+
"title": "Host",
|
| 640 |
+
"type": "string"
|
| 641 |
+
},
|
| 642 |
+
"namespace": {
|
| 643 |
+
"default": "default",
|
| 644 |
+
"title": "Namespace",
|
| 645 |
+
"type": "string"
|
| 646 |
+
},
|
| 647 |
+
"task_queue": {
|
| 648 |
+
"title": "Task Queue",
|
| 649 |
+
"type": "string"
|
| 650 |
+
},
|
| 651 |
+
"api_key": {
|
| 652 |
+
"anyOf": [
|
| 653 |
+
{
|
| 654 |
+
"type": "string"
|
| 655 |
+
},
|
| 656 |
+
{
|
| 657 |
+
"type": "null"
|
| 658 |
+
}
|
| 659 |
+
],
|
| 660 |
+
"default": null,
|
| 661 |
+
"title": "Api Key"
|
| 662 |
+
}
|
| 663 |
+
},
|
| 664 |
+
"required": [
|
| 665 |
+
"host",
|
| 666 |
+
"task_queue"
|
| 667 |
+
],
|
| 668 |
+
"title": "TemporalSettings",
|
| 669 |
+
"type": "object"
|
| 670 |
+
},
|
| 671 |
+
"UsageTelemetrySettings": {
|
| 672 |
+
"description": "Settings for usage telemetry in the MCP Agent application.\nAnonymized usage metrics are sent to a telemetry server to help improve the product.",
|
| 673 |
+
"properties": {
|
| 674 |
+
"enabled": {
|
| 675 |
+
"default": true,
|
| 676 |
+
"title": "Enabled",
|
| 677 |
+
"type": "boolean",
|
| 678 |
+
"description": "Enable usage telemetry in the MCP Agent application."
|
| 679 |
+
},
|
| 680 |
+
"enable_detailed_telemetry": {
|
| 681 |
+
"default": false,
|
| 682 |
+
"title": "Enable Detailed Telemetry",
|
| 683 |
+
"type": "boolean",
|
| 684 |
+
"description": "If enabled, detailed telemetry data, including prompts and agents, will be sent to the telemetry server."
|
| 685 |
+
}
|
| 686 |
+
},
|
| 687 |
+
"title": "UsageTelemetrySettings",
|
| 688 |
+
"type": "object"
|
| 689 |
+
}
|
| 690 |
+
},
|
| 691 |
+
"additionalProperties": true,
|
| 692 |
+
"description": "Configuration schema for MCP Agent applications",
|
| 693 |
+
"properties": {
|
| 694 |
+
"mcp": {
|
| 695 |
+
"anyOf": [
|
| 696 |
+
{
|
| 697 |
+
"$ref": "#/$defs/MCPSettings"
|
| 698 |
+
},
|
| 699 |
+
{
|
| 700 |
+
"type": "null"
|
| 701 |
+
}
|
| 702 |
+
],
|
| 703 |
+
"default": {
|
| 704 |
+
"servers": {}
|
| 705 |
+
},
|
| 706 |
+
"description": "MCP config, such as MCP servers"
|
| 707 |
+
},
|
| 708 |
+
"execution_engine": {
|
| 709 |
+
"default": "asyncio",
|
| 710 |
+
"enum": [
|
| 711 |
+
"asyncio",
|
| 712 |
+
"temporal"
|
| 713 |
+
],
|
| 714 |
+
"title": "Execution Engine",
|
| 715 |
+
"type": "string",
|
| 716 |
+
"description": "Execution engine for the MCP Agent application"
|
| 717 |
+
},
|
| 718 |
+
"temporal": {
|
| 719 |
+
"anyOf": [
|
| 720 |
+
{
|
| 721 |
+
"$ref": "#/$defs/TemporalSettings"
|
| 722 |
+
},
|
| 723 |
+
{
|
| 724 |
+
"type": "null"
|
| 725 |
+
}
|
| 726 |
+
],
|
| 727 |
+
"default": null,
|
| 728 |
+
"description": "Settings for Temporal workflow orchestration"
|
| 729 |
+
},
|
| 730 |
+
"anthropic": {
|
| 731 |
+
"anyOf": [
|
| 732 |
+
{
|
| 733 |
+
"$ref": "#/$defs/AnthropicSettings"
|
| 734 |
+
},
|
| 735 |
+
{
|
| 736 |
+
"type": "null"
|
| 737 |
+
}
|
| 738 |
+
],
|
| 739 |
+
"default": null,
|
| 740 |
+
"description": "Settings for using Anthropic models in the MCP Agent application"
|
| 741 |
+
},
|
| 742 |
+
"cohere": {
|
| 743 |
+
"anyOf": [
|
| 744 |
+
{
|
| 745 |
+
"$ref": "#/$defs/CohereSettings"
|
| 746 |
+
},
|
| 747 |
+
{
|
| 748 |
+
"type": "null"
|
| 749 |
+
}
|
| 750 |
+
],
|
| 751 |
+
"default": null,
|
| 752 |
+
"description": "Settings for using Cohere models in the MCP Agent application"
|
| 753 |
+
},
|
| 754 |
+
"openai": {
|
| 755 |
+
"anyOf": [
|
| 756 |
+
{
|
| 757 |
+
"$ref": "#/$defs/OpenAISettings"
|
| 758 |
+
},
|
| 759 |
+
{
|
| 760 |
+
"type": "null"
|
| 761 |
+
}
|
| 762 |
+
],
|
| 763 |
+
"default": null,
|
| 764 |
+
"description": "Settings for using OpenAI models in the MCP Agent application"
|
| 765 |
+
},
|
| 766 |
+
"azure": {
|
| 767 |
+
"anyOf": [
|
| 768 |
+
{
|
| 769 |
+
"$ref": "#/$defs/AzureSettings"
|
| 770 |
+
},
|
| 771 |
+
{
|
| 772 |
+
"type": "null"
|
| 773 |
+
}
|
| 774 |
+
],
|
| 775 |
+
"default": null,
|
| 776 |
+
"description": "Settings for using Azure models in the MCP Agent application"
|
| 777 |
+
},
|
| 778 |
+
"bedrock": {
|
| 779 |
+
"anyOf": [
|
| 780 |
+
{
|
| 781 |
+
"$ref": "#/$defs/BedrockSettings"
|
| 782 |
+
},
|
| 783 |
+
{
|
| 784 |
+
"type": "null"
|
| 785 |
+
}
|
| 786 |
+
],
|
| 787 |
+
"default": null,
|
| 788 |
+
"description": "Settings for using Bedrock models in the MCP Agent application"
|
| 789 |
+
},
|
| 790 |
+
"otel": {
|
| 791 |
+
"anyOf": [
|
| 792 |
+
{
|
| 793 |
+
"$ref": "#/$defs/OpenTelemetrySettings"
|
| 794 |
+
},
|
| 795 |
+
{
|
| 796 |
+
"type": "null"
|
| 797 |
+
}
|
| 798 |
+
],
|
| 799 |
+
"default": {
|
| 800 |
+
"enabled": true,
|
| 801 |
+
"service_name": "mcp-agent",
|
| 802 |
+
"service_instance_id": null,
|
| 803 |
+
"service_version": null,
|
| 804 |
+
"otlp_endpoint": null,
|
| 805 |
+
"console_debug": false,
|
| 806 |
+
"sample_rate": 1.0
|
| 807 |
+
},
|
| 808 |
+
"description": "OpenTelemetry logging settings for the MCP Agent application"
|
| 809 |
+
},
|
| 810 |
+
"logger": {
|
| 811 |
+
"anyOf": [
|
| 812 |
+
{
|
| 813 |
+
"$ref": "#/$defs/LoggerSettings"
|
| 814 |
+
},
|
| 815 |
+
{
|
| 816 |
+
"type": "null"
|
| 817 |
+
}
|
| 818 |
+
],
|
| 819 |
+
"default": {
|
| 820 |
+
"type": "console",
|
| 821 |
+
"transports": [],
|
| 822 |
+
"level": "info",
|
| 823 |
+
"progress_display": true,
|
| 824 |
+
"path": "mcp-agent.jsonl",
|
| 825 |
+
"path_settings": null,
|
| 826 |
+
"batch_size": 100,
|
| 827 |
+
"flush_interval": 2.0,
|
| 828 |
+
"max_queue_size": 2048,
|
| 829 |
+
"http_endpoint": null,
|
| 830 |
+
"http_headers": null,
|
| 831 |
+
"http_timeout": 5.0
|
| 832 |
+
},
|
| 833 |
+
"description": "Logger settings for the MCP Agent application"
|
| 834 |
+
},
|
| 835 |
+
"usage_telemetry": {
|
| 836 |
+
"anyOf": [
|
| 837 |
+
{
|
| 838 |
+
"$ref": "#/$defs/UsageTelemetrySettings"
|
| 839 |
+
},
|
| 840 |
+
{
|
| 841 |
+
"type": "null"
|
| 842 |
+
}
|
| 843 |
+
],
|
| 844 |
+
"default": {
|
| 845 |
+
"enabled": true,
|
| 846 |
+
"enable_detailed_telemetry": false
|
| 847 |
+
},
|
| 848 |
+
"description": "Usage tracking settings for the MCP Agent application"
|
| 849 |
+
}
|
| 850 |
+
},
|
| 851 |
+
"title": "MCP Agent Configuration Schema",
|
| 852 |
+
"type": "object",
|
| 853 |
+
"$schema": "http://json-schema.org/draft-07/schema#"
|
| 854 |
+
}
|
projects/ui/DeepCode/tools/__init__.py
ADDED
|
File without changes
|
projects/ui/DeepCode/tools/bocha_search_server.py
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
import httpx
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from mcp.server.fastmcp import FastMCP
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# Initialize FastMCP server
|
| 13 |
+
server = FastMCP(
|
| 14 |
+
"bocha-search-mcp",
|
| 15 |
+
prompt="""
|
| 16 |
+
# Bocha Search MCP Server
|
| 17 |
+
|
| 18 |
+
Bocha is a Chinese search engine for AI, This server provides tools for searching the web using Bocha Search API.
|
| 19 |
+
It allows you to get enhanced search details from billions of web documents, including weather, news, wikis, healthcare, train tickets, images, and more.
|
| 20 |
+
|
| 21 |
+
## Available Tools
|
| 22 |
+
|
| 23 |
+
### 1. bocha_web_search
|
| 24 |
+
Search with Bocha Web Search and get enhanced search details from billions of web documents, including page titles, urls, summaries, site names, site icons, publication dates, image links, and more.
|
| 25 |
+
|
| 26 |
+
### 2. bocha_ai_search
|
| 27 |
+
Search with Bocha AI Search, recognizes the semantics of search terms and additionally returns structured modal cards with content from vertical domains.
|
| 28 |
+
|
| 29 |
+
## Output Format
|
| 30 |
+
|
| 31 |
+
All search results will be formatted as text with clear sections for each
|
| 32 |
+
result item, including:
|
| 33 |
+
|
| 34 |
+
- Bocha Web search: Title, URL, Description, Published date and Site name
|
| 35 |
+
- Bocha AI search: Title, URL, Description, Published date, Site name, and structured data card
|
| 36 |
+
|
| 37 |
+
If the API key is missing or invalid, appropriate error messages will be returned.
|
| 38 |
+
""",
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@server.tool()
|
| 43 |
+
async def bocha_web_search(
|
| 44 |
+
query: str, freshness: str = "noLimit", count: int = 10
|
| 45 |
+
) -> str:
|
| 46 |
+
"""Search with Bocha Web Search and get enhanced search details from billions of web documents,
|
| 47 |
+
including page titles, urls, summaries, site names, site icons, publication dates, image links, and more.
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
query: Search query (required)
|
| 51 |
+
freshness: The time range for the search results. (Available options YYYY-MM-DD, YYYY-MM-DD..YYYY-MM-DD, noLimit, oneYear, oneMonth, oneWeek, oneDay. Default is noLimit)
|
| 52 |
+
count: Number of results (1-50, default 10)
|
| 53 |
+
"""
|
| 54 |
+
# Get API key from environment
|
| 55 |
+
boch_api_key = os.environ.get("BOCHA_API_KEY", "")
|
| 56 |
+
|
| 57 |
+
if not boch_api_key:
|
| 58 |
+
return (
|
| 59 |
+
"Error: Bocha API key is not configured. Please set the "
|
| 60 |
+
"BOCHA_API_KEY environment variable."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Endpoint
|
| 64 |
+
endpoint = "https://api.bochaai.com/v1/web-search?utm_source=bocha-mcp-local"
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
payload = {
|
| 68 |
+
"query": query,
|
| 69 |
+
"summary": True,
|
| 70 |
+
"freshness": freshness,
|
| 71 |
+
"count": count,
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
headers = {
|
| 75 |
+
"Authorization": f"Bearer {boch_api_key}",
|
| 76 |
+
"Content-Type": "application/json",
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
async with httpx.AsyncClient() as client:
|
| 80 |
+
response = await client.post(
|
| 81 |
+
endpoint, headers=headers, json=payload, timeout=10.0
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
response.raise_for_status()
|
| 85 |
+
resp = response.json()
|
| 86 |
+
if "data" not in resp:
|
| 87 |
+
return "Search error."
|
| 88 |
+
|
| 89 |
+
data = resp["data"]
|
| 90 |
+
|
| 91 |
+
if "webPages" not in data:
|
| 92 |
+
return "No results found."
|
| 93 |
+
|
| 94 |
+
results = []
|
| 95 |
+
for result in data["webPages"]["value"]:
|
| 96 |
+
results.append(
|
| 97 |
+
f"Title: {result['name']}\n"
|
| 98 |
+
f"URL: {result['url']}\n"
|
| 99 |
+
f"Description: {result['summary']}\n"
|
| 100 |
+
f"Published date: {result['datePublished']}\n"
|
| 101 |
+
f"Site name: {result['siteName']}"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
return "\n\n".join(results)
|
| 105 |
+
|
| 106 |
+
except httpx.HTTPStatusError as e:
|
| 107 |
+
return f"Bocha Web Search API HTTP error occurred: {e.response.status_code} - {e.response.text}"
|
| 108 |
+
except httpx.RequestError as e:
|
| 109 |
+
return f"Error communicating with Bocha Web Search API: {str(e)}"
|
| 110 |
+
except Exception as e:
|
| 111 |
+
return f"Unexpected error: {str(e)}"
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
@server.tool()
|
| 115 |
+
async def bocha_ai_search(
|
| 116 |
+
query: str, freshness: str = "noLimit", count: int = 10
|
| 117 |
+
) -> str:
|
| 118 |
+
"""Search with Bocha AI Search, recognizes the semantics of search terms
|
| 119 |
+
and additionally returns structured modal cards with content from vertical domains.
|
| 120 |
+
|
| 121 |
+
Args:
|
| 122 |
+
query: Search query (required)
|
| 123 |
+
freshness: The time range for the search results. (Available options noLimit, oneYear, oneMonth, oneWeek, oneDay. Default is noLimit)
|
| 124 |
+
count: Number of results (1-50, default 10)
|
| 125 |
+
"""
|
| 126 |
+
# Get API key from environment
|
| 127 |
+
boch_api_key = os.environ.get("BOCHA_API_KEY", "")
|
| 128 |
+
|
| 129 |
+
if not boch_api_key:
|
| 130 |
+
return (
|
| 131 |
+
"Error: Bocha API key is not configured. Please set the "
|
| 132 |
+
"BOCHA_API_KEY environment variable."
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# Endpoint
|
| 136 |
+
endpoint = "https://api.bochaai.com/v1/ai-search?utm_source=bocha-mcp-local"
|
| 137 |
+
|
| 138 |
+
try:
|
| 139 |
+
payload = {
|
| 140 |
+
"query": query,
|
| 141 |
+
"freshness": freshness,
|
| 142 |
+
"count": count,
|
| 143 |
+
"answer": False,
|
| 144 |
+
"stream": False,
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
headers = {
|
| 148 |
+
"Authorization": f"Bearer {boch_api_key}",
|
| 149 |
+
"Content-Type": "application/json",
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
async with httpx.AsyncClient() as client:
|
| 153 |
+
response = await client.post(
|
| 154 |
+
endpoint, headers=headers, json=payload, timeout=10.0
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
response.raise_for_status()
|
| 158 |
+
response = response.json()
|
| 159 |
+
results = []
|
| 160 |
+
if "messages" in response:
|
| 161 |
+
for message in response["messages"]:
|
| 162 |
+
content = {}
|
| 163 |
+
try:
|
| 164 |
+
content = json.loads(message["content"])
|
| 165 |
+
except (json.JSONDecodeError, TypeError):
|
| 166 |
+
content = {}
|
| 167 |
+
|
| 168 |
+
# 网页
|
| 169 |
+
if message["content_type"] == "webpage":
|
| 170 |
+
if "value" in content:
|
| 171 |
+
for item in content["value"]:
|
| 172 |
+
results.append(
|
| 173 |
+
f"Title: {item['name']}\n"
|
| 174 |
+
f"URL: {item['url']}\n"
|
| 175 |
+
f"Description: {item['summary']}\n"
|
| 176 |
+
f"Published date: {item['datePublished']}\n"
|
| 177 |
+
f"Site name: {item['siteName']}"
|
| 178 |
+
)
|
| 179 |
+
elif (
|
| 180 |
+
message["content_type"] != "image"
|
| 181 |
+
and message["content"] != "{}"
|
| 182 |
+
):
|
| 183 |
+
results.append(message["content"])
|
| 184 |
+
|
| 185 |
+
if not results:
|
| 186 |
+
return "No results found."
|
| 187 |
+
|
| 188 |
+
return "\n\n".join(results)
|
| 189 |
+
|
| 190 |
+
except httpx.HTTPStatusError as e:
|
| 191 |
+
return f"Bocha AI Search API HTTP error occurred: {e.response.status_code} - {e.response.text}"
|
| 192 |
+
except httpx.RequestError as e:
|
| 193 |
+
return f"Error communicating with Bocha AI Search API: {str(e)}"
|
| 194 |
+
except Exception as e:
|
| 195 |
+
return f"Unexpected error: {str(e)}"
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
def main():
|
| 199 |
+
"""Initialize and run the MCP server."""
|
| 200 |
+
|
| 201 |
+
# Check for required environment variables
|
| 202 |
+
if "BOCHA_API_KEY" not in os.environ:
|
| 203 |
+
print(
|
| 204 |
+
"Error: BOCHA_API_KEY environment variable is required",
|
| 205 |
+
file=sys.stderr,
|
| 206 |
+
)
|
| 207 |
+
print(
|
| 208 |
+
"Get a Bocha API key from: " "https://open.bochaai.com",
|
| 209 |
+
file=sys.stderr,
|
| 210 |
+
)
|
| 211 |
+
sys.exit(1)
|
| 212 |
+
|
| 213 |
+
print("Starting Bocha Search MCP server...", file=sys.stderr)
|
| 214 |
+
|
| 215 |
+
server.run(transport="stdio")
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
if __name__ == "__main__":
|
| 219 |
+
main()
|
projects/ui/DeepCode/tools/code_implementation_server.py
ADDED
|
@@ -0,0 +1,1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 6 |
+
1. File read/write operations
|
| 7 |
+
2. Code execution and testing
|
| 8 |
+
3. Code search and analysis
|
| 9 |
+
4. Iterative improvement support
|
| 10 |
+
|
| 11 |
+
Usage:
|
| 12 |
+
python tools/code_implementation_server.py
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
import os
|
| 16 |
+
import subprocess
|
| 17 |
+
import json
|
| 18 |
+
import sys
|
| 19 |
+
import io
|
| 20 |
+
from pathlib import Path
|
| 21 |
+
import re
|
| 22 |
+
from typing import Dict, Any, List
|
| 23 |
+
import tempfile
|
| 24 |
+
import shutil
|
| 25 |
+
import logging
|
| 26 |
+
from datetime import datetime
|
| 27 |
+
|
| 28 |
+
# Set standard output encoding to UTF-8
|
| 29 |
+
if sys.stdout.encoding != "utf-8":
|
| 30 |
+
try:
|
| 31 |
+
if hasattr(sys.stdout, "reconfigure"):
|
| 32 |
+
sys.stdout.reconfigure(encoding="utf-8")
|
| 33 |
+
sys.stderr.reconfigure(encoding="utf-8")
|
| 34 |
+
else:
|
| 35 |
+
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding="utf-8")
|
| 36 |
+
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding="utf-8")
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Warning: Could not set UTF-8 encoding: {e}")
|
| 39 |
+
|
| 40 |
+
# Import MCP related modules
|
| 41 |
+
from mcp.server.fastmcp import FastMCP
|
| 42 |
+
|
| 43 |
+
# Setup logging
|
| 44 |
+
logging.basicConfig(level=logging.INFO)
|
| 45 |
+
logger = logging.getLogger(__name__)
|
| 46 |
+
|
| 47 |
+
# Create FastMCP server instance
|
| 48 |
+
mcp = FastMCP("code-implementation-server")
|
| 49 |
+
|
| 50 |
+
# Global variables: workspace directory and operation history
|
| 51 |
+
WORKSPACE_DIR = None
|
| 52 |
+
OPERATION_HISTORY = []
|
| 53 |
+
CURRENT_FILES = {}
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def initialize_workspace(workspace_dir: str = None):
|
| 57 |
+
"""
|
| 58 |
+
Initialize workspace
|
| 59 |
+
|
| 60 |
+
By default, the workspace will be set by the workflow via the set_workspace tool to:
|
| 61 |
+
{plan_file_parent}/generate_code
|
| 62 |
+
|
| 63 |
+
Args:
|
| 64 |
+
workspace_dir: Optional workspace directory path
|
| 65 |
+
"""
|
| 66 |
+
global WORKSPACE_DIR
|
| 67 |
+
if workspace_dir is None:
|
| 68 |
+
# Default to generate_code directory under current directory, but don't create immediately
|
| 69 |
+
# This default value will be overridden by workflow via set_workspace tool
|
| 70 |
+
WORKSPACE_DIR = Path.cwd() / "generate_code"
|
| 71 |
+
# logger.info(f"Workspace initialized (default value, will be overridden by workflow): {WORKSPACE_DIR}")
|
| 72 |
+
# logger.info("Note: Actual workspace will be set by workflow via set_workspace tool to {plan_file_parent}/generate_code")
|
| 73 |
+
else:
|
| 74 |
+
WORKSPACE_DIR = Path(workspace_dir).resolve()
|
| 75 |
+
# Only create when explicitly specified
|
| 76 |
+
WORKSPACE_DIR.mkdir(parents=True, exist_ok=True)
|
| 77 |
+
logger.info(f"Workspace initialized: {WORKSPACE_DIR}")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def ensure_workspace_exists():
|
| 81 |
+
"""Ensure workspace directory exists"""
|
| 82 |
+
global WORKSPACE_DIR
|
| 83 |
+
if WORKSPACE_DIR is None:
|
| 84 |
+
initialize_workspace()
|
| 85 |
+
|
| 86 |
+
# Create workspace directory (if it doesn't exist)
|
| 87 |
+
if not WORKSPACE_DIR.exists():
|
| 88 |
+
WORKSPACE_DIR.mkdir(parents=True, exist_ok=True)
|
| 89 |
+
logger.info(f"Workspace directory created: {WORKSPACE_DIR}")
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def validate_path(path: str) -> Path:
|
| 93 |
+
"""Validate if path is within workspace"""
|
| 94 |
+
if WORKSPACE_DIR is None:
|
| 95 |
+
initialize_workspace()
|
| 96 |
+
|
| 97 |
+
full_path = (WORKSPACE_DIR / path).resolve()
|
| 98 |
+
if not str(full_path).startswith(str(WORKSPACE_DIR)):
|
| 99 |
+
raise ValueError(f"Path {path} is outside workspace scope")
|
| 100 |
+
return full_path
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def log_operation(action: str, details: Dict[str, Any]):
|
| 104 |
+
"""Log operation history"""
|
| 105 |
+
OPERATION_HISTORY.append(
|
| 106 |
+
{"timestamp": datetime.now().isoformat(), "action": action, "details": details}
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
# ==================== File Operation Tools ====================
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
@mcp.tool()
|
| 114 |
+
async def read_file(
|
| 115 |
+
file_path: str, start_line: int = None, end_line: int = None
|
| 116 |
+
) -> str:
|
| 117 |
+
"""
|
| 118 |
+
Read file content, supports specifying line number range
|
| 119 |
+
|
| 120 |
+
Args:
|
| 121 |
+
file_path: File path, relative to workspace
|
| 122 |
+
start_line: Starting line number (1-based, optional)
|
| 123 |
+
end_line: Ending line number (1-based, optional)
|
| 124 |
+
|
| 125 |
+
Returns:
|
| 126 |
+
JSON string of file content or error message
|
| 127 |
+
"""
|
| 128 |
+
try:
|
| 129 |
+
full_path = validate_path(file_path)
|
| 130 |
+
|
| 131 |
+
if not full_path.exists():
|
| 132 |
+
result = {"status": "error", "message": f"File does not exist: {file_path}"}
|
| 133 |
+
log_operation(
|
| 134 |
+
"read_file_error", {"file_path": file_path, "error": "file_not_found"}
|
| 135 |
+
)
|
| 136 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 137 |
+
|
| 138 |
+
with open(full_path, "r", encoding="utf-8") as f:
|
| 139 |
+
lines = f.readlines()
|
| 140 |
+
|
| 141 |
+
# 处理行号范围
|
| 142 |
+
if start_line is not None or end_line is not None:
|
| 143 |
+
start_idx = (start_line - 1) if start_line else 0
|
| 144 |
+
end_idx = end_line if end_line else len(lines)
|
| 145 |
+
lines = lines[start_idx:end_idx]
|
| 146 |
+
|
| 147 |
+
content = "".join(lines)
|
| 148 |
+
|
| 149 |
+
result = {
|
| 150 |
+
"status": "success",
|
| 151 |
+
"content": content,
|
| 152 |
+
"file_path": file_path,
|
| 153 |
+
"total_lines": len(lines),
|
| 154 |
+
"size_bytes": len(content.encode("utf-8")),
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
log_operation(
|
| 158 |
+
"read_file",
|
| 159 |
+
{
|
| 160 |
+
"file_path": file_path,
|
| 161 |
+
"start_line": start_line,
|
| 162 |
+
"end_line": end_line,
|
| 163 |
+
"lines_read": len(lines),
|
| 164 |
+
},
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 168 |
+
|
| 169 |
+
except Exception as e:
|
| 170 |
+
result = {
|
| 171 |
+
"status": "error",
|
| 172 |
+
"message": f"Failed to read file: {str(e)}",
|
| 173 |
+
"file_path": file_path,
|
| 174 |
+
}
|
| 175 |
+
log_operation("read_file_error", {"file_path": file_path, "error": str(e)})
|
| 176 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
@mcp.tool()
|
| 180 |
+
async def read_multiple_files(file_requests: str, max_files: int = 5) -> str:
|
| 181 |
+
"""
|
| 182 |
+
Read multiple files in a single operation (for batch reading)
|
| 183 |
+
|
| 184 |
+
Args:
|
| 185 |
+
file_requests: JSON string with file requests, e.g.,
|
| 186 |
+
'{"file1.py": {}, "file2.py": {"start_line": 1, "end_line": 10}}'
|
| 187 |
+
or simple array: '["file1.py", "file2.py"]'
|
| 188 |
+
max_files: Maximum number of files to read in one operation (default: 5)
|
| 189 |
+
|
| 190 |
+
Returns:
|
| 191 |
+
JSON string of operation results for all files
|
| 192 |
+
"""
|
| 193 |
+
try:
|
| 194 |
+
# Parse the file requests
|
| 195 |
+
try:
|
| 196 |
+
requests_data = json.loads(file_requests)
|
| 197 |
+
except json.JSONDecodeError as e:
|
| 198 |
+
return json.dumps(
|
| 199 |
+
{
|
| 200 |
+
"status": "error",
|
| 201 |
+
"message": f"Invalid JSON format for file_requests: {str(e)}",
|
| 202 |
+
"operation_type": "multi_file",
|
| 203 |
+
"timestamp": datetime.now().isoformat(),
|
| 204 |
+
},
|
| 205 |
+
ensure_ascii=False,
|
| 206 |
+
indent=2,
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
# Normalize requests format
|
| 210 |
+
if isinstance(requests_data, list):
|
| 211 |
+
# Convert simple array to dict format
|
| 212 |
+
normalized_requests = {file_path: {} for file_path in requests_data}
|
| 213 |
+
elif isinstance(requests_data, dict):
|
| 214 |
+
normalized_requests = requests_data
|
| 215 |
+
else:
|
| 216 |
+
return json.dumps(
|
| 217 |
+
{
|
| 218 |
+
"status": "error",
|
| 219 |
+
"message": "file_requests must be a JSON object or array",
|
| 220 |
+
"operation_type": "multi_file",
|
| 221 |
+
"timestamp": datetime.now().isoformat(),
|
| 222 |
+
},
|
| 223 |
+
ensure_ascii=False,
|
| 224 |
+
indent=2,
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
# Validate input
|
| 228 |
+
if len(normalized_requests) == 0:
|
| 229 |
+
return json.dumps(
|
| 230 |
+
{
|
| 231 |
+
"status": "error",
|
| 232 |
+
"message": "No files provided for reading",
|
| 233 |
+
"operation_type": "multi_file",
|
| 234 |
+
"timestamp": datetime.now().isoformat(),
|
| 235 |
+
},
|
| 236 |
+
ensure_ascii=False,
|
| 237 |
+
indent=2,
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
if len(normalized_requests) > max_files:
|
| 241 |
+
return json.dumps(
|
| 242 |
+
{
|
| 243 |
+
"status": "error",
|
| 244 |
+
"message": f"Too many files provided ({len(normalized_requests)}), maximum is {max_files}",
|
| 245 |
+
"operation_type": "multi_file",
|
| 246 |
+
"timestamp": datetime.now().isoformat(),
|
| 247 |
+
},
|
| 248 |
+
ensure_ascii=False,
|
| 249 |
+
indent=2,
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
# Process each file
|
| 253 |
+
results = {
|
| 254 |
+
"status": "success",
|
| 255 |
+
"message": f"Successfully processed {len(normalized_requests)} files",
|
| 256 |
+
"operation_type": "multi_file",
|
| 257 |
+
"timestamp": datetime.now().isoformat(),
|
| 258 |
+
"files_processed": len(normalized_requests),
|
| 259 |
+
"files": {},
|
| 260 |
+
"summary": {
|
| 261 |
+
"successful": 0,
|
| 262 |
+
"failed": 0,
|
| 263 |
+
"total_size_bytes": 0,
|
| 264 |
+
"total_lines": 0,
|
| 265 |
+
"files_not_found": 0,
|
| 266 |
+
},
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
# Process each file individually
|
| 270 |
+
for file_path, options in normalized_requests.items():
|
| 271 |
+
try:
|
| 272 |
+
full_path = validate_path(file_path)
|
| 273 |
+
start_line = options.get("start_line")
|
| 274 |
+
end_line = options.get("end_line")
|
| 275 |
+
|
| 276 |
+
if not full_path.exists():
|
| 277 |
+
results["files"][file_path] = {
|
| 278 |
+
"status": "error",
|
| 279 |
+
"message": f"File does not exist: {file_path}",
|
| 280 |
+
"file_path": file_path,
|
| 281 |
+
"content": "",
|
| 282 |
+
"total_lines": 0,
|
| 283 |
+
"size_bytes": 0,
|
| 284 |
+
"start_line": start_line,
|
| 285 |
+
"end_line": end_line,
|
| 286 |
+
}
|
| 287 |
+
results["summary"]["failed"] += 1
|
| 288 |
+
results["summary"]["files_not_found"] += 1
|
| 289 |
+
continue
|
| 290 |
+
|
| 291 |
+
with open(full_path, "r", encoding="utf-8") as f:
|
| 292 |
+
lines = f.readlines()
|
| 293 |
+
|
| 294 |
+
# Handle line range
|
| 295 |
+
original_line_count = len(lines)
|
| 296 |
+
if start_line is not None or end_line is not None:
|
| 297 |
+
start_idx = (start_line - 1) if start_line else 0
|
| 298 |
+
end_idx = end_line if end_line else len(lines)
|
| 299 |
+
lines = lines[start_idx:end_idx]
|
| 300 |
+
|
| 301 |
+
content = "".join(lines)
|
| 302 |
+
size_bytes = len(content.encode("utf-8"))
|
| 303 |
+
lines_count = len(lines)
|
| 304 |
+
|
| 305 |
+
# Record individual file result
|
| 306 |
+
results["files"][file_path] = {
|
| 307 |
+
"status": "success",
|
| 308 |
+
"message": f"File read successfully: {file_path}",
|
| 309 |
+
"file_path": file_path,
|
| 310 |
+
"content": content,
|
| 311 |
+
"total_lines": lines_count,
|
| 312 |
+
"original_total_lines": original_line_count,
|
| 313 |
+
"size_bytes": size_bytes,
|
| 314 |
+
"start_line": start_line,
|
| 315 |
+
"end_line": end_line,
|
| 316 |
+
"line_range_applied": start_line is not None
|
| 317 |
+
or end_line is not None,
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
# Update summary
|
| 321 |
+
results["summary"]["successful"] += 1
|
| 322 |
+
results["summary"]["total_size_bytes"] += size_bytes
|
| 323 |
+
results["summary"]["total_lines"] += lines_count
|
| 324 |
+
|
| 325 |
+
# Log individual file operation
|
| 326 |
+
log_operation(
|
| 327 |
+
"read_file_multi",
|
| 328 |
+
{
|
| 329 |
+
"file_path": file_path,
|
| 330 |
+
"start_line": start_line,
|
| 331 |
+
"end_line": end_line,
|
| 332 |
+
"lines_read": lines_count,
|
| 333 |
+
"size_bytes": size_bytes,
|
| 334 |
+
"batch_operation": True,
|
| 335 |
+
},
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
except Exception as file_error:
|
| 339 |
+
# Record individual file error
|
| 340 |
+
results["files"][file_path] = {
|
| 341 |
+
"status": "error",
|
| 342 |
+
"message": f"Failed to read file: {str(file_error)}",
|
| 343 |
+
"file_path": file_path,
|
| 344 |
+
"content": "",
|
| 345 |
+
"total_lines": 0,
|
| 346 |
+
"size_bytes": 0,
|
| 347 |
+
"start_line": options.get("start_line"),
|
| 348 |
+
"end_line": options.get("end_line"),
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
results["summary"]["failed"] += 1
|
| 352 |
+
|
| 353 |
+
# Log individual file error
|
| 354 |
+
log_operation(
|
| 355 |
+
"read_file_multi_error",
|
| 356 |
+
{
|
| 357 |
+
"file_path": file_path,
|
| 358 |
+
"error": str(file_error),
|
| 359 |
+
"batch_operation": True,
|
| 360 |
+
},
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
+
# Determine overall status
|
| 364 |
+
if results["summary"]["failed"] > 0:
|
| 365 |
+
if results["summary"]["successful"] > 0:
|
| 366 |
+
results["status"] = "partial_success"
|
| 367 |
+
results["message"] = (
|
| 368 |
+
f"Read {results['summary']['successful']} files successfully, {results['summary']['failed']} failed"
|
| 369 |
+
)
|
| 370 |
+
else:
|
| 371 |
+
results["status"] = "failed"
|
| 372 |
+
results["message"] = (
|
| 373 |
+
f"All {results['summary']['failed']} files failed to read"
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
+
# Log overall operation
|
| 377 |
+
log_operation(
|
| 378 |
+
"read_multiple_files",
|
| 379 |
+
{
|
| 380 |
+
"files_count": len(normalized_requests),
|
| 381 |
+
"successful": results["summary"]["successful"],
|
| 382 |
+
"failed": results["summary"]["failed"],
|
| 383 |
+
"total_size_bytes": results["summary"]["total_size_bytes"],
|
| 384 |
+
"status": results["status"],
|
| 385 |
+
},
|
| 386 |
+
)
|
| 387 |
+
|
| 388 |
+
return json.dumps(results, ensure_ascii=False, indent=2)
|
| 389 |
+
|
| 390 |
+
except Exception as e:
|
| 391 |
+
result = {
|
| 392 |
+
"status": "error",
|
| 393 |
+
"message": f"Failed to read multiple files: {str(e)}",
|
| 394 |
+
"operation_type": "multi_file",
|
| 395 |
+
"timestamp": datetime.now().isoformat(),
|
| 396 |
+
"files_processed": 0,
|
| 397 |
+
}
|
| 398 |
+
log_operation("read_multiple_files_error", {"error": str(e)})
|
| 399 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
@mcp.tool()
|
| 403 |
+
async def write_file(
|
| 404 |
+
file_path: str, content: str, create_dirs: bool = True, create_backup: bool = False
|
| 405 |
+
) -> str:
|
| 406 |
+
"""
|
| 407 |
+
Write content to file
|
| 408 |
+
|
| 409 |
+
Args:
|
| 410 |
+
file_path: File path, relative to workspace
|
| 411 |
+
content: Content to write to file
|
| 412 |
+
create_dirs: Whether to create directories if they don't exist
|
| 413 |
+
create_backup: Whether to create backup file if file already exists
|
| 414 |
+
|
| 415 |
+
Returns:
|
| 416 |
+
JSON string of operation result
|
| 417 |
+
"""
|
| 418 |
+
try:
|
| 419 |
+
full_path = validate_path(file_path)
|
| 420 |
+
|
| 421 |
+
# Create directories (if needed)
|
| 422 |
+
if create_dirs:
|
| 423 |
+
full_path.parent.mkdir(parents=True, exist_ok=True)
|
| 424 |
+
|
| 425 |
+
# Backup existing file (only when explicitly requested)
|
| 426 |
+
backup_created = False
|
| 427 |
+
if full_path.exists() and create_backup:
|
| 428 |
+
backup_path = full_path.with_suffix(full_path.suffix + ".backup")
|
| 429 |
+
shutil.copy2(full_path, backup_path)
|
| 430 |
+
backup_created = True
|
| 431 |
+
|
| 432 |
+
# Write file
|
| 433 |
+
with open(full_path, "w", encoding="utf-8") as f:
|
| 434 |
+
f.write(content)
|
| 435 |
+
|
| 436 |
+
# Update current file record
|
| 437 |
+
CURRENT_FILES[file_path] = {
|
| 438 |
+
"last_modified": datetime.now().isoformat(),
|
| 439 |
+
"size_bytes": len(content.encode("utf-8")),
|
| 440 |
+
"lines": len(content.split("\n")),
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
result = {
|
| 444 |
+
"status": "success",
|
| 445 |
+
"message": f"File written successfully: {file_path}",
|
| 446 |
+
"file_path": file_path,
|
| 447 |
+
"size_bytes": len(content.encode("utf-8")),
|
| 448 |
+
"lines_written": len(content.split("\n")),
|
| 449 |
+
"backup_created": backup_created,
|
| 450 |
+
}
|
| 451 |
+
|
| 452 |
+
log_operation(
|
| 453 |
+
"write_file",
|
| 454 |
+
{
|
| 455 |
+
"file_path": file_path,
|
| 456 |
+
"size_bytes": len(content.encode("utf-8")),
|
| 457 |
+
"lines": len(content.split("\n")),
|
| 458 |
+
"backup_created": backup_created,
|
| 459 |
+
},
|
| 460 |
+
)
|
| 461 |
+
|
| 462 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 463 |
+
|
| 464 |
+
except Exception as e:
|
| 465 |
+
result = {
|
| 466 |
+
"status": "error",
|
| 467 |
+
"message": f"Failed to write file: {str(e)}",
|
| 468 |
+
"file_path": file_path,
|
| 469 |
+
}
|
| 470 |
+
log_operation("write_file_error", {"file_path": file_path, "error": str(e)})
|
| 471 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 472 |
+
|
| 473 |
+
|
| 474 |
+
@mcp.tool()
|
| 475 |
+
async def write_multiple_files(
|
| 476 |
+
file_implementations: str,
|
| 477 |
+
create_dirs: bool = True,
|
| 478 |
+
create_backup: bool = False,
|
| 479 |
+
max_files: int = 5,
|
| 480 |
+
) -> str:
|
| 481 |
+
"""
|
| 482 |
+
Write multiple files in a single operation (for batch implementation)
|
| 483 |
+
|
| 484 |
+
Args:
|
| 485 |
+
file_implementations: JSON string mapping file paths to content, e.g.,
|
| 486 |
+
'{"file1.py": "content1", "file2.py": "content2"}'
|
| 487 |
+
create_dirs: Whether to create directories if they don't exist
|
| 488 |
+
create_backup: Whether to create backup files if they already exist
|
| 489 |
+
max_files: Maximum number of files to write in one operation (default: 5)
|
| 490 |
+
|
| 491 |
+
Returns:
|
| 492 |
+
JSON string of operation results for all files
|
| 493 |
+
"""
|
| 494 |
+
try:
|
| 495 |
+
# Parse the file implementations
|
| 496 |
+
try:
|
| 497 |
+
files_dict = json.loads(file_implementations)
|
| 498 |
+
except json.JSONDecodeError as e:
|
| 499 |
+
return json.dumps(
|
| 500 |
+
{
|
| 501 |
+
"status": "error",
|
| 502 |
+
"message": f"Invalid JSON format for file_implementations: {str(e)}",
|
| 503 |
+
"operation_type": "multi_file",
|
| 504 |
+
"timestamp": datetime.now().isoformat(),
|
| 505 |
+
},
|
| 506 |
+
ensure_ascii=False,
|
| 507 |
+
indent=2,
|
| 508 |
+
)
|
| 509 |
+
|
| 510 |
+
# Validate input
|
| 511 |
+
if not isinstance(files_dict, dict):
|
| 512 |
+
return json.dumps(
|
| 513 |
+
{
|
| 514 |
+
"status": "error",
|
| 515 |
+
"message": "file_implementations must be a JSON object mapping file paths to content",
|
| 516 |
+
"operation_type": "multi_file",
|
| 517 |
+
"timestamp": datetime.now().isoformat(),
|
| 518 |
+
},
|
| 519 |
+
ensure_ascii=False,
|
| 520 |
+
indent=2,
|
| 521 |
+
)
|
| 522 |
+
|
| 523 |
+
if len(files_dict) == 0:
|
| 524 |
+
return json.dumps(
|
| 525 |
+
{
|
| 526 |
+
"status": "error",
|
| 527 |
+
"message": "No files provided for writing",
|
| 528 |
+
"operation_type": "multi_file",
|
| 529 |
+
"timestamp": datetime.now().isoformat(),
|
| 530 |
+
},
|
| 531 |
+
ensure_ascii=False,
|
| 532 |
+
indent=2,
|
| 533 |
+
)
|
| 534 |
+
|
| 535 |
+
if len(files_dict) > max_files:
|
| 536 |
+
return json.dumps(
|
| 537 |
+
{
|
| 538 |
+
"status": "error",
|
| 539 |
+
"message": f"Too many files provided ({len(files_dict)}), maximum is {max_files}",
|
| 540 |
+
"operation_type": "multi_file",
|
| 541 |
+
"timestamp": datetime.now().isoformat(),
|
| 542 |
+
},
|
| 543 |
+
ensure_ascii=False,
|
| 544 |
+
indent=2,
|
| 545 |
+
)
|
| 546 |
+
|
| 547 |
+
# Process each file
|
| 548 |
+
results = {
|
| 549 |
+
"status": "success",
|
| 550 |
+
"message": f"Successfully processed {len(files_dict)} files",
|
| 551 |
+
"operation_type": "multi_file",
|
| 552 |
+
"timestamp": datetime.now().isoformat(),
|
| 553 |
+
"files_processed": len(files_dict),
|
| 554 |
+
"files": {},
|
| 555 |
+
"summary": {
|
| 556 |
+
"successful": 0,
|
| 557 |
+
"failed": 0,
|
| 558 |
+
"total_size_bytes": 0,
|
| 559 |
+
"total_lines": 0,
|
| 560 |
+
"backups_created": 0,
|
| 561 |
+
},
|
| 562 |
+
}
|
| 563 |
+
|
| 564 |
+
# Process each file individually
|
| 565 |
+
for file_path, content in files_dict.items():
|
| 566 |
+
try:
|
| 567 |
+
full_path = validate_path(file_path)
|
| 568 |
+
|
| 569 |
+
# Create directories (if needed)
|
| 570 |
+
if create_dirs:
|
| 571 |
+
full_path.parent.mkdir(parents=True, exist_ok=True)
|
| 572 |
+
|
| 573 |
+
# Backup existing file (only when explicitly requested)
|
| 574 |
+
backup_created = False
|
| 575 |
+
if full_path.exists() and create_backup:
|
| 576 |
+
backup_path = full_path.with_suffix(full_path.suffix + ".backup")
|
| 577 |
+
shutil.copy2(full_path, backup_path)
|
| 578 |
+
backup_created = True
|
| 579 |
+
results["summary"]["backups_created"] += 1
|
| 580 |
+
|
| 581 |
+
# Write file
|
| 582 |
+
with open(full_path, "w", encoding="utf-8") as f:
|
| 583 |
+
f.write(content)
|
| 584 |
+
|
| 585 |
+
# Calculate file metrics
|
| 586 |
+
size_bytes = len(content.encode("utf-8"))
|
| 587 |
+
lines_count = len(content.split("\n"))
|
| 588 |
+
|
| 589 |
+
# Update current file record
|
| 590 |
+
CURRENT_FILES[file_path] = {
|
| 591 |
+
"last_modified": datetime.now().isoformat(),
|
| 592 |
+
"size_bytes": size_bytes,
|
| 593 |
+
"lines": lines_count,
|
| 594 |
+
}
|
| 595 |
+
|
| 596 |
+
# Record individual file result
|
| 597 |
+
results["files"][file_path] = {
|
| 598 |
+
"status": "success",
|
| 599 |
+
"message": f"File written successfully: {file_path}",
|
| 600 |
+
"size_bytes": size_bytes,
|
| 601 |
+
"lines_written": lines_count,
|
| 602 |
+
"backup_created": backup_created,
|
| 603 |
+
}
|
| 604 |
+
|
| 605 |
+
# Update summary
|
| 606 |
+
results["summary"]["successful"] += 1
|
| 607 |
+
results["summary"]["total_size_bytes"] += size_bytes
|
| 608 |
+
results["summary"]["total_lines"] += lines_count
|
| 609 |
+
|
| 610 |
+
# Log individual file operation
|
| 611 |
+
log_operation(
|
| 612 |
+
"write_file_multi",
|
| 613 |
+
{
|
| 614 |
+
"file_path": file_path,
|
| 615 |
+
"size_bytes": size_bytes,
|
| 616 |
+
"lines": lines_count,
|
| 617 |
+
"backup_created": backup_created,
|
| 618 |
+
"batch_operation": True,
|
| 619 |
+
},
|
| 620 |
+
)
|
| 621 |
+
|
| 622 |
+
except Exception as file_error:
|
| 623 |
+
# Record individual file error
|
| 624 |
+
results["files"][file_path] = {
|
| 625 |
+
"status": "error",
|
| 626 |
+
"message": f"Failed to write file: {str(file_error)}",
|
| 627 |
+
"size_bytes": 0,
|
| 628 |
+
"lines_written": 0,
|
| 629 |
+
"backup_created": False,
|
| 630 |
+
}
|
| 631 |
+
|
| 632 |
+
results["summary"]["failed"] += 1
|
| 633 |
+
|
| 634 |
+
# Log individual file error
|
| 635 |
+
log_operation(
|
| 636 |
+
"write_file_multi_error",
|
| 637 |
+
{
|
| 638 |
+
"file_path": file_path,
|
| 639 |
+
"error": str(file_error),
|
| 640 |
+
"batch_operation": True,
|
| 641 |
+
},
|
| 642 |
+
)
|
| 643 |
+
|
| 644 |
+
# Determine overall status
|
| 645 |
+
if results["summary"]["failed"] > 0:
|
| 646 |
+
if results["summary"]["successful"] > 0:
|
| 647 |
+
results["status"] = "partial_success"
|
| 648 |
+
results["message"] = (
|
| 649 |
+
f"Processed {results['summary']['successful']} files successfully, {results['summary']['failed']} failed"
|
| 650 |
+
)
|
| 651 |
+
else:
|
| 652 |
+
results["status"] = "failed"
|
| 653 |
+
results["message"] = (
|
| 654 |
+
f"All {results['summary']['failed']} files failed to write"
|
| 655 |
+
)
|
| 656 |
+
|
| 657 |
+
# Log overall operation
|
| 658 |
+
log_operation(
|
| 659 |
+
"write_multiple_files",
|
| 660 |
+
{
|
| 661 |
+
"files_count": len(files_dict),
|
| 662 |
+
"successful": results["summary"]["successful"],
|
| 663 |
+
"failed": results["summary"]["failed"],
|
| 664 |
+
"total_size_bytes": results["summary"]["total_size_bytes"],
|
| 665 |
+
"status": results["status"],
|
| 666 |
+
},
|
| 667 |
+
)
|
| 668 |
+
|
| 669 |
+
return json.dumps(results, ensure_ascii=False, indent=2)
|
| 670 |
+
|
| 671 |
+
except Exception as e:
|
| 672 |
+
result = {
|
| 673 |
+
"status": "error",
|
| 674 |
+
"message": f"Failed to write multiple files: {str(e)}",
|
| 675 |
+
"operation_type": "multi_file",
|
| 676 |
+
"timestamp": datetime.now().isoformat(),
|
| 677 |
+
"files_processed": 0,
|
| 678 |
+
}
|
| 679 |
+
log_operation("write_multiple_files_error", {"error": str(e)})
|
| 680 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 681 |
+
|
| 682 |
+
|
| 683 |
+
# ==================== Code Execution Tools ====================
|
| 684 |
+
|
| 685 |
+
|
| 686 |
+
@mcp.tool()
|
| 687 |
+
async def execute_python(code: str, timeout: int = 30) -> str:
|
| 688 |
+
"""
|
| 689 |
+
Execute Python code and return output
|
| 690 |
+
|
| 691 |
+
Args:
|
| 692 |
+
code: Python code to execute
|
| 693 |
+
timeout: Timeout in seconds
|
| 694 |
+
|
| 695 |
+
Returns:
|
| 696 |
+
JSON string of execution result
|
| 697 |
+
"""
|
| 698 |
+
try:
|
| 699 |
+
# Create temporary file
|
| 700 |
+
with tempfile.NamedTemporaryFile(
|
| 701 |
+
mode="w", suffix=".py", delete=False, encoding="utf-8"
|
| 702 |
+
) as f:
|
| 703 |
+
f.write(code)
|
| 704 |
+
temp_file = f.name
|
| 705 |
+
|
| 706 |
+
try:
|
| 707 |
+
# Ensure workspace directory exists
|
| 708 |
+
ensure_workspace_exists()
|
| 709 |
+
|
| 710 |
+
# Execute Python code
|
| 711 |
+
result = subprocess.run(
|
| 712 |
+
[sys.executable, temp_file],
|
| 713 |
+
cwd=WORKSPACE_DIR,
|
| 714 |
+
capture_output=True,
|
| 715 |
+
text=True,
|
| 716 |
+
timeout=timeout,
|
| 717 |
+
encoding="utf-8",
|
| 718 |
+
)
|
| 719 |
+
|
| 720 |
+
execution_result = {
|
| 721 |
+
"status": "success" if result.returncode == 0 else "error",
|
| 722 |
+
"return_code": result.returncode,
|
| 723 |
+
"stdout": result.stdout,
|
| 724 |
+
"stderr": result.stderr,
|
| 725 |
+
"timeout": timeout,
|
| 726 |
+
}
|
| 727 |
+
|
| 728 |
+
if result.returncode != 0:
|
| 729 |
+
execution_result["message"] = "Python code execution failed"
|
| 730 |
+
else:
|
| 731 |
+
execution_result["message"] = "Python code execution successful"
|
| 732 |
+
|
| 733 |
+
log_operation(
|
| 734 |
+
"execute_python",
|
| 735 |
+
{
|
| 736 |
+
"return_code": result.returncode,
|
| 737 |
+
"stdout_length": len(result.stdout),
|
| 738 |
+
"stderr_length": len(result.stderr),
|
| 739 |
+
},
|
| 740 |
+
)
|
| 741 |
+
|
| 742 |
+
return json.dumps(execution_result, ensure_ascii=False, indent=2)
|
| 743 |
+
|
| 744 |
+
finally:
|
| 745 |
+
# Clean up temporary file
|
| 746 |
+
os.unlink(temp_file)
|
| 747 |
+
|
| 748 |
+
except subprocess.TimeoutExpired:
|
| 749 |
+
result = {
|
| 750 |
+
"status": "error",
|
| 751 |
+
"message": f"Python code execution timeout ({timeout}秒)",
|
| 752 |
+
"timeout": timeout,
|
| 753 |
+
}
|
| 754 |
+
log_operation("execute_python_timeout", {"timeout": timeout})
|
| 755 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 756 |
+
|
| 757 |
+
except Exception as e:
|
| 758 |
+
result = {
|
| 759 |
+
"status": "error",
|
| 760 |
+
"message": f"Python code execution failed: {str(e)}",
|
| 761 |
+
}
|
| 762 |
+
log_operation("execute_python_error", {"error": str(e)})
|
| 763 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 764 |
+
|
| 765 |
+
|
| 766 |
+
@mcp.tool()
|
| 767 |
+
async def execute_bash(command: str, timeout: int = 30) -> str:
|
| 768 |
+
"""
|
| 769 |
+
Execute bash command
|
| 770 |
+
|
| 771 |
+
Args:
|
| 772 |
+
command: Bash command to execute
|
| 773 |
+
timeout: Timeout in seconds
|
| 774 |
+
|
| 775 |
+
Returns:
|
| 776 |
+
JSON string of execution result
|
| 777 |
+
"""
|
| 778 |
+
try:
|
| 779 |
+
# 安全检查:禁止危险命令
|
| 780 |
+
dangerous_commands = ["rm -rf", "sudo", "chmod 777", "mkfs", "dd if="]
|
| 781 |
+
if any(dangerous in command.lower() for dangerous in dangerous_commands):
|
| 782 |
+
result = {
|
| 783 |
+
"status": "error",
|
| 784 |
+
"message": f"Dangerous command execution prohibited: {command}",
|
| 785 |
+
}
|
| 786 |
+
log_operation(
|
| 787 |
+
"execute_bash_blocked",
|
| 788 |
+
{"command": command, "reason": "dangerous_command"},
|
| 789 |
+
)
|
| 790 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 791 |
+
|
| 792 |
+
# Ensure workspace directory exists
|
| 793 |
+
ensure_workspace_exists()
|
| 794 |
+
|
| 795 |
+
# Execute command
|
| 796 |
+
result = subprocess.run(
|
| 797 |
+
command,
|
| 798 |
+
shell=True,
|
| 799 |
+
cwd=WORKSPACE_DIR,
|
| 800 |
+
capture_output=True,
|
| 801 |
+
text=True,
|
| 802 |
+
timeout=timeout,
|
| 803 |
+
encoding="utf-8",
|
| 804 |
+
)
|
| 805 |
+
|
| 806 |
+
execution_result = {
|
| 807 |
+
"status": "success" if result.returncode == 0 else "error",
|
| 808 |
+
"return_code": result.returncode,
|
| 809 |
+
"stdout": result.stdout,
|
| 810 |
+
"stderr": result.stderr,
|
| 811 |
+
"command": command,
|
| 812 |
+
"timeout": timeout,
|
| 813 |
+
}
|
| 814 |
+
|
| 815 |
+
if result.returncode != 0:
|
| 816 |
+
execution_result["message"] = "Bash command execution failed"
|
| 817 |
+
else:
|
| 818 |
+
execution_result["message"] = "Bash command execution successful"
|
| 819 |
+
|
| 820 |
+
log_operation(
|
| 821 |
+
"execute_bash",
|
| 822 |
+
{
|
| 823 |
+
"command": command,
|
| 824 |
+
"return_code": result.returncode,
|
| 825 |
+
"stdout_length": len(result.stdout),
|
| 826 |
+
"stderr_length": len(result.stderr),
|
| 827 |
+
},
|
| 828 |
+
)
|
| 829 |
+
|
| 830 |
+
return json.dumps(execution_result, ensure_ascii=False, indent=2)
|
| 831 |
+
|
| 832 |
+
except subprocess.TimeoutExpired:
|
| 833 |
+
result = {
|
| 834 |
+
"status": "error",
|
| 835 |
+
"message": f"Bash command execution timeout ({timeout} seconds)",
|
| 836 |
+
"command": command,
|
| 837 |
+
"timeout": timeout,
|
| 838 |
+
}
|
| 839 |
+
log_operation("execute_bash_timeout", {"command": command, "timeout": timeout})
|
| 840 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 841 |
+
|
| 842 |
+
except Exception as e:
|
| 843 |
+
result = {
|
| 844 |
+
"status": "error",
|
| 845 |
+
"message": f"Failed to execute bash command: {str(e)}",
|
| 846 |
+
"command": command,
|
| 847 |
+
}
|
| 848 |
+
log_operation("execute_bash_error", {"command": command, "error": str(e)})
|
| 849 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 850 |
+
|
| 851 |
+
|
| 852 |
+
@mcp.tool()
|
| 853 |
+
async def read_code_mem(file_paths: List[str]) -> str:
|
| 854 |
+
"""
|
| 855 |
+
Check if file summaries exist in implement_code_summary.md for multiple files
|
| 856 |
+
|
| 857 |
+
Args:
|
| 858 |
+
file_paths: List of file paths to check for summary information in implement_code_summary.md
|
| 859 |
+
|
| 860 |
+
Returns:
|
| 861 |
+
Summary information for all requested files if available
|
| 862 |
+
"""
|
| 863 |
+
try:
|
| 864 |
+
if not file_paths or not isinstance(file_paths, list):
|
| 865 |
+
result = {
|
| 866 |
+
"status": "error",
|
| 867 |
+
"message": "file_paths parameter is required and must be a list",
|
| 868 |
+
}
|
| 869 |
+
log_operation(
|
| 870 |
+
"read_code_mem_error", {"error": "missing_or_invalid_file_paths"}
|
| 871 |
+
)
|
| 872 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 873 |
+
|
| 874 |
+
# Remove duplicates while preserving order
|
| 875 |
+
unique_file_paths = list(dict.fromkeys(file_paths))
|
| 876 |
+
|
| 877 |
+
# Ensure workspace exists
|
| 878 |
+
ensure_workspace_exists()
|
| 879 |
+
|
| 880 |
+
# Look for implement_code_summary.md in the workspace
|
| 881 |
+
current_path = Path(WORKSPACE_DIR)
|
| 882 |
+
summary_file_path = current_path.parent / "implement_code_summary.md"
|
| 883 |
+
|
| 884 |
+
if not summary_file_path.exists():
|
| 885 |
+
result = {
|
| 886 |
+
"status": "no_summary",
|
| 887 |
+
"file_paths": unique_file_paths,
|
| 888 |
+
"message": "No summary file found.",
|
| 889 |
+
"results": [],
|
| 890 |
+
}
|
| 891 |
+
log_operation(
|
| 892 |
+
"read_code_mem",
|
| 893 |
+
{"file_paths": unique_file_paths, "status": "no_summary_file"},
|
| 894 |
+
)
|
| 895 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 896 |
+
|
| 897 |
+
# Read the summary file
|
| 898 |
+
with open(summary_file_path, "r", encoding="utf-8") as f:
|
| 899 |
+
summary_content = f.read()
|
| 900 |
+
|
| 901 |
+
if not summary_content.strip():
|
| 902 |
+
result = {
|
| 903 |
+
"status": "no_summary",
|
| 904 |
+
"file_paths": unique_file_paths,
|
| 905 |
+
"message": "Summary file is empty.",
|
| 906 |
+
"results": [],
|
| 907 |
+
}
|
| 908 |
+
log_operation(
|
| 909 |
+
"read_code_mem",
|
| 910 |
+
{"file_paths": unique_file_paths, "status": "empty_summary"},
|
| 911 |
+
)
|
| 912 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 913 |
+
|
| 914 |
+
# Process each file path and collect results
|
| 915 |
+
results = []
|
| 916 |
+
summaries_found = 0
|
| 917 |
+
|
| 918 |
+
for file_path in unique_file_paths:
|
| 919 |
+
# Extract file-specific section from summary
|
| 920 |
+
file_section = _extract_file_section_from_summary(
|
| 921 |
+
summary_content, file_path
|
| 922 |
+
)
|
| 923 |
+
|
| 924 |
+
if file_section:
|
| 925 |
+
file_result = {
|
| 926 |
+
"file_path": file_path,
|
| 927 |
+
"status": "summary_found",
|
| 928 |
+
"summary_content": file_section,
|
| 929 |
+
"message": f"Summary information found for {file_path}",
|
| 930 |
+
}
|
| 931 |
+
summaries_found += 1
|
| 932 |
+
else:
|
| 933 |
+
file_result = {
|
| 934 |
+
"file_path": file_path,
|
| 935 |
+
"status": "no_summary",
|
| 936 |
+
"summary_content": None,
|
| 937 |
+
"message": f"No summary found for {file_path}",
|
| 938 |
+
}
|
| 939 |
+
|
| 940 |
+
results.append(file_result)
|
| 941 |
+
|
| 942 |
+
# Determine overall status
|
| 943 |
+
if summaries_found == len(unique_file_paths):
|
| 944 |
+
overall_status = "all_summaries_found"
|
| 945 |
+
elif summaries_found > 0:
|
| 946 |
+
overall_status = "partial_summaries_found"
|
| 947 |
+
else:
|
| 948 |
+
overall_status = "no_summaries_found"
|
| 949 |
+
|
| 950 |
+
result = {
|
| 951 |
+
"status": overall_status,
|
| 952 |
+
"file_paths": unique_file_paths,
|
| 953 |
+
"total_requested": len(unique_file_paths),
|
| 954 |
+
"summaries_found": summaries_found,
|
| 955 |
+
"message": f"Found summaries for {summaries_found}/{len(unique_file_paths)} files",
|
| 956 |
+
"results": results,
|
| 957 |
+
}
|
| 958 |
+
|
| 959 |
+
log_operation(
|
| 960 |
+
"read_code_mem",
|
| 961 |
+
{
|
| 962 |
+
"file_paths": unique_file_paths,
|
| 963 |
+
"status": overall_status,
|
| 964 |
+
"total_requested": len(unique_file_paths),
|
| 965 |
+
"summaries_found": summaries_found,
|
| 966 |
+
},
|
| 967 |
+
)
|
| 968 |
+
|
| 969 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 970 |
+
|
| 971 |
+
except Exception as e:
|
| 972 |
+
result = {
|
| 973 |
+
"status": "error",
|
| 974 |
+
"message": f"Failed to check code memory: {str(e)}",
|
| 975 |
+
"file_paths": file_paths
|
| 976 |
+
if isinstance(file_paths, list)
|
| 977 |
+
else [str(file_paths)],
|
| 978 |
+
"results": [],
|
| 979 |
+
}
|
| 980 |
+
log_operation(
|
| 981 |
+
"read_code_mem_error", {"file_paths": file_paths, "error": str(e)}
|
| 982 |
+
)
|
| 983 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 984 |
+
|
| 985 |
+
|
| 986 |
+
def _extract_file_section_from_summary(
|
| 987 |
+
summary_content: str, target_file_path: str
|
| 988 |
+
) -> str:
|
| 989 |
+
"""
|
| 990 |
+
Extract the specific section for a file from the summary content
|
| 991 |
+
|
| 992 |
+
Args:
|
| 993 |
+
summary_content: Full summary content
|
| 994 |
+
target_file_path: Path of the target file
|
| 995 |
+
|
| 996 |
+
Returns:
|
| 997 |
+
File-specific section or None if not found
|
| 998 |
+
"""
|
| 999 |
+
import re
|
| 1000 |
+
|
| 1001 |
+
# Normalize the target path for comparison
|
| 1002 |
+
normalized_target = _normalize_file_path(target_file_path)
|
| 1003 |
+
|
| 1004 |
+
# Pattern to match implementation sections with separator lines
|
| 1005 |
+
section_pattern = r"={80}\s*\n## IMPLEMENTATION File ([^;]+); ROUND \d+\s*\n={80}(.*?)(?=\n={80}|\Z)"
|
| 1006 |
+
|
| 1007 |
+
matches = re.findall(section_pattern, summary_content, re.DOTALL)
|
| 1008 |
+
|
| 1009 |
+
for file_path_in_summary, section_content in matches:
|
| 1010 |
+
file_path_in_summary = file_path_in_summary.strip()
|
| 1011 |
+
section_content = section_content.strip()
|
| 1012 |
+
|
| 1013 |
+
# Normalize the path from summary for comparison
|
| 1014 |
+
normalized_summary_path = _normalize_file_path(file_path_in_summary)
|
| 1015 |
+
|
| 1016 |
+
# Check if paths match using multiple strategies
|
| 1017 |
+
if _paths_match(
|
| 1018 |
+
normalized_target,
|
| 1019 |
+
normalized_summary_path,
|
| 1020 |
+
target_file_path,
|
| 1021 |
+
file_path_in_summary,
|
| 1022 |
+
):
|
| 1023 |
+
# Return the complete section with proper formatting
|
| 1024 |
+
file_section = f"""================================================================================
|
| 1025 |
+
## IMPLEMENTATION File {file_path_in_summary}; ROUND [X]
|
| 1026 |
+
================================================================================
|
| 1027 |
+
|
| 1028 |
+
{section_content}
|
| 1029 |
+
|
| 1030 |
+
---
|
| 1031 |
+
*Extracted from implement_code_summary.md*"""
|
| 1032 |
+
return file_section
|
| 1033 |
+
|
| 1034 |
+
# If no section-based match, try alternative parsing method
|
| 1035 |
+
return _extract_file_section_alternative(summary_content, target_file_path)
|
| 1036 |
+
|
| 1037 |
+
|
| 1038 |
+
def _normalize_file_path(file_path: str) -> str:
|
| 1039 |
+
"""Normalize file path for comparison"""
|
| 1040 |
+
# Remove leading/trailing slashes and convert to lowercase
|
| 1041 |
+
normalized = file_path.strip("/").lower()
|
| 1042 |
+
# Replace backslashes with forward slashes
|
| 1043 |
+
normalized = normalized.replace("\\", "/")
|
| 1044 |
+
|
| 1045 |
+
# Remove common prefixes to make matching more flexible
|
| 1046 |
+
common_prefixes = ["src/", "./src/", "./", "core/", "lib/", "main/"]
|
| 1047 |
+
for prefix in common_prefixes:
|
| 1048 |
+
if normalized.startswith(prefix):
|
| 1049 |
+
normalized = normalized[len(prefix) :]
|
| 1050 |
+
break
|
| 1051 |
+
|
| 1052 |
+
return normalized
|
| 1053 |
+
|
| 1054 |
+
|
| 1055 |
+
def _paths_match(
|
| 1056 |
+
normalized_target: str,
|
| 1057 |
+
normalized_summary: str,
|
| 1058 |
+
original_target: str,
|
| 1059 |
+
original_summary: str,
|
| 1060 |
+
) -> bool:
|
| 1061 |
+
"""Check if two file paths match using multiple strategies"""
|
| 1062 |
+
|
| 1063 |
+
# Strategy 1: Exact normalized match
|
| 1064 |
+
if normalized_target == normalized_summary:
|
| 1065 |
+
return True
|
| 1066 |
+
|
| 1067 |
+
# Strategy 2: Basename match (filename only)
|
| 1068 |
+
target_basename = os.path.basename(original_target)
|
| 1069 |
+
summary_basename = os.path.basename(original_summary)
|
| 1070 |
+
if target_basename == summary_basename and len(target_basename) > 4:
|
| 1071 |
+
return True
|
| 1072 |
+
|
| 1073 |
+
# Strategy 3: Suffix match (remove common prefixes and compare)
|
| 1074 |
+
target_suffix = _remove_common_prefixes(normalized_target)
|
| 1075 |
+
summary_suffix = _remove_common_prefixes(normalized_summary)
|
| 1076 |
+
if target_suffix == summary_suffix:
|
| 1077 |
+
return True
|
| 1078 |
+
|
| 1079 |
+
# Strategy 4: Ends with match
|
| 1080 |
+
if normalized_target.endswith(normalized_summary) or normalized_summary.endswith(
|
| 1081 |
+
normalized_target
|
| 1082 |
+
):
|
| 1083 |
+
return True
|
| 1084 |
+
|
| 1085 |
+
# Strategy 5: Contains match for longer paths
|
| 1086 |
+
if len(normalized_target) > 10 and normalized_target in normalized_summary:
|
| 1087 |
+
return True
|
| 1088 |
+
if len(normalized_summary) > 10 and normalized_summary in normalized_target:
|
| 1089 |
+
return True
|
| 1090 |
+
|
| 1091 |
+
return False
|
| 1092 |
+
|
| 1093 |
+
|
| 1094 |
+
def _remove_common_prefixes(file_path: str) -> str:
|
| 1095 |
+
"""Remove common prefixes from file path"""
|
| 1096 |
+
prefixes_to_remove = ["src/", "core/", "./", "lib/", "main/"]
|
| 1097 |
+
path = file_path
|
| 1098 |
+
|
| 1099 |
+
for prefix in prefixes_to_remove:
|
| 1100 |
+
if path.startswith(prefix):
|
| 1101 |
+
path = path[len(prefix) :]
|
| 1102 |
+
|
| 1103 |
+
return path
|
| 1104 |
+
|
| 1105 |
+
|
| 1106 |
+
def _extract_file_section_alternative(
|
| 1107 |
+
summary_content: str, target_file_path: str
|
| 1108 |
+
) -> str:
|
| 1109 |
+
"""Alternative method to extract file section using simpler pattern matching"""
|
| 1110 |
+
|
| 1111 |
+
# Get the basename for fallback matching
|
| 1112 |
+
target_basename = os.path.basename(target_file_path)
|
| 1113 |
+
|
| 1114 |
+
# Split by separator lines to get individual sections
|
| 1115 |
+
sections = summary_content.split("=" * 80)
|
| 1116 |
+
|
| 1117 |
+
for i, section in enumerate(sections):
|
| 1118 |
+
if "## IMPLEMENTATION File" in section:
|
| 1119 |
+
# Extract the file path from the header
|
| 1120 |
+
lines = section.strip().split("\n")
|
| 1121 |
+
for line in lines:
|
| 1122 |
+
if "## IMPLEMENTATION File" in line:
|
| 1123 |
+
# Extract file path between "File " and "; ROUND"
|
| 1124 |
+
try:
|
| 1125 |
+
file_part = line.split("File ")[1].split("; ROUND")[0].strip()
|
| 1126 |
+
|
| 1127 |
+
# Check if this matches our target
|
| 1128 |
+
if (
|
| 1129 |
+
_normalize_file_path(target_file_path)
|
| 1130 |
+
== _normalize_file_path(file_part)
|
| 1131 |
+
or target_basename == os.path.basename(file_part)
|
| 1132 |
+
or target_file_path in file_part
|
| 1133 |
+
or file_part.endswith(target_file_path)
|
| 1134 |
+
):
|
| 1135 |
+
# Get the next section which contains the content
|
| 1136 |
+
if i + 1 < len(sections):
|
| 1137 |
+
content_section = sections[i + 1].strip()
|
| 1138 |
+
return f"""================================================================================
|
| 1139 |
+
## IMPLEMENTATION File {file_part}
|
| 1140 |
+
================================================================================
|
| 1141 |
+
|
| 1142 |
+
{content_section}
|
| 1143 |
+
|
| 1144 |
+
---
|
| 1145 |
+
*Extracted from implement_code_summary.md using alternative method*"""
|
| 1146 |
+
except (IndexError, AttributeError):
|
| 1147 |
+
continue
|
| 1148 |
+
|
| 1149 |
+
return None
|
| 1150 |
+
|
| 1151 |
+
|
| 1152 |
+
# ==================== Code Search Tools ====================
|
| 1153 |
+
|
| 1154 |
+
|
| 1155 |
+
@mcp.tool()
|
| 1156 |
+
async def search_code(
|
| 1157 |
+
pattern: str,
|
| 1158 |
+
file_pattern: str = "*.json",
|
| 1159 |
+
use_regex: bool = False,
|
| 1160 |
+
search_directory: str = None,
|
| 1161 |
+
) -> str:
|
| 1162 |
+
"""
|
| 1163 |
+
Search patterns in code files
|
| 1164 |
+
|
| 1165 |
+
Args:
|
| 1166 |
+
pattern: Search pattern
|
| 1167 |
+
file_pattern: File pattern (e.g., '*.py')
|
| 1168 |
+
use_regex: Whether to use regular expressions
|
| 1169 |
+
search_directory: Specify search directory (optional, uses WORKSPACE_DIR if not specified)
|
| 1170 |
+
|
| 1171 |
+
Returns:
|
| 1172 |
+
JSON string of search results
|
| 1173 |
+
"""
|
| 1174 |
+
try:
|
| 1175 |
+
# Determine search directory
|
| 1176 |
+
if search_directory:
|
| 1177 |
+
# If search directory is specified, use the specified directory
|
| 1178 |
+
if os.path.isabs(search_directory):
|
| 1179 |
+
search_path = Path(search_directory)
|
| 1180 |
+
else:
|
| 1181 |
+
# Relative path, relative to current working directory
|
| 1182 |
+
search_path = Path.cwd() / search_directory
|
| 1183 |
+
else:
|
| 1184 |
+
# 如果没有指定Search directory,使用默认的WORKSPACE_DIR
|
| 1185 |
+
ensure_workspace_exists()
|
| 1186 |
+
search_path = WORKSPACE_DIR
|
| 1187 |
+
|
| 1188 |
+
# 检查Search directory是否存在
|
| 1189 |
+
if not search_path.exists():
|
| 1190 |
+
result = {
|
| 1191 |
+
"status": "error",
|
| 1192 |
+
"message": f"Search directory不存在: {search_path}",
|
| 1193 |
+
"pattern": pattern,
|
| 1194 |
+
}
|
| 1195 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 1196 |
+
|
| 1197 |
+
import glob
|
| 1198 |
+
|
| 1199 |
+
# Get matching files
|
| 1200 |
+
file_paths = glob.glob(str(search_path / "**" / file_pattern), recursive=True)
|
| 1201 |
+
|
| 1202 |
+
matches = []
|
| 1203 |
+
total_files_searched = 0
|
| 1204 |
+
|
| 1205 |
+
for file_path in file_paths:
|
| 1206 |
+
try:
|
| 1207 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 1208 |
+
lines = f.readlines()
|
| 1209 |
+
|
| 1210 |
+
total_files_searched += 1
|
| 1211 |
+
relative_path = os.path.relpath(file_path, search_path)
|
| 1212 |
+
|
| 1213 |
+
for line_num, line in enumerate(lines, 1):
|
| 1214 |
+
if use_regex:
|
| 1215 |
+
if re.search(pattern, line):
|
| 1216 |
+
matches.append(
|
| 1217 |
+
{
|
| 1218 |
+
"file": relative_path,
|
| 1219 |
+
"line_number": line_num,
|
| 1220 |
+
"line_content": line.strip(),
|
| 1221 |
+
"match_type": "regex",
|
| 1222 |
+
}
|
| 1223 |
+
)
|
| 1224 |
+
else:
|
| 1225 |
+
if pattern.lower() in line.lower():
|
| 1226 |
+
matches.append(
|
| 1227 |
+
{
|
| 1228 |
+
"file": relative_path,
|
| 1229 |
+
"line_number": line_num,
|
| 1230 |
+
"line_content": line.strip(),
|
| 1231 |
+
"match_type": "substring",
|
| 1232 |
+
}
|
| 1233 |
+
)
|
| 1234 |
+
|
| 1235 |
+
except Exception as e:
|
| 1236 |
+
logger.warning(f"Error searching file {file_path}: {e}")
|
| 1237 |
+
continue
|
| 1238 |
+
|
| 1239 |
+
result = {
|
| 1240 |
+
"status": "success",
|
| 1241 |
+
"pattern": pattern,
|
| 1242 |
+
"file_pattern": file_pattern,
|
| 1243 |
+
"use_regex": use_regex,
|
| 1244 |
+
"search_directory": str(search_path),
|
| 1245 |
+
"total_matches": len(matches),
|
| 1246 |
+
"total_files_searched": total_files_searched,
|
| 1247 |
+
"matches": matches[:50], # 限制返回前50个匹配
|
| 1248 |
+
}
|
| 1249 |
+
|
| 1250 |
+
if len(matches) > 50:
|
| 1251 |
+
result["note"] = f"显示前50个匹配,总共找到{len(matches)}个匹配"
|
| 1252 |
+
|
| 1253 |
+
log_operation(
|
| 1254 |
+
"search_code",
|
| 1255 |
+
{
|
| 1256 |
+
"pattern": pattern,
|
| 1257 |
+
"file_pattern": file_pattern,
|
| 1258 |
+
"use_regex": use_regex,
|
| 1259 |
+
"search_directory": str(search_path),
|
| 1260 |
+
"total_matches": len(matches),
|
| 1261 |
+
"files_searched": total_files_searched,
|
| 1262 |
+
},
|
| 1263 |
+
)
|
| 1264 |
+
|
| 1265 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 1266 |
+
|
| 1267 |
+
except Exception as e:
|
| 1268 |
+
result = {
|
| 1269 |
+
"status": "error",
|
| 1270 |
+
"message": f"Code search failed: {str(e)}",
|
| 1271 |
+
"pattern": pattern,
|
| 1272 |
+
}
|
| 1273 |
+
log_operation("search_code_error", {"pattern": pattern, "error": str(e)})
|
| 1274 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 1275 |
+
|
| 1276 |
+
|
| 1277 |
+
# ==================== File Structure Tools ====================
|
| 1278 |
+
|
| 1279 |
+
|
| 1280 |
+
@mcp.tool()
|
| 1281 |
+
async def get_file_structure(directory: str = ".", max_depth: int = 5) -> str:
|
| 1282 |
+
"""
|
| 1283 |
+
Get directory file structure
|
| 1284 |
+
|
| 1285 |
+
Args:
|
| 1286 |
+
directory: Directory path, relative to workspace
|
| 1287 |
+
max_depth: 最大遍历深度
|
| 1288 |
+
|
| 1289 |
+
Returns:
|
| 1290 |
+
JSON string of file structure
|
| 1291 |
+
"""
|
| 1292 |
+
try:
|
| 1293 |
+
ensure_workspace_exists()
|
| 1294 |
+
|
| 1295 |
+
if directory == ".":
|
| 1296 |
+
target_dir = WORKSPACE_DIR
|
| 1297 |
+
else:
|
| 1298 |
+
target_dir = validate_path(directory)
|
| 1299 |
+
|
| 1300 |
+
if not target_dir.exists():
|
| 1301 |
+
result = {
|
| 1302 |
+
"status": "error",
|
| 1303 |
+
"message": f"Directory does not exist: {directory}",
|
| 1304 |
+
}
|
| 1305 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 1306 |
+
|
| 1307 |
+
def scan_directory(path: Path, current_depth: int = 0) -> Dict[str, Any]:
|
| 1308 |
+
"""Recursively scan directory"""
|
| 1309 |
+
if current_depth >= max_depth:
|
| 1310 |
+
return {"type": "directory", "name": path.name, "truncated": True}
|
| 1311 |
+
|
| 1312 |
+
items = []
|
| 1313 |
+
try:
|
| 1314 |
+
for item in sorted(path.iterdir()):
|
| 1315 |
+
relative_path = os.path.relpath(item, WORKSPACE_DIR)
|
| 1316 |
+
|
| 1317 |
+
if item.is_file():
|
| 1318 |
+
file_info = {
|
| 1319 |
+
"type": "file",
|
| 1320 |
+
"name": item.name,
|
| 1321 |
+
"path": relative_path,
|
| 1322 |
+
"size_bytes": item.stat().st_size,
|
| 1323 |
+
"extension": item.suffix,
|
| 1324 |
+
}
|
| 1325 |
+
items.append(file_info)
|
| 1326 |
+
elif item.is_dir() and not item.name.startswith("."):
|
| 1327 |
+
dir_info = scan_directory(item, current_depth + 1)
|
| 1328 |
+
dir_info["path"] = relative_path
|
| 1329 |
+
items.append(dir_info)
|
| 1330 |
+
except PermissionError:
|
| 1331 |
+
pass
|
| 1332 |
+
|
| 1333 |
+
return {
|
| 1334 |
+
"type": "directory",
|
| 1335 |
+
"name": path.name,
|
| 1336 |
+
"items": items,
|
| 1337 |
+
"item_count": len(items),
|
| 1338 |
+
}
|
| 1339 |
+
|
| 1340 |
+
structure = scan_directory(target_dir)
|
| 1341 |
+
|
| 1342 |
+
# 统计信息
|
| 1343 |
+
def count_items(node):
|
| 1344 |
+
if node["type"] == "file":
|
| 1345 |
+
return {"files": 1, "directories": 0}
|
| 1346 |
+
else:
|
| 1347 |
+
counts = {"files": 0, "directories": 1}
|
| 1348 |
+
for item in node.get("items", []):
|
| 1349 |
+
item_counts = count_items(item)
|
| 1350 |
+
counts["files"] += item_counts["files"]
|
| 1351 |
+
counts["directories"] += item_counts["directories"]
|
| 1352 |
+
return counts
|
| 1353 |
+
|
| 1354 |
+
counts = count_items(structure)
|
| 1355 |
+
|
| 1356 |
+
result = {
|
| 1357 |
+
"status": "success",
|
| 1358 |
+
"directory": directory,
|
| 1359 |
+
"max_depth": max_depth,
|
| 1360 |
+
"structure": structure,
|
| 1361 |
+
"summary": {
|
| 1362 |
+
"total_files": counts["files"],
|
| 1363 |
+
"total_directories": counts["directories"]
|
| 1364 |
+
- 1, # Exclude root directory
|
| 1365 |
+
},
|
| 1366 |
+
}
|
| 1367 |
+
|
| 1368 |
+
log_operation(
|
| 1369 |
+
"get_file_structure",
|
| 1370 |
+
{
|
| 1371 |
+
"directory": directory,
|
| 1372 |
+
"max_depth": max_depth,
|
| 1373 |
+
"total_files": counts["files"],
|
| 1374 |
+
"total_directories": counts["directories"] - 1,
|
| 1375 |
+
},
|
| 1376 |
+
)
|
| 1377 |
+
|
| 1378 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 1379 |
+
|
| 1380 |
+
except Exception as e:
|
| 1381 |
+
result = {
|
| 1382 |
+
"status": "error",
|
| 1383 |
+
"message": f"Failed to get file structure: {str(e)}",
|
| 1384 |
+
"directory": directory,
|
| 1385 |
+
}
|
| 1386 |
+
log_operation(
|
| 1387 |
+
"get_file_structure_error", {"directory": directory, "error": str(e)}
|
| 1388 |
+
)
|
| 1389 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 1390 |
+
|
| 1391 |
+
|
| 1392 |
+
# ==================== Workspace Management Tools ====================
|
| 1393 |
+
|
| 1394 |
+
|
| 1395 |
+
@mcp.tool()
|
| 1396 |
+
async def set_workspace(workspace_path: str) -> str:
|
| 1397 |
+
"""
|
| 1398 |
+
Set workspace directory
|
| 1399 |
+
|
| 1400 |
+
Called by workflow to set workspace to: {plan_file_parent}/generate_code
|
| 1401 |
+
This ensures all file operations are executed relative to the correct project directory
|
| 1402 |
+
|
| 1403 |
+
Args:
|
| 1404 |
+
workspace_path: Workspace path (Usually {plan_file_parent}/generate_code)
|
| 1405 |
+
|
| 1406 |
+
Returns:
|
| 1407 |
+
JSON string of operation result
|
| 1408 |
+
"""
|
| 1409 |
+
try:
|
| 1410 |
+
global WORKSPACE_DIR
|
| 1411 |
+
new_workspace = Path(workspace_path).resolve()
|
| 1412 |
+
|
| 1413 |
+
# Create directory (if it does not exist)
|
| 1414 |
+
new_workspace.mkdir(parents=True, exist_ok=True)
|
| 1415 |
+
|
| 1416 |
+
old_workspace = WORKSPACE_DIR
|
| 1417 |
+
WORKSPACE_DIR = new_workspace
|
| 1418 |
+
|
| 1419 |
+
logger.info(f"New Workspace: {WORKSPACE_DIR}")
|
| 1420 |
+
|
| 1421 |
+
result = {
|
| 1422 |
+
"status": "success",
|
| 1423 |
+
"message": f"Workspace setup successful: {workspace_path}",
|
| 1424 |
+
"new_workspace": str(WORKSPACE_DIR),
|
| 1425 |
+
}
|
| 1426 |
+
|
| 1427 |
+
log_operation(
|
| 1428 |
+
"set_workspace",
|
| 1429 |
+
{
|
| 1430 |
+
"old_workspace": str(old_workspace) if old_workspace else None,
|
| 1431 |
+
"new_workspace": str(WORKSPACE_DIR),
|
| 1432 |
+
"workspace_alignment": "plan_file_parent/generate_code",
|
| 1433 |
+
},
|
| 1434 |
+
)
|
| 1435 |
+
|
| 1436 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 1437 |
+
|
| 1438 |
+
except Exception as e:
|
| 1439 |
+
result = {
|
| 1440 |
+
"status": "error",
|
| 1441 |
+
"message": f"Failed to set workspace: {str(e)}",
|
| 1442 |
+
"workspace_path": workspace_path,
|
| 1443 |
+
}
|
| 1444 |
+
log_operation(
|
| 1445 |
+
"set_workspace_error", {"workspace_path": workspace_path, "error": str(e)}
|
| 1446 |
+
)
|
| 1447 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 1448 |
+
|
| 1449 |
+
|
| 1450 |
+
@mcp.tool()
|
| 1451 |
+
async def get_operation_history(last_n: int = 10) -> str:
|
| 1452 |
+
"""
|
| 1453 |
+
Get operation history
|
| 1454 |
+
|
| 1455 |
+
Args:
|
| 1456 |
+
last_n: Return the last N operations
|
| 1457 |
+
|
| 1458 |
+
Returns:
|
| 1459 |
+
JSON string of operation history
|
| 1460 |
+
"""
|
| 1461 |
+
try:
|
| 1462 |
+
recent_history = (
|
| 1463 |
+
OPERATION_HISTORY[-last_n:] if last_n > 0 else OPERATION_HISTORY
|
| 1464 |
+
)
|
| 1465 |
+
|
| 1466 |
+
result = {
|
| 1467 |
+
"status": "success",
|
| 1468 |
+
"total_operations": len(OPERATION_HISTORY),
|
| 1469 |
+
"returned_operations": len(recent_history),
|
| 1470 |
+
"workspace": str(WORKSPACE_DIR) if WORKSPACE_DIR else None,
|
| 1471 |
+
"history": recent_history,
|
| 1472 |
+
}
|
| 1473 |
+
|
| 1474 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 1475 |
+
|
| 1476 |
+
except Exception as e:
|
| 1477 |
+
result = {
|
| 1478 |
+
"status": "error",
|
| 1479 |
+
"message": f"Failed to get operation history: {str(e)}",
|
| 1480 |
+
}
|
| 1481 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 1482 |
+
|
| 1483 |
+
|
| 1484 |
+
# ==================== Server Initialization ====================
|
| 1485 |
+
|
| 1486 |
+
|
| 1487 |
+
def main():
|
| 1488 |
+
"""Start MCP server"""
|
| 1489 |
+
print("🚀 Code Implementation MCP Server")
|
| 1490 |
+
print(
|
| 1491 |
+
"📝 Paper Code Implementation Tool Server / Paper Code Implementation Tool Server"
|
| 1492 |
+
)
|
| 1493 |
+
print("")
|
| 1494 |
+
print("Available tools / Available tools:")
|
| 1495 |
+
# print(" • read_file - Read file contents / Read file contents")
|
| 1496 |
+
print(
|
| 1497 |
+
" • read_code_mem - Read code summary from implement_code_summary.md / Read code summary from implement_code_summary.md"
|
| 1498 |
+
)
|
| 1499 |
+
print(" • write_file - Write file contents / Write file contents")
|
| 1500 |
+
print(" • execute_python - Execute Python code / Execute Python code")
|
| 1501 |
+
print(" • execute_bash - Execute bash command / Execute bash commands")
|
| 1502 |
+
print(" • search_code - Search code patterns / Search code patterns")
|
| 1503 |
+
print(" • get_file_structure - Get file structure / Get file structure")
|
| 1504 |
+
print(" • set_workspace - Set workspace / Set workspace")
|
| 1505 |
+
print(" • get_operation_history - Get operation history / Get operation history")
|
| 1506 |
+
print("")
|
| 1507 |
+
print("🔧 Server starting...")
|
| 1508 |
+
|
| 1509 |
+
# Initialize default workspace
|
| 1510 |
+
initialize_workspace()
|
| 1511 |
+
|
| 1512 |
+
# Start server
|
| 1513 |
+
mcp.run()
|
| 1514 |
+
|
| 1515 |
+
|
| 1516 |
+
if __name__ == "__main__":
|
| 1517 |
+
main()
|
projects/ui/DeepCode/tools/code_indexer.py
ADDED
|
@@ -0,0 +1,1677 @@
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Code Indexer for Repository Analysis
|
| 3 |
+
|
| 4 |
+
Analyzes code repositories to build comprehensive indexes for each subdirectory,
|
| 5 |
+
identifying file relationships and reusable components for implementation.
|
| 6 |
+
|
| 7 |
+
Features:
|
| 8 |
+
- Recursive file traversal
|
| 9 |
+
- LLM-powered code similarity analysis using augmented LLM classes
|
| 10 |
+
- JSON-based relationship storage
|
| 11 |
+
- Configurable matching strategies
|
| 12 |
+
- Progress tracking and error handling
|
| 13 |
+
- Automatic LLM provider selection based on API key availability
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import asyncio
|
| 17 |
+
import json
|
| 18 |
+
import logging
|
| 19 |
+
import os
|
| 20 |
+
import re
|
| 21 |
+
from datetime import datetime
|
| 22 |
+
from pathlib import Path
|
| 23 |
+
from dataclasses import dataclass, asdict
|
| 24 |
+
from typing import List, Dict, Any
|
| 25 |
+
|
| 26 |
+
# MCP Agent imports for LLM
|
| 27 |
+
import yaml
|
| 28 |
+
from utils.llm_utils import get_preferred_llm_class
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def get_default_models(config_path: str = "mcp_agent.config.yaml"):
|
| 32 |
+
"""
|
| 33 |
+
Get default models from configuration file.
|
| 34 |
+
|
| 35 |
+
Args:
|
| 36 |
+
config_path: Path to the configuration file
|
| 37 |
+
|
| 38 |
+
Returns:
|
| 39 |
+
dict: Dictionary with 'anthropic' and 'openai' default models
|
| 40 |
+
"""
|
| 41 |
+
try:
|
| 42 |
+
if os.path.exists(config_path):
|
| 43 |
+
with open(config_path, "r", encoding="utf-8") as f:
|
| 44 |
+
config = yaml.safe_load(f)
|
| 45 |
+
|
| 46 |
+
anthropic_model = config.get("anthropic", {}).get(
|
| 47 |
+
"default_model", "claude-sonnet-4-20250514"
|
| 48 |
+
)
|
| 49 |
+
openai_model = config.get("openai", {}).get("default_model", "o3-mini")
|
| 50 |
+
|
| 51 |
+
return {"anthropic": anthropic_model, "openai": openai_model}
|
| 52 |
+
else:
|
| 53 |
+
print(f"Config file {config_path} not found, using default models")
|
| 54 |
+
return {"anthropic": "claude-sonnet-4-20250514", "openai": "o3-mini"}
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"Error reading config file {config_path}: {e}")
|
| 58 |
+
return {"anthropic": "claude-sonnet-4-20250514", "openai": "o3-mini"}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@dataclass
|
| 62 |
+
class FileRelationship:
|
| 63 |
+
"""Represents a relationship between a repo file and target structure file"""
|
| 64 |
+
|
| 65 |
+
repo_file_path: str
|
| 66 |
+
target_file_path: str
|
| 67 |
+
relationship_type: str # 'direct_match', 'partial_match', 'reference', 'utility'
|
| 68 |
+
confidence_score: float # 0.0 to 1.0
|
| 69 |
+
helpful_aspects: List[str]
|
| 70 |
+
potential_contributions: List[str]
|
| 71 |
+
usage_suggestions: str
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@dataclass
|
| 75 |
+
class FileSummary:
|
| 76 |
+
"""Summary information for a repository file"""
|
| 77 |
+
|
| 78 |
+
file_path: str
|
| 79 |
+
file_type: str
|
| 80 |
+
main_functions: List[str]
|
| 81 |
+
key_concepts: List[str]
|
| 82 |
+
dependencies: List[str]
|
| 83 |
+
summary: str
|
| 84 |
+
lines_of_code: int
|
| 85 |
+
last_modified: str
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
@dataclass
|
| 89 |
+
class RepoIndex:
|
| 90 |
+
"""Complete index for a repository"""
|
| 91 |
+
|
| 92 |
+
repo_name: str
|
| 93 |
+
total_files: int
|
| 94 |
+
file_summaries: List[FileSummary]
|
| 95 |
+
relationships: List[FileRelationship]
|
| 96 |
+
analysis_metadata: Dict[str, Any]
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
class CodeIndexer:
|
| 100 |
+
"""Main class for building code repository indexes"""
|
| 101 |
+
|
| 102 |
+
def __init__(
|
| 103 |
+
self,
|
| 104 |
+
code_base_path: str = None,
|
| 105 |
+
target_structure: str = None,
|
| 106 |
+
output_dir: str = None,
|
| 107 |
+
config_path: str = "mcp_agent.secrets.yaml",
|
| 108 |
+
indexer_config_path: str = None,
|
| 109 |
+
enable_pre_filtering: bool = True,
|
| 110 |
+
):
|
| 111 |
+
# Load configurations first
|
| 112 |
+
self.config_path = config_path
|
| 113 |
+
self.indexer_config_path = indexer_config_path
|
| 114 |
+
self.api_config = self._load_api_config()
|
| 115 |
+
self.indexer_config = self._load_indexer_config()
|
| 116 |
+
self.default_models = get_default_models("mcp_agent.config.yaml")
|
| 117 |
+
|
| 118 |
+
# Use config paths if not provided as parameters
|
| 119 |
+
paths_config = self.indexer_config.get("paths", {})
|
| 120 |
+
self.code_base_path = Path(
|
| 121 |
+
code_base_path or paths_config.get("code_base_path", "code_base")
|
| 122 |
+
)
|
| 123 |
+
self.output_dir = Path(output_dir or paths_config.get("output_dir", "indexes"))
|
| 124 |
+
self.target_structure = (
|
| 125 |
+
target_structure # This must be provided as it's project-specific
|
| 126 |
+
)
|
| 127 |
+
self.enable_pre_filtering = enable_pre_filtering
|
| 128 |
+
|
| 129 |
+
# LLM clients
|
| 130 |
+
self.llm_client = None
|
| 131 |
+
self.llm_client_type = None
|
| 132 |
+
|
| 133 |
+
# Initialize logger early
|
| 134 |
+
self.logger = self._setup_logger()
|
| 135 |
+
|
| 136 |
+
# Create output directory if it doesn't exist
|
| 137 |
+
self.output_dir.mkdir(parents=True, exist_ok=True)
|
| 138 |
+
|
| 139 |
+
# Load file analysis configuration
|
| 140 |
+
file_analysis_config = self.indexer_config.get("file_analysis", {})
|
| 141 |
+
self.supported_extensions = set(
|
| 142 |
+
file_analysis_config.get(
|
| 143 |
+
"supported_extensions",
|
| 144 |
+
[
|
| 145 |
+
".py",
|
| 146 |
+
".js",
|
| 147 |
+
".ts",
|
| 148 |
+
".java",
|
| 149 |
+
".cpp",
|
| 150 |
+
".c",
|
| 151 |
+
".h",
|
| 152 |
+
".hpp",
|
| 153 |
+
".cs",
|
| 154 |
+
".php",
|
| 155 |
+
".rb",
|
| 156 |
+
".go",
|
| 157 |
+
".rs",
|
| 158 |
+
".scala",
|
| 159 |
+
".kt",
|
| 160 |
+
".swift",
|
| 161 |
+
".m",
|
| 162 |
+
".mm",
|
| 163 |
+
".r",
|
| 164 |
+
".matlab",
|
| 165 |
+
".sql",
|
| 166 |
+
".sh",
|
| 167 |
+
".bat",
|
| 168 |
+
".ps1",
|
| 169 |
+
".yaml",
|
| 170 |
+
".yml",
|
| 171 |
+
".json",
|
| 172 |
+
".xml",
|
| 173 |
+
".toml",
|
| 174 |
+
],
|
| 175 |
+
)
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
self.skip_directories = set(
|
| 179 |
+
file_analysis_config.get(
|
| 180 |
+
"skip_directories",
|
| 181 |
+
[
|
| 182 |
+
"__pycache__",
|
| 183 |
+
"node_modules",
|
| 184 |
+
"target",
|
| 185 |
+
"build",
|
| 186 |
+
"dist",
|
| 187 |
+
"venv",
|
| 188 |
+
"env",
|
| 189 |
+
],
|
| 190 |
+
)
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
self.max_file_size = file_analysis_config.get("max_file_size", 1048576) # 1MB
|
| 194 |
+
self.max_content_length = file_analysis_config.get("max_content_length", 3000)
|
| 195 |
+
|
| 196 |
+
# Load LLM configuration
|
| 197 |
+
llm_config = self.indexer_config.get("llm", {})
|
| 198 |
+
self.model_provider = llm_config.get("model_provider", "anthropic")
|
| 199 |
+
self.llm_max_tokens = llm_config.get("max_tokens", 4000)
|
| 200 |
+
self.llm_temperature = llm_config.get("temperature", 0.3)
|
| 201 |
+
self.llm_system_prompt = llm_config.get(
|
| 202 |
+
"system_prompt",
|
| 203 |
+
"You are a code analysis expert. Provide precise, structured analysis of code relationships and similarities.",
|
| 204 |
+
)
|
| 205 |
+
self.request_delay = llm_config.get("request_delay", 0.1)
|
| 206 |
+
self.max_retries = llm_config.get("max_retries", 3)
|
| 207 |
+
self.retry_delay = llm_config.get("retry_delay", 1.0)
|
| 208 |
+
|
| 209 |
+
# Load relationship configuration
|
| 210 |
+
relationship_config = self.indexer_config.get("relationships", {})
|
| 211 |
+
self.min_confidence_score = relationship_config.get("min_confidence_score", 0.3)
|
| 212 |
+
self.high_confidence_threshold = relationship_config.get(
|
| 213 |
+
"high_confidence_threshold", 0.7
|
| 214 |
+
)
|
| 215 |
+
self.relationship_types = relationship_config.get(
|
| 216 |
+
"relationship_types",
|
| 217 |
+
{
|
| 218 |
+
"direct_match": 1.0,
|
| 219 |
+
"partial_match": 0.8,
|
| 220 |
+
"reference": 0.6,
|
| 221 |
+
"utility": 0.4,
|
| 222 |
+
},
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
# Load performance configuration
|
| 226 |
+
performance_config = self.indexer_config.get("performance", {})
|
| 227 |
+
self.enable_concurrent_analysis = performance_config.get(
|
| 228 |
+
"enable_concurrent_analysis", False
|
| 229 |
+
)
|
| 230 |
+
self.max_concurrent_files = performance_config.get("max_concurrent_files", 5)
|
| 231 |
+
self.enable_content_caching = performance_config.get(
|
| 232 |
+
"enable_content_caching", False
|
| 233 |
+
)
|
| 234 |
+
self.max_cache_size = performance_config.get("max_cache_size", 100)
|
| 235 |
+
|
| 236 |
+
# Load debug configuration
|
| 237 |
+
debug_config = self.indexer_config.get("debug", {})
|
| 238 |
+
self.save_raw_responses = debug_config.get("save_raw_responses", False)
|
| 239 |
+
self.raw_responses_dir = debug_config.get(
|
| 240 |
+
"raw_responses_dir", "debug_responses"
|
| 241 |
+
)
|
| 242 |
+
self.verbose_output = debug_config.get("verbose_output", False)
|
| 243 |
+
self.mock_llm_responses = debug_config.get("mock_llm_responses", False)
|
| 244 |
+
|
| 245 |
+
# Load output configuration
|
| 246 |
+
output_config = self.indexer_config.get("output", {})
|
| 247 |
+
self.generate_summary = output_config.get("generate_summary", True)
|
| 248 |
+
self.generate_statistics = output_config.get("generate_statistics", True)
|
| 249 |
+
self.include_metadata = output_config.get("include_metadata", True)
|
| 250 |
+
self.index_filename_pattern = output_config.get(
|
| 251 |
+
"index_filename_pattern", "{repo_name}_index.json"
|
| 252 |
+
)
|
| 253 |
+
self.summary_filename = output_config.get(
|
| 254 |
+
"summary_filename", "indexing_summary.json"
|
| 255 |
+
)
|
| 256 |
+
self.stats_filename = output_config.get(
|
| 257 |
+
"stats_filename", "indexing_statistics.json"
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
# Initialize caching if enabled
|
| 261 |
+
self.content_cache = {} if self.enable_content_caching else None
|
| 262 |
+
|
| 263 |
+
# Create debug directory if needed
|
| 264 |
+
if self.save_raw_responses:
|
| 265 |
+
Path(self.raw_responses_dir).mkdir(parents=True, exist_ok=True)
|
| 266 |
+
|
| 267 |
+
# Debug logging
|
| 268 |
+
if self.verbose_output:
|
| 269 |
+
self.logger.info(
|
| 270 |
+
f"Initialized CodeIndexer with config: {self.indexer_config_path}"
|
| 271 |
+
)
|
| 272 |
+
self.logger.info(f"Code base path: {self.code_base_path}")
|
| 273 |
+
self.logger.info(f"Output directory: {self.output_dir}")
|
| 274 |
+
self.logger.info(f"Model provider: {self.model_provider}")
|
| 275 |
+
self.logger.info(f"Concurrent analysis: {self.enable_concurrent_analysis}")
|
| 276 |
+
self.logger.info(f"Content caching: {self.enable_content_caching}")
|
| 277 |
+
self.logger.info(f"Mock LLM responses: {self.mock_llm_responses}")
|
| 278 |
+
|
| 279 |
+
def _setup_logger(self) -> logging.Logger:
|
| 280 |
+
"""Setup logging configuration from config file"""
|
| 281 |
+
logger = logging.getLogger("CodeIndexer")
|
| 282 |
+
|
| 283 |
+
# Get logging config
|
| 284 |
+
logging_config = self.indexer_config.get("logging", {})
|
| 285 |
+
log_level = logging_config.get("level", "INFO")
|
| 286 |
+
log_format = logging_config.get(
|
| 287 |
+
"log_format", "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
logger.setLevel(getattr(logging, log_level.upper(), logging.INFO))
|
| 291 |
+
|
| 292 |
+
# Clear existing handlers
|
| 293 |
+
logger.handlers.clear()
|
| 294 |
+
|
| 295 |
+
# Console handler
|
| 296 |
+
handler = logging.StreamHandler()
|
| 297 |
+
formatter = logging.Formatter(log_format)
|
| 298 |
+
handler.setFormatter(formatter)
|
| 299 |
+
logger.addHandler(handler)
|
| 300 |
+
|
| 301 |
+
# File handler if enabled
|
| 302 |
+
if logging_config.get("log_to_file", False):
|
| 303 |
+
log_file = logging_config.get("log_file", "indexer.log")
|
| 304 |
+
file_handler = logging.FileHandler(log_file, encoding="utf-8")
|
| 305 |
+
file_handler.setFormatter(formatter)
|
| 306 |
+
logger.addHandler(file_handler)
|
| 307 |
+
|
| 308 |
+
return logger
|
| 309 |
+
|
| 310 |
+
def _load_api_config(self) -> Dict[str, Any]:
|
| 311 |
+
"""Load API configuration from YAML file"""
|
| 312 |
+
try:
|
| 313 |
+
import yaml
|
| 314 |
+
|
| 315 |
+
with open(self.config_path, "r", encoding="utf-8") as f:
|
| 316 |
+
return yaml.safe_load(f)
|
| 317 |
+
except Exception as e:
|
| 318 |
+
# Create a basic logger for this error since self.logger doesn't exist yet
|
| 319 |
+
print(f"Warning: Failed to load API config from {self.config_path}: {e}")
|
| 320 |
+
return {}
|
| 321 |
+
|
| 322 |
+
def _load_indexer_config(self) -> Dict[str, Any]:
|
| 323 |
+
"""Load indexer configuration from YAML file"""
|
| 324 |
+
try:
|
| 325 |
+
import yaml
|
| 326 |
+
|
| 327 |
+
with open(self.indexer_config_path, "r", encoding="utf-8") as f:
|
| 328 |
+
config = yaml.safe_load(f)
|
| 329 |
+
if config is None:
|
| 330 |
+
config = {}
|
| 331 |
+
return config
|
| 332 |
+
except Exception as e:
|
| 333 |
+
print(
|
| 334 |
+
f"Warning: Failed to load indexer config from {self.indexer_config_path}: {e}"
|
| 335 |
+
)
|
| 336 |
+
print("Using default configuration values")
|
| 337 |
+
return {}
|
| 338 |
+
|
| 339 |
+
async def _initialize_llm_client(self):
|
| 340 |
+
"""Initialize LLM client (Anthropic or OpenAI) based on API key availability"""
|
| 341 |
+
if self.llm_client is not None:
|
| 342 |
+
return self.llm_client, self.llm_client_type
|
| 343 |
+
|
| 344 |
+
# Check if mock responses are enabled
|
| 345 |
+
if self.mock_llm_responses:
|
| 346 |
+
self.logger.info("Using mock LLM responses for testing")
|
| 347 |
+
self.llm_client = "mock"
|
| 348 |
+
self.llm_client_type = "mock"
|
| 349 |
+
return "mock", "mock"
|
| 350 |
+
|
| 351 |
+
# Check which API has available key and try that first
|
| 352 |
+
anthropic_key = self.api_config.get("anthropic", {}).get("api_key", "")
|
| 353 |
+
openai_key = self.api_config.get("openai", {}).get("api_key", "")
|
| 354 |
+
|
| 355 |
+
# Try Anthropic API first if key is available
|
| 356 |
+
if anthropic_key and anthropic_key.strip():
|
| 357 |
+
try:
|
| 358 |
+
from anthropic import AsyncAnthropic
|
| 359 |
+
|
| 360 |
+
client = AsyncAnthropic(api_key=anthropic_key)
|
| 361 |
+
# Test connection with default model from config
|
| 362 |
+
await client.messages.create(
|
| 363 |
+
model=self.default_models["anthropic"],
|
| 364 |
+
max_tokens=10,
|
| 365 |
+
messages=[{"role": "user", "content": "test"}],
|
| 366 |
+
)
|
| 367 |
+
self.logger.info(
|
| 368 |
+
f"Using Anthropic API with model: {self.default_models['anthropic']}"
|
| 369 |
+
)
|
| 370 |
+
self.llm_client = client
|
| 371 |
+
self.llm_client_type = "anthropic"
|
| 372 |
+
return client, "anthropic"
|
| 373 |
+
except Exception as e:
|
| 374 |
+
self.logger.warning(f"Anthropic API unavailable: {e}")
|
| 375 |
+
|
| 376 |
+
# Try OpenAI API if Anthropic failed or key not available
|
| 377 |
+
if openai_key and openai_key.strip():
|
| 378 |
+
try:
|
| 379 |
+
from openai import AsyncOpenAI
|
| 380 |
+
|
| 381 |
+
# Handle custom base_url if specified
|
| 382 |
+
openai_config = self.api_config.get("openai", {})
|
| 383 |
+
base_url = openai_config.get("base_url")
|
| 384 |
+
|
| 385 |
+
if base_url:
|
| 386 |
+
client = AsyncOpenAI(api_key=openai_key, base_url=base_url)
|
| 387 |
+
else:
|
| 388 |
+
client = AsyncOpenAI(api_key=openai_key)
|
| 389 |
+
|
| 390 |
+
# Test connection with default model from config
|
| 391 |
+
await client.chat.completions.create(
|
| 392 |
+
model=self.default_models["openai"],
|
| 393 |
+
max_tokens=10,
|
| 394 |
+
messages=[{"role": "user", "content": "test"}],
|
| 395 |
+
)
|
| 396 |
+
self.logger.info(
|
| 397 |
+
f"Using OpenAI API with model: {self.default_models['openai']}"
|
| 398 |
+
)
|
| 399 |
+
if base_url:
|
| 400 |
+
self.logger.info(f"Using custom base URL: {base_url}")
|
| 401 |
+
self.llm_client = client
|
| 402 |
+
self.llm_client_type = "openai"
|
| 403 |
+
return client, "openai"
|
| 404 |
+
except Exception as e:
|
| 405 |
+
self.logger.warning(f"OpenAI API unavailable: {e}")
|
| 406 |
+
|
| 407 |
+
raise ValueError(
|
| 408 |
+
"No available LLM API - please check your API keys in configuration"
|
| 409 |
+
)
|
| 410 |
+
|
| 411 |
+
async def _call_llm(
|
| 412 |
+
self, prompt: str, system_prompt: str = None, max_tokens: int = None
|
| 413 |
+
) -> str:
|
| 414 |
+
"""Call LLM for code analysis with retry mechanism and debugging support"""
|
| 415 |
+
if system_prompt is None:
|
| 416 |
+
system_prompt = self.llm_system_prompt
|
| 417 |
+
if max_tokens is None:
|
| 418 |
+
max_tokens = self.llm_max_tokens
|
| 419 |
+
|
| 420 |
+
# Mock response for testing
|
| 421 |
+
if self.mock_llm_responses:
|
| 422 |
+
mock_response = self._generate_mock_response(prompt)
|
| 423 |
+
if self.save_raw_responses:
|
| 424 |
+
self._save_debug_response("mock", prompt, mock_response)
|
| 425 |
+
return mock_response
|
| 426 |
+
|
| 427 |
+
last_error = None
|
| 428 |
+
|
| 429 |
+
# Retry mechanism
|
| 430 |
+
for attempt in range(self.max_retries):
|
| 431 |
+
try:
|
| 432 |
+
if self.verbose_output and attempt > 0:
|
| 433 |
+
self.logger.info(
|
| 434 |
+
f"LLM call attempt {attempt + 1}/{self.max_retries}"
|
| 435 |
+
)
|
| 436 |
+
|
| 437 |
+
client, client_type = await self._initialize_llm_client()
|
| 438 |
+
|
| 439 |
+
if client_type == "anthropic":
|
| 440 |
+
response = await client.messages.create(
|
| 441 |
+
model=self.default_models["anthropic"],
|
| 442 |
+
system=system_prompt,
|
| 443 |
+
messages=[{"role": "user", "content": prompt}],
|
| 444 |
+
max_tokens=max_tokens,
|
| 445 |
+
temperature=self.llm_temperature,
|
| 446 |
+
)
|
| 447 |
+
|
| 448 |
+
content = ""
|
| 449 |
+
for block in response.content:
|
| 450 |
+
if block.type == "text":
|
| 451 |
+
content += block.text
|
| 452 |
+
|
| 453 |
+
# Save debug response if enabled
|
| 454 |
+
if self.save_raw_responses:
|
| 455 |
+
self._save_debug_response("anthropic", prompt, content)
|
| 456 |
+
|
| 457 |
+
return content
|
| 458 |
+
|
| 459 |
+
elif client_type == "openai":
|
| 460 |
+
messages = [
|
| 461 |
+
{"role": "system", "content": system_prompt},
|
| 462 |
+
{"role": "user", "content": prompt},
|
| 463 |
+
]
|
| 464 |
+
|
| 465 |
+
response = await client.chat.completions.create(
|
| 466 |
+
model=self.default_models["openai"],
|
| 467 |
+
messages=messages,
|
| 468 |
+
max_tokens=max_tokens,
|
| 469 |
+
temperature=self.llm_temperature,
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
+
content = response.choices[0].message.content or ""
|
| 473 |
+
|
| 474 |
+
# Save debug response if enabled
|
| 475 |
+
if self.save_raw_responses:
|
| 476 |
+
self._save_debug_response("openai", prompt, content)
|
| 477 |
+
|
| 478 |
+
return content
|
| 479 |
+
else:
|
| 480 |
+
raise ValueError(f"Unsupported client type: {client_type}")
|
| 481 |
+
|
| 482 |
+
except Exception as e:
|
| 483 |
+
last_error = e
|
| 484 |
+
self.logger.warning(f"LLM call attempt {attempt + 1} failed: {e}")
|
| 485 |
+
|
| 486 |
+
if attempt < self.max_retries - 1:
|
| 487 |
+
await asyncio.sleep(
|
| 488 |
+
self.retry_delay * (attempt + 1)
|
| 489 |
+
) # Exponential backoff
|
| 490 |
+
|
| 491 |
+
# All retries failed
|
| 492 |
+
error_msg = f"LLM call failed after {self.max_retries} attempts. Last error: {str(last_error)}"
|
| 493 |
+
self.logger.error(error_msg)
|
| 494 |
+
return f"Error in LLM analysis: {error_msg}"
|
| 495 |
+
|
| 496 |
+
def _generate_mock_response(self, prompt: str) -> str:
|
| 497 |
+
"""Generate mock LLM response for testing"""
|
| 498 |
+
if "JSON format" in prompt and "file_type" in prompt:
|
| 499 |
+
# File analysis mock
|
| 500 |
+
return """
|
| 501 |
+
{
|
| 502 |
+
"file_type": "Python module",
|
| 503 |
+
"main_functions": ["main_function", "helper_function"],
|
| 504 |
+
"key_concepts": ["data_processing", "algorithm"],
|
| 505 |
+
"dependencies": ["numpy", "pandas"],
|
| 506 |
+
"summary": "Mock analysis of code file functionality."
|
| 507 |
+
}
|
| 508 |
+
"""
|
| 509 |
+
elif "relationships" in prompt:
|
| 510 |
+
# Relationship analysis mock
|
| 511 |
+
return """
|
| 512 |
+
{
|
| 513 |
+
"relationships": [
|
| 514 |
+
{
|
| 515 |
+
"target_file_path": "src/core/mock.py",
|
| 516 |
+
"relationship_type": "partial_match",
|
| 517 |
+
"confidence_score": 0.8,
|
| 518 |
+
"helpful_aspects": ["algorithm implementation", "data structures"],
|
| 519 |
+
"potential_contributions": ["core functionality", "utility methods"],
|
| 520 |
+
"usage_suggestions": "Mock relationship suggestion for testing."
|
| 521 |
+
}
|
| 522 |
+
]
|
| 523 |
+
}
|
| 524 |
+
"""
|
| 525 |
+
elif "relevant_files" in prompt:
|
| 526 |
+
# File filtering mock
|
| 527 |
+
return """
|
| 528 |
+
{
|
| 529 |
+
"relevant_files": [
|
| 530 |
+
{
|
| 531 |
+
"file_path": "mock_file.py",
|
| 532 |
+
"relevance_reason": "Mock relevance reason",
|
| 533 |
+
"confidence": 0.9,
|
| 534 |
+
"expected_contribution": "Mock contribution"
|
| 535 |
+
}
|
| 536 |
+
],
|
| 537 |
+
"summary": {
|
| 538 |
+
"total_files_analyzed": "10",
|
| 539 |
+
"relevant_files_count": "1",
|
| 540 |
+
"filtering_strategy": "Mock filtering strategy"
|
| 541 |
+
}
|
| 542 |
+
}
|
| 543 |
+
"""
|
| 544 |
+
else:
|
| 545 |
+
return "Mock LLM response for testing purposes."
|
| 546 |
+
|
| 547 |
+
def _save_debug_response(self, provider: str, prompt: str, response: str):
|
| 548 |
+
"""Save LLM response for debugging"""
|
| 549 |
+
try:
|
| 550 |
+
import hashlib
|
| 551 |
+
from datetime import datetime
|
| 552 |
+
|
| 553 |
+
# Create a hash of the prompt for filename
|
| 554 |
+
prompt_hash = hashlib.md5(prompt.encode()).hexdigest()[:8]
|
| 555 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 556 |
+
filename = f"{provider}_{timestamp}_{prompt_hash}.json"
|
| 557 |
+
|
| 558 |
+
debug_data = {
|
| 559 |
+
"timestamp": datetime.now().isoformat(),
|
| 560 |
+
"provider": provider,
|
| 561 |
+
"prompt": prompt[:500] + "..." if len(prompt) > 500 else prompt,
|
| 562 |
+
"response": response,
|
| 563 |
+
"full_prompt_length": len(prompt),
|
| 564 |
+
}
|
| 565 |
+
|
| 566 |
+
debug_file = Path(self.raw_responses_dir) / filename
|
| 567 |
+
with open(debug_file, "w", encoding="utf-8") as f:
|
| 568 |
+
json.dump(debug_data, f, indent=2, ensure_ascii=False)
|
| 569 |
+
|
| 570 |
+
except Exception as e:
|
| 571 |
+
self.logger.warning(f"Failed to save debug response: {e}")
|
| 572 |
+
|
| 573 |
+
def get_all_repo_files(self, repo_path: Path) -> List[Path]:
|
| 574 |
+
"""Recursively get all supported files in a repository"""
|
| 575 |
+
files = []
|
| 576 |
+
|
| 577 |
+
try:
|
| 578 |
+
for root, dirs, filenames in os.walk(repo_path):
|
| 579 |
+
# Skip common non-code directories
|
| 580 |
+
dirs[:] = [
|
| 581 |
+
d
|
| 582 |
+
for d in dirs
|
| 583 |
+
if not d.startswith(".") and d not in self.skip_directories
|
| 584 |
+
]
|
| 585 |
+
|
| 586 |
+
for filename in filenames:
|
| 587 |
+
file_path = Path(root) / filename
|
| 588 |
+
if file_path.suffix.lower() in self.supported_extensions:
|
| 589 |
+
files.append(file_path)
|
| 590 |
+
|
| 591 |
+
except Exception as e:
|
| 592 |
+
self.logger.error(f"Error traversing {repo_path}: {e}")
|
| 593 |
+
|
| 594 |
+
return files
|
| 595 |
+
|
| 596 |
+
def generate_file_tree(self, repo_path: Path, max_depth: int = 5) -> str:
|
| 597 |
+
"""Generate file tree structure string for the repository"""
|
| 598 |
+
tree_lines = []
|
| 599 |
+
|
| 600 |
+
def add_to_tree(current_path: Path, prefix: str = "", depth: int = 0):
|
| 601 |
+
if depth > max_depth:
|
| 602 |
+
return
|
| 603 |
+
|
| 604 |
+
try:
|
| 605 |
+
items = sorted(
|
| 606 |
+
current_path.iterdir(), key=lambda x: (x.is_file(), x.name.lower())
|
| 607 |
+
)
|
| 608 |
+
# Filter out irrelevant directories and files
|
| 609 |
+
items = [
|
| 610 |
+
item
|
| 611 |
+
for item in items
|
| 612 |
+
if not item.name.startswith(".")
|
| 613 |
+
and item.name not in self.skip_directories
|
| 614 |
+
]
|
| 615 |
+
|
| 616 |
+
for i, item in enumerate(items):
|
| 617 |
+
is_last = i == len(items) - 1
|
| 618 |
+
current_prefix = "└── " if is_last else "├── "
|
| 619 |
+
tree_lines.append(f"{prefix}{current_prefix}{item.name}")
|
| 620 |
+
|
| 621 |
+
if item.is_dir():
|
| 622 |
+
extension_prefix = " " if is_last else "│ "
|
| 623 |
+
add_to_tree(item, prefix + extension_prefix, depth + 1)
|
| 624 |
+
elif item.suffix.lower() in self.supported_extensions:
|
| 625 |
+
# Add file size information
|
| 626 |
+
try:
|
| 627 |
+
size = item.stat().st_size
|
| 628 |
+
if size > 1024:
|
| 629 |
+
size_str = f" ({size // 1024}KB)"
|
| 630 |
+
else:
|
| 631 |
+
size_str = f" ({size}B)"
|
| 632 |
+
tree_lines[-1] += size_str
|
| 633 |
+
except (OSError, PermissionError):
|
| 634 |
+
pass
|
| 635 |
+
|
| 636 |
+
except PermissionError:
|
| 637 |
+
tree_lines.append(f"{prefix}├── [Permission Denied]")
|
| 638 |
+
except Exception as e:
|
| 639 |
+
tree_lines.append(f"{prefix}├── [Error: {str(e)}]")
|
| 640 |
+
|
| 641 |
+
tree_lines.append(f"{repo_path.name}/")
|
| 642 |
+
add_to_tree(repo_path)
|
| 643 |
+
return "\n".join(tree_lines)
|
| 644 |
+
|
| 645 |
+
async def pre_filter_files(self, repo_path: Path, file_tree: str) -> List[str]:
|
| 646 |
+
"""Use LLM to pre-filter relevant files based on target structure"""
|
| 647 |
+
filter_prompt = f"""
|
| 648 |
+
You are a code analysis expert. Please analyze the following code repository file tree based on the target project structure and filter out files that may be relevant to the target project.
|
| 649 |
+
|
| 650 |
+
Target Project Structure:
|
| 651 |
+
{self.target_structure}
|
| 652 |
+
|
| 653 |
+
Code Repository File Tree:
|
| 654 |
+
{file_tree}
|
| 655 |
+
|
| 656 |
+
Please analyze which files might be helpful for implementing the target project structure, including:
|
| 657 |
+
- Core algorithm implementation files (such as GCN, recommendation systems, graph neural networks, etc.)
|
| 658 |
+
- Data processing and preprocessing files
|
| 659 |
+
- Loss functions and evaluation metric files
|
| 660 |
+
- Configuration and utility files
|
| 661 |
+
- Test files
|
| 662 |
+
- Documentation files
|
| 663 |
+
|
| 664 |
+
Please return the filtering results in JSON format:
|
| 665 |
+
{{
|
| 666 |
+
"relevant_files": [
|
| 667 |
+
{{
|
| 668 |
+
"file_path": "file path relative to repository root",
|
| 669 |
+
"relevance_reason": "why this file is relevant",
|
| 670 |
+
"confidence": 0.0-1.0,
|
| 671 |
+
"expected_contribution": "expected contribution to the target project"
|
| 672 |
+
}}
|
| 673 |
+
],
|
| 674 |
+
"summary": {{
|
| 675 |
+
"total_files_analyzed": "total number of files analyzed",
|
| 676 |
+
"relevant_files_count": "number of relevant files",
|
| 677 |
+
"filtering_strategy": "explanation of filtering strategy"
|
| 678 |
+
}}
|
| 679 |
+
}}
|
| 680 |
+
|
| 681 |
+
Only return files with confidence > {self.min_confidence_score}. Focus on files related to recommendation systems, graph neural networks, and diffusion models.
|
| 682 |
+
"""
|
| 683 |
+
|
| 684 |
+
try:
|
| 685 |
+
self.logger.info("Starting LLM pre-filtering of files...")
|
| 686 |
+
llm_response = await self._call_llm(
|
| 687 |
+
filter_prompt,
|
| 688 |
+
system_prompt="You are a professional code analysis and project architecture expert, skilled at identifying code file functionality and relevance.",
|
| 689 |
+
max_tokens=2000,
|
| 690 |
+
)
|
| 691 |
+
|
| 692 |
+
# Parse JSON response
|
| 693 |
+
match = re.search(r"\{.*\}", llm_response, re.DOTALL)
|
| 694 |
+
if not match:
|
| 695 |
+
self.logger.warning(
|
| 696 |
+
"Unable to parse LLM filtering response, will use all files"
|
| 697 |
+
)
|
| 698 |
+
return []
|
| 699 |
+
|
| 700 |
+
filter_data = json.loads(match.group(0))
|
| 701 |
+
relevant_files = filter_data.get("relevant_files", [])
|
| 702 |
+
|
| 703 |
+
# Extract file paths
|
| 704 |
+
selected_files = []
|
| 705 |
+
for file_info in relevant_files:
|
| 706 |
+
file_path = file_info.get("file_path", "")
|
| 707 |
+
confidence = file_info.get("confidence", 0.0)
|
| 708 |
+
# Use configured minimum confidence threshold
|
| 709 |
+
if file_path and confidence > self.min_confidence_score:
|
| 710 |
+
selected_files.append(file_path)
|
| 711 |
+
|
| 712 |
+
summary = filter_data.get("summary", {})
|
| 713 |
+
self.logger.info(
|
| 714 |
+
f"LLM filtering completed: {summary.get('relevant_files_count', len(selected_files))} relevant files selected"
|
| 715 |
+
)
|
| 716 |
+
self.logger.info(
|
| 717 |
+
f"Filtering strategy: {summary.get('filtering_strategy', 'Not provided')}"
|
| 718 |
+
)
|
| 719 |
+
|
| 720 |
+
return selected_files
|
| 721 |
+
|
| 722 |
+
except Exception as e:
|
| 723 |
+
self.logger.error(f"LLM pre-filtering failed: {e}")
|
| 724 |
+
self.logger.info("Will fallback to analyzing all files")
|
| 725 |
+
return []
|
| 726 |
+
|
| 727 |
+
def filter_files_by_paths(
|
| 728 |
+
self, all_files: List[Path], selected_paths: List[str], repo_path: Path
|
| 729 |
+
) -> List[Path]:
|
| 730 |
+
"""Filter file list based on LLM-selected paths"""
|
| 731 |
+
if not selected_paths:
|
| 732 |
+
return all_files
|
| 733 |
+
|
| 734 |
+
filtered_files = []
|
| 735 |
+
|
| 736 |
+
for file_path in all_files:
|
| 737 |
+
# Get path relative to repository root
|
| 738 |
+
relative_path = str(file_path.relative_to(repo_path))
|
| 739 |
+
|
| 740 |
+
# Check if it's in the selected list
|
| 741 |
+
for selected_path in selected_paths:
|
| 742 |
+
# Normalize path comparison
|
| 743 |
+
if (
|
| 744 |
+
relative_path == selected_path
|
| 745 |
+
or relative_path.replace("\\", "/")
|
| 746 |
+
== selected_path.replace("\\", "/")
|
| 747 |
+
or selected_path in relative_path
|
| 748 |
+
or relative_path in selected_path
|
| 749 |
+
):
|
| 750 |
+
filtered_files.append(file_path)
|
| 751 |
+
break
|
| 752 |
+
|
| 753 |
+
return filtered_files
|
| 754 |
+
|
| 755 |
+
def _get_cache_key(self, file_path: Path) -> str:
|
| 756 |
+
"""Generate cache key for file content"""
|
| 757 |
+
try:
|
| 758 |
+
stats = file_path.stat()
|
| 759 |
+
return f"{file_path}:{stats.st_mtime}:{stats.st_size}"
|
| 760 |
+
except (OSError, PermissionError):
|
| 761 |
+
return str(file_path)
|
| 762 |
+
|
| 763 |
+
def _manage_cache_size(self):
|
| 764 |
+
"""Manage cache size to stay within limits"""
|
| 765 |
+
if not self.enable_content_caching or not self.content_cache:
|
| 766 |
+
return
|
| 767 |
+
|
| 768 |
+
if len(self.content_cache) > self.max_cache_size:
|
| 769 |
+
# Remove oldest entries (simple FIFO strategy)
|
| 770 |
+
excess_count = len(self.content_cache) - self.max_cache_size + 10
|
| 771 |
+
keys_to_remove = list(self.content_cache.keys())[:excess_count]
|
| 772 |
+
|
| 773 |
+
for key in keys_to_remove:
|
| 774 |
+
del self.content_cache[key]
|
| 775 |
+
|
| 776 |
+
if self.verbose_output:
|
| 777 |
+
self.logger.info(
|
| 778 |
+
f"Cache cleaned: removed {excess_count} entries, {len(self.content_cache)} entries remaining"
|
| 779 |
+
)
|
| 780 |
+
|
| 781 |
+
async def analyze_file_content(self, file_path: Path) -> FileSummary:
|
| 782 |
+
"""Analyze a single file and create summary with caching support"""
|
| 783 |
+
try:
|
| 784 |
+
# Check file size before reading
|
| 785 |
+
file_size = file_path.stat().st_size
|
| 786 |
+
if file_size > self.max_file_size:
|
| 787 |
+
self.logger.warning(
|
| 788 |
+
f"Skipping file {file_path} - size {file_size} bytes exceeds limit {self.max_file_size}"
|
| 789 |
+
)
|
| 790 |
+
return FileSummary(
|
| 791 |
+
file_path=str(file_path.relative_to(self.code_base_path)),
|
| 792 |
+
file_type="skipped - too large",
|
| 793 |
+
main_functions=[],
|
| 794 |
+
key_concepts=[],
|
| 795 |
+
dependencies=[],
|
| 796 |
+
summary=f"File skipped - size {file_size} bytes exceeds {self.max_file_size} byte limit",
|
| 797 |
+
lines_of_code=0,
|
| 798 |
+
last_modified=datetime.fromtimestamp(
|
| 799 |
+
file_path.stat().st_mtime
|
| 800 |
+
).isoformat(),
|
| 801 |
+
)
|
| 802 |
+
|
| 803 |
+
# Check cache if enabled
|
| 804 |
+
cache_key = None
|
| 805 |
+
if self.enable_content_caching:
|
| 806 |
+
cache_key = self._get_cache_key(file_path)
|
| 807 |
+
if cache_key in self.content_cache:
|
| 808 |
+
if self.verbose_output:
|
| 809 |
+
self.logger.info(f"Using cached analysis for {file_path.name}")
|
| 810 |
+
return self.content_cache[cache_key]
|
| 811 |
+
|
| 812 |
+
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 813 |
+
content = f.read()
|
| 814 |
+
|
| 815 |
+
# Get file stats
|
| 816 |
+
stats = file_path.stat()
|
| 817 |
+
lines_of_code = len([line for line in content.split("\n") if line.strip()])
|
| 818 |
+
|
| 819 |
+
# Truncate content based on config
|
| 820 |
+
content_for_analysis = content[: self.max_content_length]
|
| 821 |
+
content_suffix = "..." if len(content) > self.max_content_length else ""
|
| 822 |
+
|
| 823 |
+
# Create analysis prompt
|
| 824 |
+
analysis_prompt = f"""
|
| 825 |
+
Analyze this code file and provide a structured summary:
|
| 826 |
+
|
| 827 |
+
File: {file_path.name}
|
| 828 |
+
Content:
|
| 829 |
+
```
|
| 830 |
+
{content_for_analysis}{content_suffix}
|
| 831 |
+
```
|
| 832 |
+
|
| 833 |
+
Please provide analysis in this JSON format:
|
| 834 |
+
{{
|
| 835 |
+
"file_type": "description of what type of file this is",
|
| 836 |
+
"main_functions": ["list", "of", "main", "functions", "or", "classes"],
|
| 837 |
+
"key_concepts": ["important", "concepts", "algorithms", "patterns"],
|
| 838 |
+
"dependencies": ["external", "libraries", "or", "imports"],
|
| 839 |
+
"summary": "2-3 sentence summary of what this file does"
|
| 840 |
+
}}
|
| 841 |
+
|
| 842 |
+
Focus on the core functionality and potential reusability.
|
| 843 |
+
"""
|
| 844 |
+
|
| 845 |
+
# Get LLM analysis with configured parameters
|
| 846 |
+
llm_response = await self._call_llm(analysis_prompt, max_tokens=1000)
|
| 847 |
+
|
| 848 |
+
try:
|
| 849 |
+
# Try to parse JSON response
|
| 850 |
+
match = re.search(r"\{.*\}", llm_response, re.DOTALL)
|
| 851 |
+
analysis_data = json.loads(match.group(0))
|
| 852 |
+
except json.JSONDecodeError:
|
| 853 |
+
# Fallback to basic analysis if JSON parsing fails
|
| 854 |
+
analysis_data = {
|
| 855 |
+
"file_type": f"{file_path.suffix} file",
|
| 856 |
+
"main_functions": [],
|
| 857 |
+
"key_concepts": [],
|
| 858 |
+
"dependencies": [],
|
| 859 |
+
"summary": "File analysis failed - JSON parsing error",
|
| 860 |
+
}
|
| 861 |
+
|
| 862 |
+
file_summary = FileSummary(
|
| 863 |
+
file_path=str(file_path.relative_to(self.code_base_path)),
|
| 864 |
+
file_type=analysis_data.get("file_type", "unknown"),
|
| 865 |
+
main_functions=analysis_data.get("main_functions", []),
|
| 866 |
+
key_concepts=analysis_data.get("key_concepts", []),
|
| 867 |
+
dependencies=analysis_data.get("dependencies", []),
|
| 868 |
+
summary=analysis_data.get("summary", "No summary available"),
|
| 869 |
+
lines_of_code=lines_of_code,
|
| 870 |
+
last_modified=datetime.fromtimestamp(stats.st_mtime).isoformat(),
|
| 871 |
+
)
|
| 872 |
+
|
| 873 |
+
# Cache the result if caching is enabled
|
| 874 |
+
if self.enable_content_caching and cache_key:
|
| 875 |
+
self.content_cache[cache_key] = file_summary
|
| 876 |
+
self._manage_cache_size()
|
| 877 |
+
|
| 878 |
+
return file_summary
|
| 879 |
+
|
| 880 |
+
except Exception as e:
|
| 881 |
+
self.logger.error(f"Error analyzing file {file_path}: {e}")
|
| 882 |
+
return FileSummary(
|
| 883 |
+
file_path=str(file_path.relative_to(self.code_base_path)),
|
| 884 |
+
file_type="error",
|
| 885 |
+
main_functions=[],
|
| 886 |
+
key_concepts=[],
|
| 887 |
+
dependencies=[],
|
| 888 |
+
summary=f"Analysis failed: {str(e)}",
|
| 889 |
+
lines_of_code=0,
|
| 890 |
+
last_modified="",
|
| 891 |
+
)
|
| 892 |
+
|
| 893 |
+
async def find_relationships(
|
| 894 |
+
self, file_summary: FileSummary
|
| 895 |
+
) -> List[FileRelationship]:
|
| 896 |
+
"""Find relationships between a repo file and target structure"""
|
| 897 |
+
|
| 898 |
+
# Build relationship type description from config
|
| 899 |
+
relationship_type_desc = []
|
| 900 |
+
for rel_type, weight in self.relationship_types.items():
|
| 901 |
+
relationship_type_desc.append(f"- {rel_type} (priority: {weight})")
|
| 902 |
+
|
| 903 |
+
relationship_prompt = f"""
|
| 904 |
+
Analyze the relationship between this existing code file and the target project structure.
|
| 905 |
+
|
| 906 |
+
Existing File Analysis:
|
| 907 |
+
- Path: {file_summary.file_path}
|
| 908 |
+
- Type: {file_summary.file_type}
|
| 909 |
+
- Functions: {', '.join(file_summary.main_functions)}
|
| 910 |
+
- Concepts: {', '.join(file_summary.key_concepts)}
|
| 911 |
+
- Summary: {file_summary.summary}
|
| 912 |
+
|
| 913 |
+
Target Project Structure:
|
| 914 |
+
{self.target_structure}
|
| 915 |
+
|
| 916 |
+
Available relationship types (with priority weights):
|
| 917 |
+
{chr(10).join(relationship_type_desc)}
|
| 918 |
+
|
| 919 |
+
Identify potential relationships and provide analysis in this JSON format:
|
| 920 |
+
{{
|
| 921 |
+
"relationships": [
|
| 922 |
+
{{
|
| 923 |
+
"target_file_path": "path/in/target/structure",
|
| 924 |
+
"relationship_type": "direct_match|partial_match|reference|utility",
|
| 925 |
+
"confidence_score": 0.0-1.0,
|
| 926 |
+
"helpful_aspects": ["specific", "aspects", "that", "could", "help"],
|
| 927 |
+
"potential_contributions": ["how", "this", "could", "contribute"],
|
| 928 |
+
"usage_suggestions": "detailed suggestion on how to use this file"
|
| 929 |
+
}}
|
| 930 |
+
]
|
| 931 |
+
}}
|
| 932 |
+
|
| 933 |
+
Consider the priority weights when determining relationship types. Higher weight types should be preferred when multiple types apply.
|
| 934 |
+
Only include relationships with confidence > {self.min_confidence_score}. Focus on concrete, actionable connections.
|
| 935 |
+
"""
|
| 936 |
+
|
| 937 |
+
try:
|
| 938 |
+
llm_response = await self._call_llm(relationship_prompt, max_tokens=1500)
|
| 939 |
+
|
| 940 |
+
match = re.search(r"\{.*\}", llm_response, re.DOTALL)
|
| 941 |
+
relationship_data = json.loads(match.group(0))
|
| 942 |
+
|
| 943 |
+
relationships = []
|
| 944 |
+
for rel_data in relationship_data.get("relationships", []):
|
| 945 |
+
confidence_score = float(rel_data.get("confidence_score", 0.0))
|
| 946 |
+
relationship_type = rel_data.get("relationship_type", "reference")
|
| 947 |
+
|
| 948 |
+
# Validate relationship type is in config
|
| 949 |
+
if relationship_type not in self.relationship_types:
|
| 950 |
+
if self.verbose_output:
|
| 951 |
+
self.logger.warning(
|
| 952 |
+
f"Unknown relationship type '{relationship_type}', using 'reference'"
|
| 953 |
+
)
|
| 954 |
+
relationship_type = "reference"
|
| 955 |
+
|
| 956 |
+
# Apply configured minimum confidence filter
|
| 957 |
+
if confidence_score > self.min_confidence_score:
|
| 958 |
+
relationship = FileRelationship(
|
| 959 |
+
repo_file_path=file_summary.file_path,
|
| 960 |
+
target_file_path=rel_data.get("target_file_path", ""),
|
| 961 |
+
relationship_type=relationship_type,
|
| 962 |
+
confidence_score=confidence_score,
|
| 963 |
+
helpful_aspects=rel_data.get("helpful_aspects", []),
|
| 964 |
+
potential_contributions=rel_data.get(
|
| 965 |
+
"potential_contributions", []
|
| 966 |
+
),
|
| 967 |
+
usage_suggestions=rel_data.get("usage_suggestions", ""),
|
| 968 |
+
)
|
| 969 |
+
relationships.append(relationship)
|
| 970 |
+
|
| 971 |
+
return relationships
|
| 972 |
+
|
| 973 |
+
except Exception as e:
|
| 974 |
+
self.logger.error(
|
| 975 |
+
f"Error finding relationships for {file_summary.file_path}: {e}"
|
| 976 |
+
)
|
| 977 |
+
return []
|
| 978 |
+
|
| 979 |
+
async def _analyze_single_file_with_relationships(
|
| 980 |
+
self, file_path: Path, index: int, total: int
|
| 981 |
+
) -> tuple:
|
| 982 |
+
"""Analyze a single file and its relationships (for concurrent processing)"""
|
| 983 |
+
if self.verbose_output:
|
| 984 |
+
self.logger.info(f"Analyzing file {index}/{total}: {file_path.name}")
|
| 985 |
+
|
| 986 |
+
# Get file summary
|
| 987 |
+
file_summary = await self.analyze_file_content(file_path)
|
| 988 |
+
|
| 989 |
+
# Find relationships
|
| 990 |
+
relationships = await self.find_relationships(file_summary)
|
| 991 |
+
|
| 992 |
+
return file_summary, relationships
|
| 993 |
+
|
| 994 |
+
async def process_repository(self, repo_path: Path) -> RepoIndex:
|
| 995 |
+
"""Process a single repository and create complete index with optional concurrent processing"""
|
| 996 |
+
repo_name = repo_path.name
|
| 997 |
+
self.logger.info(f"Processing repository: {repo_name}")
|
| 998 |
+
|
| 999 |
+
# Step 1: Generate file tree
|
| 1000 |
+
self.logger.info("Generating file tree structure...")
|
| 1001 |
+
file_tree = self.generate_file_tree(repo_path)
|
| 1002 |
+
|
| 1003 |
+
# Step 2: Get all files
|
| 1004 |
+
all_files = self.get_all_repo_files(repo_path)
|
| 1005 |
+
self.logger.info(f"Found {len(all_files)} files in {repo_name}")
|
| 1006 |
+
|
| 1007 |
+
# Step 3: LLM pre-filtering of relevant files
|
| 1008 |
+
if self.enable_pre_filtering:
|
| 1009 |
+
self.logger.info("Using LLM for file pre-filtering...")
|
| 1010 |
+
selected_file_paths = await self.pre_filter_files(repo_path, file_tree)
|
| 1011 |
+
else:
|
| 1012 |
+
self.logger.info("Pre-filtering is disabled, will analyze all files")
|
| 1013 |
+
selected_file_paths = []
|
| 1014 |
+
|
| 1015 |
+
# Step 4: Filter file list based on filtering results
|
| 1016 |
+
if selected_file_paths:
|
| 1017 |
+
files_to_analyze = self.filter_files_by_paths(
|
| 1018 |
+
all_files, selected_file_paths, repo_path
|
| 1019 |
+
)
|
| 1020 |
+
self.logger.info(
|
| 1021 |
+
f"After LLM filtering, will analyze {len(files_to_analyze)} relevant files (from {len(all_files)} total)"
|
| 1022 |
+
)
|
| 1023 |
+
else:
|
| 1024 |
+
files_to_analyze = all_files
|
| 1025 |
+
self.logger.info("LLM filtering failed, will analyze all files")
|
| 1026 |
+
|
| 1027 |
+
# Step 5: Analyze filtered files (concurrent or sequential)
|
| 1028 |
+
if self.enable_concurrent_analysis and len(files_to_analyze) > 1:
|
| 1029 |
+
self.logger.info(
|
| 1030 |
+
f"Using concurrent analysis with max {self.max_concurrent_files} parallel files"
|
| 1031 |
+
)
|
| 1032 |
+
file_summaries, all_relationships = await self._process_files_concurrently(
|
| 1033 |
+
files_to_analyze
|
| 1034 |
+
)
|
| 1035 |
+
else:
|
| 1036 |
+
self.logger.info("Using sequential file analysis")
|
| 1037 |
+
file_summaries, all_relationships = await self._process_files_sequentially(
|
| 1038 |
+
files_to_analyze
|
| 1039 |
+
)
|
| 1040 |
+
|
| 1041 |
+
# Step 6: Create repository index
|
| 1042 |
+
repo_index = RepoIndex(
|
| 1043 |
+
repo_name=repo_name,
|
| 1044 |
+
total_files=len(all_files), # Record original file count
|
| 1045 |
+
file_summaries=file_summaries,
|
| 1046 |
+
relationships=all_relationships,
|
| 1047 |
+
analysis_metadata={
|
| 1048 |
+
"analysis_date": datetime.now().isoformat(),
|
| 1049 |
+
"target_structure_analyzed": self.target_structure[:200] + "...",
|
| 1050 |
+
"total_relationships_found": len(all_relationships),
|
| 1051 |
+
"high_confidence_relationships": len(
|
| 1052 |
+
[
|
| 1053 |
+
r
|
| 1054 |
+
for r in all_relationships
|
| 1055 |
+
if r.confidence_score > self.high_confidence_threshold
|
| 1056 |
+
]
|
| 1057 |
+
),
|
| 1058 |
+
"analyzer_version": "1.4.0", # Updated version to reflect augmented LLM support
|
| 1059 |
+
"pre_filtering_enabled": self.enable_pre_filtering,
|
| 1060 |
+
"files_before_filtering": len(all_files),
|
| 1061 |
+
"files_after_filtering": len(files_to_analyze),
|
| 1062 |
+
"filtering_efficiency": round(
|
| 1063 |
+
(1 - len(files_to_analyze) / len(all_files)) * 100, 2
|
| 1064 |
+
)
|
| 1065 |
+
if all_files
|
| 1066 |
+
else 0,
|
| 1067 |
+
"config_file_used": self.indexer_config_path,
|
| 1068 |
+
"min_confidence_score": self.min_confidence_score,
|
| 1069 |
+
"high_confidence_threshold": self.high_confidence_threshold,
|
| 1070 |
+
"concurrent_analysis_used": self.enable_concurrent_analysis,
|
| 1071 |
+
"content_caching_enabled": self.enable_content_caching,
|
| 1072 |
+
"cache_hits": len(self.content_cache) if self.content_cache else 0,
|
| 1073 |
+
},
|
| 1074 |
+
)
|
| 1075 |
+
|
| 1076 |
+
return repo_index
|
| 1077 |
+
|
| 1078 |
+
async def _process_files_sequentially(self, files_to_analyze: list) -> tuple:
|
| 1079 |
+
"""Process files sequentially (original method)"""
|
| 1080 |
+
file_summaries = []
|
| 1081 |
+
all_relationships = []
|
| 1082 |
+
|
| 1083 |
+
for i, file_path in enumerate(files_to_analyze, 1):
|
| 1084 |
+
(
|
| 1085 |
+
file_summary,
|
| 1086 |
+
relationships,
|
| 1087 |
+
) = await self._analyze_single_file_with_relationships(
|
| 1088 |
+
file_path, i, len(files_to_analyze)
|
| 1089 |
+
)
|
| 1090 |
+
file_summaries.append(file_summary)
|
| 1091 |
+
all_relationships.extend(relationships)
|
| 1092 |
+
|
| 1093 |
+
# Add configured delay to avoid overwhelming the LLM API
|
| 1094 |
+
await asyncio.sleep(self.request_delay)
|
| 1095 |
+
|
| 1096 |
+
return file_summaries, all_relationships
|
| 1097 |
+
|
| 1098 |
+
async def _process_files_concurrently(self, files_to_analyze: list) -> tuple:
|
| 1099 |
+
"""Process files concurrently with semaphore limiting"""
|
| 1100 |
+
file_summaries = []
|
| 1101 |
+
all_relationships = []
|
| 1102 |
+
|
| 1103 |
+
# Create semaphore to limit concurrent tasks
|
| 1104 |
+
semaphore = asyncio.Semaphore(self.max_concurrent_files)
|
| 1105 |
+
tasks = []
|
| 1106 |
+
|
| 1107 |
+
async def _process_with_semaphore(file_path: Path, index: int, total: int):
|
| 1108 |
+
async with semaphore:
|
| 1109 |
+
# Add a small delay to space out concurrent requests
|
| 1110 |
+
if index > 1:
|
| 1111 |
+
await asyncio.sleep(
|
| 1112 |
+
self.request_delay * 0.5
|
| 1113 |
+
) # Reduced delay for concurrent processing
|
| 1114 |
+
return await self._analyze_single_file_with_relationships(
|
| 1115 |
+
file_path, index, total
|
| 1116 |
+
)
|
| 1117 |
+
|
| 1118 |
+
try:
|
| 1119 |
+
# Create tasks for all files
|
| 1120 |
+
tasks = [
|
| 1121 |
+
_process_with_semaphore(file_path, i, len(files_to_analyze))
|
| 1122 |
+
for i, file_path in enumerate(files_to_analyze, 1)
|
| 1123 |
+
]
|
| 1124 |
+
|
| 1125 |
+
# Process tasks and collect results
|
| 1126 |
+
if self.verbose_output:
|
| 1127 |
+
self.logger.info(
|
| 1128 |
+
f"Starting concurrent analysis of {len(tasks)} files..."
|
| 1129 |
+
)
|
| 1130 |
+
|
| 1131 |
+
try:
|
| 1132 |
+
results = await asyncio.gather(*tasks, return_exceptions=True)
|
| 1133 |
+
|
| 1134 |
+
for i, result in enumerate(results):
|
| 1135 |
+
if isinstance(result, Exception):
|
| 1136 |
+
self.logger.error(
|
| 1137 |
+
f"Failed to analyze file {files_to_analyze[i]}: {result}"
|
| 1138 |
+
)
|
| 1139 |
+
# Create error summary
|
| 1140 |
+
error_summary = FileSummary(
|
| 1141 |
+
file_path=str(
|
| 1142 |
+
files_to_analyze[i].relative_to(self.code_base_path)
|
| 1143 |
+
),
|
| 1144 |
+
file_type="error",
|
| 1145 |
+
main_functions=[],
|
| 1146 |
+
key_concepts=[],
|
| 1147 |
+
dependencies=[],
|
| 1148 |
+
summary=f"Concurrent analysis failed: {str(result)}",
|
| 1149 |
+
lines_of_code=0,
|
| 1150 |
+
last_modified="",
|
| 1151 |
+
)
|
| 1152 |
+
file_summaries.append(error_summary)
|
| 1153 |
+
else:
|
| 1154 |
+
file_summary, relationships = result
|
| 1155 |
+
file_summaries.append(file_summary)
|
| 1156 |
+
all_relationships.extend(relationships)
|
| 1157 |
+
|
| 1158 |
+
except Exception as e:
|
| 1159 |
+
self.logger.error(f"Concurrent processing failed: {e}")
|
| 1160 |
+
# Cancel any remaining tasks
|
| 1161 |
+
for task in tasks:
|
| 1162 |
+
if not task.done() and not task.cancelled():
|
| 1163 |
+
task.cancel()
|
| 1164 |
+
|
| 1165 |
+
# Wait for cancelled tasks to complete
|
| 1166 |
+
try:
|
| 1167 |
+
await asyncio.sleep(0.1) # Brief wait for cancellation
|
| 1168 |
+
except Exception:
|
| 1169 |
+
pass
|
| 1170 |
+
|
| 1171 |
+
# Fallback to sequential processing
|
| 1172 |
+
self.logger.info("Falling back to sequential processing...")
|
| 1173 |
+
return await self._process_files_sequentially(files_to_analyze)
|
| 1174 |
+
|
| 1175 |
+
if self.verbose_output:
|
| 1176 |
+
self.logger.info(
|
| 1177 |
+
f"Concurrent analysis completed: {len(file_summaries)} files processed"
|
| 1178 |
+
)
|
| 1179 |
+
|
| 1180 |
+
return file_summaries, all_relationships
|
| 1181 |
+
|
| 1182 |
+
except Exception as e:
|
| 1183 |
+
# Ensure all tasks are cancelled in case of unexpected errors
|
| 1184 |
+
if tasks:
|
| 1185 |
+
for task in tasks:
|
| 1186 |
+
if not task.done() and not task.cancelled():
|
| 1187 |
+
task.cancel()
|
| 1188 |
+
|
| 1189 |
+
# Wait briefly for cancellation to complete
|
| 1190 |
+
try:
|
| 1191 |
+
await asyncio.sleep(0.1)
|
| 1192 |
+
except Exception:
|
| 1193 |
+
pass
|
| 1194 |
+
|
| 1195 |
+
self.logger.error(f"Critical error in concurrent processing: {e}")
|
| 1196 |
+
# Fallback to sequential processing
|
| 1197 |
+
self.logger.info(
|
| 1198 |
+
"Falling back to sequential processing due to critical error..."
|
| 1199 |
+
)
|
| 1200 |
+
return await self._process_files_sequentially(files_to_analyze)
|
| 1201 |
+
|
| 1202 |
+
finally:
|
| 1203 |
+
# Final cleanup: ensure all tasks are properly finished
|
| 1204 |
+
if tasks:
|
| 1205 |
+
for task in tasks:
|
| 1206 |
+
if not task.done() and not task.cancelled():
|
| 1207 |
+
task.cancel()
|
| 1208 |
+
|
| 1209 |
+
# Clear task references to help with garbage collection
|
| 1210 |
+
tasks.clear()
|
| 1211 |
+
|
| 1212 |
+
# Force garbage collection to help clean up semaphore and related resources
|
| 1213 |
+
import gc
|
| 1214 |
+
|
| 1215 |
+
gc.collect()
|
| 1216 |
+
|
| 1217 |
+
async def build_all_indexes(self) -> Dict[str, str]:
|
| 1218 |
+
"""Build indexes for all repositories in code_base"""
|
| 1219 |
+
if not self.code_base_path.exists():
|
| 1220 |
+
raise FileNotFoundError(
|
| 1221 |
+
f"Code base path does not exist: {self.code_base_path}"
|
| 1222 |
+
)
|
| 1223 |
+
|
| 1224 |
+
# Get all repository directories
|
| 1225 |
+
repo_dirs = [
|
| 1226 |
+
d
|
| 1227 |
+
for d in self.code_base_path.iterdir()
|
| 1228 |
+
if d.is_dir() and not d.name.startswith(".")
|
| 1229 |
+
]
|
| 1230 |
+
|
| 1231 |
+
if not repo_dirs:
|
| 1232 |
+
raise ValueError(f"No repositories found in {self.code_base_path}")
|
| 1233 |
+
|
| 1234 |
+
self.logger.info(f"Found {len(repo_dirs)} repositories to process")
|
| 1235 |
+
|
| 1236 |
+
# Process each repository
|
| 1237 |
+
output_files = {}
|
| 1238 |
+
statistics_data = []
|
| 1239 |
+
|
| 1240 |
+
for repo_dir in repo_dirs:
|
| 1241 |
+
try:
|
| 1242 |
+
# Process repository
|
| 1243 |
+
repo_index = await self.process_repository(repo_dir)
|
| 1244 |
+
|
| 1245 |
+
# Generate output filename using configured pattern
|
| 1246 |
+
output_filename = self.index_filename_pattern.format(
|
| 1247 |
+
repo_name=repo_index.repo_name
|
| 1248 |
+
)
|
| 1249 |
+
output_file = self.output_dir / output_filename
|
| 1250 |
+
|
| 1251 |
+
# Get output configuration
|
| 1252 |
+
output_config = self.indexer_config.get("output", {})
|
| 1253 |
+
json_indent = output_config.get("json_indent", 2)
|
| 1254 |
+
ensure_ascii = not output_config.get("ensure_ascii", False)
|
| 1255 |
+
|
| 1256 |
+
# Save to JSON file
|
| 1257 |
+
with open(output_file, "w", encoding="utf-8") as f:
|
| 1258 |
+
if self.include_metadata:
|
| 1259 |
+
json.dump(
|
| 1260 |
+
asdict(repo_index),
|
| 1261 |
+
f,
|
| 1262 |
+
indent=json_indent,
|
| 1263 |
+
ensure_ascii=ensure_ascii,
|
| 1264 |
+
)
|
| 1265 |
+
else:
|
| 1266 |
+
# Save without metadata if disabled
|
| 1267 |
+
index_data = asdict(repo_index)
|
| 1268 |
+
index_data.pop("analysis_metadata", None)
|
| 1269 |
+
json.dump(
|
| 1270 |
+
index_data, f, indent=json_indent, ensure_ascii=ensure_ascii
|
| 1271 |
+
)
|
| 1272 |
+
|
| 1273 |
+
output_files[repo_index.repo_name] = str(output_file)
|
| 1274 |
+
self.logger.info(
|
| 1275 |
+
f"Saved index for {repo_index.repo_name} to {output_file}"
|
| 1276 |
+
)
|
| 1277 |
+
|
| 1278 |
+
# Collect statistics for report
|
| 1279 |
+
if self.generate_statistics:
|
| 1280 |
+
stats = self._extract_repository_statistics(repo_index)
|
| 1281 |
+
statistics_data.append(stats)
|
| 1282 |
+
|
| 1283 |
+
except Exception as e:
|
| 1284 |
+
self.logger.error(f"Failed to process repository {repo_dir.name}: {e}")
|
| 1285 |
+
continue
|
| 1286 |
+
|
| 1287 |
+
# Generate additional reports if configured
|
| 1288 |
+
if self.generate_summary:
|
| 1289 |
+
summary_path = self.generate_summary_report(output_files)
|
| 1290 |
+
self.logger.info(f"Generated summary report: {summary_path}")
|
| 1291 |
+
|
| 1292 |
+
if self.generate_statistics:
|
| 1293 |
+
stats_path = self.generate_statistics_report(statistics_data)
|
| 1294 |
+
self.logger.info(f"Generated statistics report: {stats_path}")
|
| 1295 |
+
|
| 1296 |
+
return output_files
|
| 1297 |
+
|
| 1298 |
+
def _extract_repository_statistics(self, repo_index: RepoIndex) -> Dict[str, Any]:
|
| 1299 |
+
"""Extract statistical information from a repository index"""
|
| 1300 |
+
metadata = repo_index.analysis_metadata
|
| 1301 |
+
|
| 1302 |
+
# Count relationship types
|
| 1303 |
+
relationship_type_counts = {}
|
| 1304 |
+
for rel in repo_index.relationships:
|
| 1305 |
+
rel_type = rel.relationship_type
|
| 1306 |
+
relationship_type_counts[rel_type] = (
|
| 1307 |
+
relationship_type_counts.get(rel_type, 0) + 1
|
| 1308 |
+
)
|
| 1309 |
+
|
| 1310 |
+
# Count file types
|
| 1311 |
+
file_type_counts = {}
|
| 1312 |
+
for file_summary in repo_index.file_summaries:
|
| 1313 |
+
file_type = file_summary.file_type
|
| 1314 |
+
file_type_counts[file_type] = file_type_counts.get(file_type, 0) + 1
|
| 1315 |
+
|
| 1316 |
+
# Calculate statistics
|
| 1317 |
+
total_lines = sum(fs.lines_of_code for fs in repo_index.file_summaries)
|
| 1318 |
+
avg_lines = (
|
| 1319 |
+
total_lines / len(repo_index.file_summaries)
|
| 1320 |
+
if repo_index.file_summaries
|
| 1321 |
+
else 0
|
| 1322 |
+
)
|
| 1323 |
+
|
| 1324 |
+
avg_confidence = (
|
| 1325 |
+
sum(r.confidence_score for r in repo_index.relationships)
|
| 1326 |
+
/ len(repo_index.relationships)
|
| 1327 |
+
if repo_index.relationships
|
| 1328 |
+
else 0
|
| 1329 |
+
)
|
| 1330 |
+
|
| 1331 |
+
return {
|
| 1332 |
+
"repo_name": repo_index.repo_name,
|
| 1333 |
+
"total_files": repo_index.total_files,
|
| 1334 |
+
"analyzed_files": len(repo_index.file_summaries),
|
| 1335 |
+
"total_relationships": len(repo_index.relationships),
|
| 1336 |
+
"high_confidence_relationships": metadata.get(
|
| 1337 |
+
"high_confidence_relationships", 0
|
| 1338 |
+
),
|
| 1339 |
+
"relationship_type_counts": relationship_type_counts,
|
| 1340 |
+
"file_type_counts": file_type_counts,
|
| 1341 |
+
"total_lines_of_code": total_lines,
|
| 1342 |
+
"average_lines_per_file": round(avg_lines, 2),
|
| 1343 |
+
"average_confidence_score": round(avg_confidence, 3),
|
| 1344 |
+
"filtering_efficiency": metadata.get("filtering_efficiency", 0),
|
| 1345 |
+
"concurrent_analysis_used": metadata.get("concurrent_analysis_used", False),
|
| 1346 |
+
"cache_hits": metadata.get("cache_hits", 0),
|
| 1347 |
+
"analysis_date": metadata.get("analysis_date", "unknown"),
|
| 1348 |
+
}
|
| 1349 |
+
|
| 1350 |
+
def generate_statistics_report(self, statistics_data: List[Dict[str, Any]]) -> str:
|
| 1351 |
+
"""Generate a detailed statistics report"""
|
| 1352 |
+
stats_path = self.output_dir / self.stats_filename
|
| 1353 |
+
|
| 1354 |
+
# Calculate aggregate statistics
|
| 1355 |
+
total_repos = len(statistics_data)
|
| 1356 |
+
total_files_analyzed = sum(stat["analyzed_files"] for stat in statistics_data)
|
| 1357 |
+
total_relationships = sum(
|
| 1358 |
+
stat["total_relationships"] for stat in statistics_data
|
| 1359 |
+
)
|
| 1360 |
+
total_lines = sum(stat["total_lines_of_code"] for stat in statistics_data)
|
| 1361 |
+
|
| 1362 |
+
# Aggregate relationship types
|
| 1363 |
+
aggregated_rel_types = {}
|
| 1364 |
+
for stat in statistics_data:
|
| 1365 |
+
for rel_type, count in stat["relationship_type_counts"].items():
|
| 1366 |
+
aggregated_rel_types[rel_type] = (
|
| 1367 |
+
aggregated_rel_types.get(rel_type, 0) + count
|
| 1368 |
+
)
|
| 1369 |
+
|
| 1370 |
+
# Aggregate file types
|
| 1371 |
+
aggregated_file_types = {}
|
| 1372 |
+
for stat in statistics_data:
|
| 1373 |
+
for file_type, count in stat["file_type_counts"].items():
|
| 1374 |
+
aggregated_file_types[file_type] = (
|
| 1375 |
+
aggregated_file_types.get(file_type, 0) + count
|
| 1376 |
+
)
|
| 1377 |
+
|
| 1378 |
+
# Calculate averages
|
| 1379 |
+
avg_files_per_repo = total_files_analyzed / total_repos if total_repos else 0
|
| 1380 |
+
avg_relationships_per_repo = (
|
| 1381 |
+
total_relationships / total_repos if total_repos else 0
|
| 1382 |
+
)
|
| 1383 |
+
avg_lines_per_repo = total_lines / total_repos if total_repos else 0
|
| 1384 |
+
|
| 1385 |
+
# Build statistics report
|
| 1386 |
+
statistics_report = {
|
| 1387 |
+
"report_generation_time": datetime.now().isoformat(),
|
| 1388 |
+
"analyzer_version": "1.4.0",
|
| 1389 |
+
"configuration_used": {
|
| 1390 |
+
"config_file": self.indexer_config_path,
|
| 1391 |
+
"concurrent_analysis_enabled": self.enable_concurrent_analysis,
|
| 1392 |
+
"content_caching_enabled": self.enable_content_caching,
|
| 1393 |
+
"pre_filtering_enabled": self.enable_pre_filtering,
|
| 1394 |
+
"min_confidence_score": self.min_confidence_score,
|
| 1395 |
+
"high_confidence_threshold": self.high_confidence_threshold,
|
| 1396 |
+
},
|
| 1397 |
+
"aggregate_statistics": {
|
| 1398 |
+
"total_repositories_processed": total_repos,
|
| 1399 |
+
"total_files_analyzed": total_files_analyzed,
|
| 1400 |
+
"total_relationships_found": total_relationships,
|
| 1401 |
+
"total_lines_of_code": total_lines,
|
| 1402 |
+
"average_files_per_repository": round(avg_files_per_repo, 2),
|
| 1403 |
+
"average_relationships_per_repository": round(
|
| 1404 |
+
avg_relationships_per_repo, 2
|
| 1405 |
+
),
|
| 1406 |
+
"average_lines_per_repository": round(avg_lines_per_repo, 2),
|
| 1407 |
+
},
|
| 1408 |
+
"relationship_type_distribution": aggregated_rel_types,
|
| 1409 |
+
"file_type_distribution": aggregated_file_types,
|
| 1410 |
+
"repository_details": statistics_data,
|
| 1411 |
+
"performance_metrics": {
|
| 1412 |
+
"concurrent_processing_repos": sum(
|
| 1413 |
+
1
|
| 1414 |
+
for s in statistics_data
|
| 1415 |
+
if s.get("concurrent_analysis_used", False)
|
| 1416 |
+
),
|
| 1417 |
+
"cache_efficiency": {
|
| 1418 |
+
"total_cache_hits": sum(
|
| 1419 |
+
s.get("cache_hits", 0) for s in statistics_data
|
| 1420 |
+
),
|
| 1421 |
+
"repositories_with_caching": sum(
|
| 1422 |
+
1 for s in statistics_data if s.get("cache_hits", 0) > 0
|
| 1423 |
+
),
|
| 1424 |
+
},
|
| 1425 |
+
"filtering_efficiency": {
|
| 1426 |
+
"average_filtering_efficiency": round(
|
| 1427 |
+
sum(s.get("filtering_efficiency", 0) for s in statistics_data)
|
| 1428 |
+
/ total_repos,
|
| 1429 |
+
2,
|
| 1430 |
+
)
|
| 1431 |
+
if total_repos
|
| 1432 |
+
else 0,
|
| 1433 |
+
"max_filtering_efficiency": max(
|
| 1434 |
+
(s.get("filtering_efficiency", 0) for s in statistics_data),
|
| 1435 |
+
default=0,
|
| 1436 |
+
),
|
| 1437 |
+
"min_filtering_efficiency": min(
|
| 1438 |
+
(s.get("filtering_efficiency", 0) for s in statistics_data),
|
| 1439 |
+
default=0,
|
| 1440 |
+
),
|
| 1441 |
+
},
|
| 1442 |
+
},
|
| 1443 |
+
}
|
| 1444 |
+
|
| 1445 |
+
# Get output configuration
|
| 1446 |
+
output_config = self.indexer_config.get("output", {})
|
| 1447 |
+
json_indent = output_config.get("json_indent", 2)
|
| 1448 |
+
ensure_ascii = not output_config.get("ensure_ascii", False)
|
| 1449 |
+
|
| 1450 |
+
with open(stats_path, "w", encoding="utf-8") as f:
|
| 1451 |
+
json.dump(
|
| 1452 |
+
statistics_report, f, indent=json_indent, ensure_ascii=ensure_ascii
|
| 1453 |
+
)
|
| 1454 |
+
|
| 1455 |
+
return str(stats_path)
|
| 1456 |
+
|
| 1457 |
+
def generate_summary_report(self, output_files: Dict[str, str]) -> str:
|
| 1458 |
+
"""Generate a summary report of all indexes created"""
|
| 1459 |
+
report_path = self.output_dir / "indexing_summary.json"
|
| 1460 |
+
|
| 1461 |
+
# Get output configuration from config file
|
| 1462 |
+
output_config = self.indexer_config.get("output", {})
|
| 1463 |
+
json_indent = output_config.get("json_indent", 2)
|
| 1464 |
+
ensure_ascii = not output_config.get("ensure_ascii", False)
|
| 1465 |
+
|
| 1466 |
+
summary_data = {
|
| 1467 |
+
"indexing_completion_time": datetime.now().isoformat(),
|
| 1468 |
+
"total_repositories_processed": len(output_files),
|
| 1469 |
+
"output_files": output_files,
|
| 1470 |
+
"target_structure": self.target_structure,
|
| 1471 |
+
"code_base_path": str(self.code_base_path),
|
| 1472 |
+
"configuration": {
|
| 1473 |
+
"config_file_used": self.indexer_config_path,
|
| 1474 |
+
"api_config_file": self.config_path,
|
| 1475 |
+
"pre_filtering_enabled": self.enable_pre_filtering,
|
| 1476 |
+
"min_confidence_score": self.min_confidence_score,
|
| 1477 |
+
"high_confidence_threshold": self.high_confidence_threshold,
|
| 1478 |
+
"max_file_size": self.max_file_size,
|
| 1479 |
+
"max_content_length": self.max_content_length,
|
| 1480 |
+
"request_delay": self.request_delay,
|
| 1481 |
+
"supported_extensions_count": len(self.supported_extensions),
|
| 1482 |
+
"skip_directories_count": len(self.skip_directories),
|
| 1483 |
+
},
|
| 1484 |
+
}
|
| 1485 |
+
|
| 1486 |
+
with open(report_path, "w", encoding="utf-8") as f:
|
| 1487 |
+
json.dump(summary_data, f, indent=json_indent, ensure_ascii=ensure_ascii)
|
| 1488 |
+
|
| 1489 |
+
return str(report_path)
|
| 1490 |
+
|
| 1491 |
+
|
| 1492 |
+
async def main():
|
| 1493 |
+
"""Main function to run the code indexer with full configuration support"""
|
| 1494 |
+
|
| 1495 |
+
# Configuration - can be overridden by config file
|
| 1496 |
+
config_file = "DeepCode/tools/indexer_config.yaml"
|
| 1497 |
+
api_config_file = "DeepCode/mcp_agent.secrets.yaml"
|
| 1498 |
+
|
| 1499 |
+
# You can override these parameters or let them be read from config
|
| 1500 |
+
code_base_path = "DeepCode/deepcode_lab/papers/1/code_base/" # Will use config file value if None
|
| 1501 |
+
output_dir = (
|
| 1502 |
+
"DeepCode/deepcode_lab/papers/1/indexes/" # Will use config file value if None
|
| 1503 |
+
)
|
| 1504 |
+
|
| 1505 |
+
# Target structure - this should be customized for your specific project
|
| 1506 |
+
target_structure = """
|
| 1507 |
+
project/
|
| 1508 |
+
├── src/
|
| 1509 |
+
│ ├── core/
|
| 1510 |
+
│ │ ├── gcn.py # GCN encoder
|
| 1511 |
+
│ │ ├── diffusion.py # forward/reverse processes
|
| 1512 |
+
│ │ ├── denoiser.py # denoising MLP
|
| 1513 |
+
│ │ └── fusion.py # fusion combiner
|
| 1514 |
+
│ ├── models/ # model wrapper classes
|
| 1515 |
+
│ │ └── recdiff.py
|
| 1516 |
+
│ ├── utils/
|
| 1517 |
+
│ │ ├── data.py # loading & preprocessing
|
| 1518 |
+
│ │ ├── predictor.py # scoring functions
|
| 1519 |
+
│ │ ├── loss.py # loss functions
|
| 1520 |
+
│ │ ├── metrics.py # NDCG, Recall etc.
|
| 1521 |
+
│ │ └── sched.py # beta/alpha schedule utils
|
| 1522 |
+
│ └── configs/
|
| 1523 |
+
│ └── default.yaml # hyperparameters, paths
|
| 1524 |
+
├── tests/
|
| 1525 |
+
│ ├── test_gcn.py
|
| 1526 |
+
│ ├── test_diffusion.py
|
| 1527 |
+
│ ├── test_denoiser.py
|
| 1528 |
+
│ ├── test_loss.py
|
| 1529 |
+
│ └── test_pipeline.py
|
| 1530 |
+
├── docs/
|
| 1531 |
+
│ ├── architecture.md
|
| 1532 |
+
│ ├── api_reference.md
|
| 1533 |
+
│ └── README.md
|
| 1534 |
+
├── experiments/
|
| 1535 |
+
│ ├── run_experiment.py
|
| 1536 |
+
│ └── notebooks/
|
| 1537 |
+
│ └── analysis.ipynb
|
| 1538 |
+
├── requirements.txt
|
| 1539 |
+
└── setup.py
|
| 1540 |
+
"""
|
| 1541 |
+
|
| 1542 |
+
print("🚀 Starting Code Indexer with Enhanced Configuration Support")
|
| 1543 |
+
print(f"📋 Configuration file: {config_file}")
|
| 1544 |
+
print(f"🔑 API configuration file: {api_config_file}")
|
| 1545 |
+
|
| 1546 |
+
# Create indexer with full configuration support
|
| 1547 |
+
try:
|
| 1548 |
+
indexer = CodeIndexer(
|
| 1549 |
+
code_base_path=code_base_path, # None = read from config
|
| 1550 |
+
target_structure=target_structure, # Required - project specific
|
| 1551 |
+
output_dir=output_dir, # None = read from config
|
| 1552 |
+
config_path=api_config_file, # API configuration file
|
| 1553 |
+
indexer_config_path=config_file, # Configuration file
|
| 1554 |
+
enable_pre_filtering=True, # Can be overridden in config
|
| 1555 |
+
)
|
| 1556 |
+
|
| 1557 |
+
# Display configuration information
|
| 1558 |
+
print(f"📁 Code base path: {indexer.code_base_path}")
|
| 1559 |
+
print(f"📂 Output directory: {indexer.output_dir}")
|
| 1560 |
+
print(
|
| 1561 |
+
f"🤖 Default models: Anthropic={indexer.default_models['anthropic']}, OpenAI={indexer.default_models['openai']}"
|
| 1562 |
+
)
|
| 1563 |
+
print(f"🔧 Preferred LLM: {get_preferred_llm_class(api_config_file).__name__}")
|
| 1564 |
+
print(
|
| 1565 |
+
f"⚡ Concurrent analysis: {'enabled' if indexer.enable_concurrent_analysis else 'disabled'}"
|
| 1566 |
+
)
|
| 1567 |
+
print(
|
| 1568 |
+
f"🗄️ Content caching: {'enabled' if indexer.enable_content_caching else 'disabled'}"
|
| 1569 |
+
)
|
| 1570 |
+
print(
|
| 1571 |
+
f"🔍 Pre-filtering: {'enabled' if indexer.enable_pre_filtering else 'disabled'}"
|
| 1572 |
+
)
|
| 1573 |
+
print(f"🐛 Debug mode: {'enabled' if indexer.verbose_output else 'disabled'}")
|
| 1574 |
+
print(
|
| 1575 |
+
f"🎭 Mock responses: {'enabled' if indexer.mock_llm_responses else 'disabled'}"
|
| 1576 |
+
)
|
| 1577 |
+
|
| 1578 |
+
# Validate configuration
|
| 1579 |
+
if not indexer.code_base_path.exists():
|
| 1580 |
+
raise FileNotFoundError(
|
| 1581 |
+
f"Code base path does not exist: {indexer.code_base_path}"
|
| 1582 |
+
)
|
| 1583 |
+
|
| 1584 |
+
if not target_structure:
|
| 1585 |
+
raise ValueError("Target structure is required for analysis")
|
| 1586 |
+
|
| 1587 |
+
print("\n🔧 Starting indexing process...")
|
| 1588 |
+
|
| 1589 |
+
# Build all indexes
|
| 1590 |
+
output_files = await indexer.build_all_indexes()
|
| 1591 |
+
|
| 1592 |
+
# Display results
|
| 1593 |
+
print("\n✅ Indexing completed successfully!")
|
| 1594 |
+
print(f"📊 Processed {len(output_files)} repositories")
|
| 1595 |
+
print("📁 Output files:")
|
| 1596 |
+
for repo_name, file_path in output_files.items():
|
| 1597 |
+
print(f" - {repo_name}: {file_path}")
|
| 1598 |
+
|
| 1599 |
+
# Display additional reports generated
|
| 1600 |
+
if indexer.generate_summary:
|
| 1601 |
+
summary_file = indexer.output_dir / indexer.summary_filename
|
| 1602 |
+
if summary_file.exists():
|
| 1603 |
+
print(f"📋 Summary report: {summary_file}")
|
| 1604 |
+
|
| 1605 |
+
if indexer.generate_statistics:
|
| 1606 |
+
stats_file = indexer.output_dir / indexer.stats_filename
|
| 1607 |
+
if stats_file.exists():
|
| 1608 |
+
print(f"📈 Statistics report: {stats_file}")
|
| 1609 |
+
|
| 1610 |
+
# Performance information
|
| 1611 |
+
if indexer.enable_content_caching and indexer.content_cache:
|
| 1612 |
+
print(f"🗄️ Cache performance: {len(indexer.content_cache)} items cached")
|
| 1613 |
+
|
| 1614 |
+
print("\n🎉 Code indexing process completed successfully!")
|
| 1615 |
+
|
| 1616 |
+
except FileNotFoundError as e:
|
| 1617 |
+
print(f"❌ File not found error: {e}")
|
| 1618 |
+
print("💡 Please check your configuration file paths")
|
| 1619 |
+
except ValueError as e:
|
| 1620 |
+
print(f"❌ Configuration error: {e}")
|
| 1621 |
+
print("💡 Please check your configuration file settings")
|
| 1622 |
+
except Exception as e:
|
| 1623 |
+
print(f"❌ Indexing failed: {e}")
|
| 1624 |
+
print("💡 Check the logs for more details")
|
| 1625 |
+
|
| 1626 |
+
# Print debug information if available
|
| 1627 |
+
try:
|
| 1628 |
+
indexer
|
| 1629 |
+
if indexer.verbose_output:
|
| 1630 |
+
import traceback
|
| 1631 |
+
|
| 1632 |
+
print("\n🐛 Debug information:")
|
| 1633 |
+
traceback.print_exc()
|
| 1634 |
+
except NameError:
|
| 1635 |
+
pass
|
| 1636 |
+
|
| 1637 |
+
|
| 1638 |
+
def print_usage_example():
|
| 1639 |
+
"""Print usage examples for different scenarios"""
|
| 1640 |
+
print("""
|
| 1641 |
+
📖 Code Indexer Usage Examples:
|
| 1642 |
+
|
| 1643 |
+
1. Basic usage with config file:
|
| 1644 |
+
- Update paths in indexer_config.yaml
|
| 1645 |
+
- Run: python code_indexer.py
|
| 1646 |
+
|
| 1647 |
+
2. Enable debugging:
|
| 1648 |
+
- Set debug.verbose_output: true in config
|
| 1649 |
+
- Set debug.save_raw_responses: true to save LLM responses
|
| 1650 |
+
|
| 1651 |
+
3. Enable concurrent processing:
|
| 1652 |
+
- Set performance.enable_concurrent_analysis: true
|
| 1653 |
+
- Adjust performance.max_concurrent_files as needed
|
| 1654 |
+
|
| 1655 |
+
4. Enable caching:
|
| 1656 |
+
- Set performance.enable_content_caching: true
|
| 1657 |
+
- Adjust performance.max_cache_size as needed
|
| 1658 |
+
|
| 1659 |
+
5. Mock mode for testing:
|
| 1660 |
+
- Set debug.mock_llm_responses: true
|
| 1661 |
+
- No API calls will be made
|
| 1662 |
+
|
| 1663 |
+
6. Custom output:
|
| 1664 |
+
- Modify output.index_filename_pattern
|
| 1665 |
+
- Set output.generate_statistics: true for detailed reports
|
| 1666 |
+
|
| 1667 |
+
📋 Configuration file location: tools/indexer_config.yaml
|
| 1668 |
+
""")
|
| 1669 |
+
|
| 1670 |
+
|
| 1671 |
+
if __name__ == "__main__":
|
| 1672 |
+
import sys
|
| 1673 |
+
|
| 1674 |
+
if len(sys.argv) > 1 and sys.argv[1] in ["--help", "-h", "help"]:
|
| 1675 |
+
print_usage_example()
|
| 1676 |
+
else:
|
| 1677 |
+
asyncio.run(main())
|
projects/ui/DeepCode/tools/code_reference_indexer.py
ADDED
|
@@ -0,0 +1,495 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Code Reference Indexer MCP Tool - Unified Version
|
| 4 |
+
|
| 5 |
+
Specialized MCP tool for searching relevant index content in indexes folder
|
| 6 |
+
and formatting it for LLM code implementation reference.
|
| 7 |
+
|
| 8 |
+
Core Features:
|
| 9 |
+
1. **UNIFIED TOOL**: Combined search_code_references that handles directory setup, loading, and searching in one call
|
| 10 |
+
2. Match relevant reference code based on target file path and functionality requirements
|
| 11 |
+
3. Format output of relevant code examples, functions and concepts
|
| 12 |
+
4. Provide structured reference information for LLM use
|
| 13 |
+
|
| 14 |
+
Key Improvement:
|
| 15 |
+
- Single tool call that handles all steps internally
|
| 16 |
+
- Agent only needs to provide indexes_path and target_file
|
| 17 |
+
- No dependency on calling order or global state management
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
import json
|
| 21 |
+
from pathlib import Path
|
| 22 |
+
from typing import Dict, List, Tuple
|
| 23 |
+
from dataclasses import dataclass
|
| 24 |
+
import logging
|
| 25 |
+
|
| 26 |
+
# Import MCP modules
|
| 27 |
+
from mcp.server.fastmcp import FastMCP
|
| 28 |
+
|
| 29 |
+
# Setup logging
|
| 30 |
+
logging.basicConfig(level=logging.INFO)
|
| 31 |
+
logger = logging.getLogger(__name__)
|
| 32 |
+
|
| 33 |
+
# Create FastMCP server instance
|
| 34 |
+
mcp = FastMCP("code-reference-indexer")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@dataclass
|
| 38 |
+
class CodeReference:
|
| 39 |
+
"""Code reference information structure"""
|
| 40 |
+
|
| 41 |
+
file_path: str
|
| 42 |
+
file_type: str
|
| 43 |
+
main_functions: List[str]
|
| 44 |
+
key_concepts: List[str]
|
| 45 |
+
dependencies: List[str]
|
| 46 |
+
summary: str
|
| 47 |
+
lines_of_code: int
|
| 48 |
+
repo_name: str
|
| 49 |
+
confidence_score: float = 0.0
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
@dataclass
|
| 53 |
+
class RelationshipInfo:
|
| 54 |
+
"""Relationship information structure"""
|
| 55 |
+
|
| 56 |
+
repo_file_path: str
|
| 57 |
+
target_file_path: str
|
| 58 |
+
relationship_type: str
|
| 59 |
+
confidence_score: float
|
| 60 |
+
helpful_aspects: List[str]
|
| 61 |
+
potential_contributions: List[str]
|
| 62 |
+
usage_suggestions: str
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def load_index_files_from_directory(indexes_directory: str) -> Dict[str, Dict]:
|
| 66 |
+
"""Load all index files from specified directory"""
|
| 67 |
+
indexes_path = Path(indexes_directory).resolve()
|
| 68 |
+
|
| 69 |
+
if not indexes_path.exists():
|
| 70 |
+
logger.warning(f"Indexes directory does not exist: {indexes_path}")
|
| 71 |
+
return {}
|
| 72 |
+
|
| 73 |
+
index_cache = {}
|
| 74 |
+
|
| 75 |
+
for index_file in indexes_path.glob("*.json"):
|
| 76 |
+
try:
|
| 77 |
+
with open(index_file, "r", encoding="utf-8") as f:
|
| 78 |
+
index_data = json.load(f)
|
| 79 |
+
index_cache[index_file.stem] = index_data
|
| 80 |
+
logger.info(f"Loaded index file: {index_file.name}")
|
| 81 |
+
except Exception as e:
|
| 82 |
+
logger.error(f"Failed to load index file {index_file.name}: {e}")
|
| 83 |
+
|
| 84 |
+
logger.info(f"Loaded {len(index_cache)} index files from {indexes_path}")
|
| 85 |
+
return index_cache
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def extract_code_references(index_data: Dict) -> List[CodeReference]:
|
| 89 |
+
"""Extract code reference information from index data"""
|
| 90 |
+
references = []
|
| 91 |
+
|
| 92 |
+
repo_name = index_data.get("repo_name", "Unknown")
|
| 93 |
+
file_summaries = index_data.get("file_summaries", [])
|
| 94 |
+
|
| 95 |
+
for file_summary in file_summaries:
|
| 96 |
+
reference = CodeReference(
|
| 97 |
+
file_path=file_summary.get("file_path", ""),
|
| 98 |
+
file_type=file_summary.get("file_type", ""),
|
| 99 |
+
main_functions=file_summary.get("main_functions", []),
|
| 100 |
+
key_concepts=file_summary.get("key_concepts", []),
|
| 101 |
+
dependencies=file_summary.get("dependencies", []),
|
| 102 |
+
summary=file_summary.get("summary", ""),
|
| 103 |
+
lines_of_code=file_summary.get("lines_of_code", 0),
|
| 104 |
+
repo_name=repo_name,
|
| 105 |
+
)
|
| 106 |
+
references.append(reference)
|
| 107 |
+
|
| 108 |
+
return references
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def extract_relationships(index_data: Dict) -> List[RelationshipInfo]:
|
| 112 |
+
"""Extract relationship information from index data"""
|
| 113 |
+
relationships = []
|
| 114 |
+
|
| 115 |
+
relationship_list = index_data.get("relationships", [])
|
| 116 |
+
|
| 117 |
+
for rel in relationship_list:
|
| 118 |
+
relationship = RelationshipInfo(
|
| 119 |
+
repo_file_path=rel.get("repo_file_path", ""),
|
| 120 |
+
target_file_path=rel.get("target_file_path", ""),
|
| 121 |
+
relationship_type=rel.get("relationship_type", ""),
|
| 122 |
+
confidence_score=rel.get("confidence_score", 0.0),
|
| 123 |
+
helpful_aspects=rel.get("helpful_aspects", []),
|
| 124 |
+
potential_contributions=rel.get("potential_contributions", []),
|
| 125 |
+
usage_suggestions=rel.get("usage_suggestions", ""),
|
| 126 |
+
)
|
| 127 |
+
relationships.append(relationship)
|
| 128 |
+
|
| 129 |
+
return relationships
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def calculate_relevance_score(
|
| 133 |
+
target_file: str, reference: CodeReference, keywords: List[str] = None
|
| 134 |
+
) -> float:
|
| 135 |
+
"""Calculate relevance score between reference code and target file"""
|
| 136 |
+
score = 0.0
|
| 137 |
+
|
| 138 |
+
# File name similarity
|
| 139 |
+
target_name = Path(target_file).stem.lower()
|
| 140 |
+
ref_name = Path(reference.file_path).stem.lower()
|
| 141 |
+
|
| 142 |
+
if target_name in ref_name or ref_name in target_name:
|
| 143 |
+
score += 0.3
|
| 144 |
+
|
| 145 |
+
# File type matching
|
| 146 |
+
target_extension = Path(target_file).suffix
|
| 147 |
+
ref_extension = Path(reference.file_path).suffix
|
| 148 |
+
|
| 149 |
+
if target_extension == ref_extension:
|
| 150 |
+
score += 0.2
|
| 151 |
+
|
| 152 |
+
# Keyword matching
|
| 153 |
+
if keywords:
|
| 154 |
+
keyword_matches = 0
|
| 155 |
+
total_searchable_text = (
|
| 156 |
+
" ".join(reference.key_concepts)
|
| 157 |
+
+ " "
|
| 158 |
+
+ " ".join(reference.main_functions)
|
| 159 |
+
+ " "
|
| 160 |
+
+ reference.summary
|
| 161 |
+
+ " "
|
| 162 |
+
+ reference.file_type
|
| 163 |
+
).lower()
|
| 164 |
+
|
| 165 |
+
for keyword in keywords:
|
| 166 |
+
if keyword.lower() in total_searchable_text:
|
| 167 |
+
keyword_matches += 1
|
| 168 |
+
|
| 169 |
+
if keywords:
|
| 170 |
+
score += (keyword_matches / len(keywords)) * 0.5
|
| 171 |
+
|
| 172 |
+
return min(score, 1.0)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def find_relevant_references_in_cache(
|
| 176 |
+
target_file: str,
|
| 177 |
+
index_cache: Dict[str, Dict],
|
| 178 |
+
keywords: List[str] = None,
|
| 179 |
+
max_results: int = 10,
|
| 180 |
+
) -> List[Tuple[CodeReference, float]]:
|
| 181 |
+
"""Find reference code relevant to target file from provided cache"""
|
| 182 |
+
all_references = []
|
| 183 |
+
|
| 184 |
+
# Collect reference information from all index files
|
| 185 |
+
for repo_name, index_data in index_cache.items():
|
| 186 |
+
references = extract_code_references(index_data)
|
| 187 |
+
for ref in references:
|
| 188 |
+
relevance_score = calculate_relevance_score(target_file, ref, keywords)
|
| 189 |
+
if relevance_score > 0.1: # Only keep results with certain relevance
|
| 190 |
+
all_references.append((ref, relevance_score))
|
| 191 |
+
|
| 192 |
+
# Sort by relevance score
|
| 193 |
+
all_references.sort(key=lambda x: x[1], reverse=True)
|
| 194 |
+
|
| 195 |
+
return all_references[:max_results]
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
def find_direct_relationships_in_cache(
|
| 199 |
+
target_file: str, index_cache: Dict[str, Dict]
|
| 200 |
+
) -> List[RelationshipInfo]:
|
| 201 |
+
"""Find direct relationships with target file from provided cache"""
|
| 202 |
+
relationships = []
|
| 203 |
+
|
| 204 |
+
# Normalize target file path (remove common prefixes if exists)
|
| 205 |
+
common_prefixes = ["src/", "core/", "lib/", "main/", "./"]
|
| 206 |
+
normalized_target = target_file.strip("/")
|
| 207 |
+
for prefix in common_prefixes:
|
| 208 |
+
if normalized_target.startswith(prefix):
|
| 209 |
+
normalized_target = normalized_target[len(prefix) :]
|
| 210 |
+
break
|
| 211 |
+
|
| 212 |
+
# Collect relationship information from all index files
|
| 213 |
+
for repo_name, index_data in index_cache.items():
|
| 214 |
+
repo_relationships = extract_relationships(index_data)
|
| 215 |
+
for rel in repo_relationships:
|
| 216 |
+
# Normalize target file path in relationship
|
| 217 |
+
normalized_rel_target = rel.target_file_path.strip("/")
|
| 218 |
+
for prefix in common_prefixes:
|
| 219 |
+
if normalized_rel_target.startswith(prefix):
|
| 220 |
+
normalized_rel_target = normalized_rel_target[len(prefix) :]
|
| 221 |
+
break
|
| 222 |
+
|
| 223 |
+
# Check target file path matching (support multiple matching methods)
|
| 224 |
+
if (
|
| 225 |
+
normalized_target == normalized_rel_target
|
| 226 |
+
or normalized_target in normalized_rel_target
|
| 227 |
+
or normalized_rel_target in normalized_target
|
| 228 |
+
or target_file in rel.target_file_path
|
| 229 |
+
or rel.target_file_path in target_file
|
| 230 |
+
):
|
| 231 |
+
relationships.append(rel)
|
| 232 |
+
|
| 233 |
+
# Sort by confidence score
|
| 234 |
+
relationships.sort(key=lambda x: x.confidence_score, reverse=True)
|
| 235 |
+
|
| 236 |
+
return relationships
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def format_reference_output(
|
| 240 |
+
target_file: str,
|
| 241 |
+
relevant_refs: List[Tuple[CodeReference, float]],
|
| 242 |
+
relationships: List[RelationshipInfo],
|
| 243 |
+
) -> str:
|
| 244 |
+
"""Format reference information output"""
|
| 245 |
+
output_lines = []
|
| 246 |
+
|
| 247 |
+
output_lines.append(f"# Code Reference Information - {target_file}")
|
| 248 |
+
output_lines.append("=" * 80)
|
| 249 |
+
output_lines.append("")
|
| 250 |
+
|
| 251 |
+
# Direct relationship information
|
| 252 |
+
if relationships:
|
| 253 |
+
output_lines.append("## 🎯 Direct Relationships")
|
| 254 |
+
output_lines.append("")
|
| 255 |
+
|
| 256 |
+
for i, rel in enumerate(relationships[:5], 1):
|
| 257 |
+
output_lines.append(f"### {i}. {rel.repo_file_path}")
|
| 258 |
+
output_lines.append(f"**Relationship Type**: {rel.relationship_type}")
|
| 259 |
+
output_lines.append(f"**Confidence Score**: {rel.confidence_score:.2f}")
|
| 260 |
+
output_lines.append(
|
| 261 |
+
f"**Helpful Aspects**: {', '.join(rel.helpful_aspects)}"
|
| 262 |
+
)
|
| 263 |
+
output_lines.append(
|
| 264 |
+
f"**Potential Contributions**: {', '.join(rel.potential_contributions)}"
|
| 265 |
+
)
|
| 266 |
+
output_lines.append(f"**Usage Suggestions**: {rel.usage_suggestions}")
|
| 267 |
+
output_lines.append("")
|
| 268 |
+
|
| 269 |
+
# Relevant code references
|
| 270 |
+
if relevant_refs:
|
| 271 |
+
output_lines.append("## 📚 Relevant Code References")
|
| 272 |
+
output_lines.append("")
|
| 273 |
+
|
| 274 |
+
for i, (ref, score) in enumerate(relevant_refs[:8], 1):
|
| 275 |
+
output_lines.append(f"### {i}. {ref.file_path} (Relevance: {score:.2f})")
|
| 276 |
+
output_lines.append(f"**Repository**: {ref.repo_name}")
|
| 277 |
+
output_lines.append(f"**File Type**: {ref.file_type}")
|
| 278 |
+
output_lines.append(
|
| 279 |
+
f"**Main Functions**: {', '.join(ref.main_functions[:5])}"
|
| 280 |
+
)
|
| 281 |
+
output_lines.append(f"**Key Concepts**: {', '.join(ref.key_concepts[:8])}")
|
| 282 |
+
output_lines.append(f"**Dependencies**: {', '.join(ref.dependencies[:6])}")
|
| 283 |
+
output_lines.append(f"**Lines of Code**: {ref.lines_of_code}")
|
| 284 |
+
output_lines.append(f"**Summary**: {ref.summary[:300]}...")
|
| 285 |
+
output_lines.append("")
|
| 286 |
+
|
| 287 |
+
# Implementation suggestions
|
| 288 |
+
output_lines.append("## 💡 Implementation Suggestions")
|
| 289 |
+
output_lines.append("")
|
| 290 |
+
|
| 291 |
+
if relevant_refs:
|
| 292 |
+
# Collect all function names and concepts
|
| 293 |
+
all_functions = set()
|
| 294 |
+
all_concepts = set()
|
| 295 |
+
all_dependencies = set()
|
| 296 |
+
|
| 297 |
+
for ref, _ in relevant_refs[:5]:
|
| 298 |
+
all_functions.update(ref.main_functions)
|
| 299 |
+
all_concepts.update(ref.key_concepts)
|
| 300 |
+
all_dependencies.update(ref.dependencies)
|
| 301 |
+
|
| 302 |
+
output_lines.append("**Reference Function Name Patterns**:")
|
| 303 |
+
for func in sorted(list(all_functions))[:10]:
|
| 304 |
+
output_lines.append(f"- {func}")
|
| 305 |
+
output_lines.append("")
|
| 306 |
+
|
| 307 |
+
output_lines.append("**Important Concepts and Patterns**:")
|
| 308 |
+
for concept in sorted(list(all_concepts))[:15]:
|
| 309 |
+
output_lines.append(f"- {concept}")
|
| 310 |
+
output_lines.append("")
|
| 311 |
+
|
| 312 |
+
output_lines.append("**Potential Dependencies Needed**:")
|
| 313 |
+
for dep in sorted(list(all_dependencies))[:10]:
|
| 314 |
+
output_lines.append(f"- {dep}")
|
| 315 |
+
output_lines.append("")
|
| 316 |
+
|
| 317 |
+
output_lines.append("## 🚀 Next Actions")
|
| 318 |
+
output_lines.append(
|
| 319 |
+
"1. Analyze design patterns and architectural styles from the above reference code"
|
| 320 |
+
)
|
| 321 |
+
output_lines.append("2. Determine core functionalities and interfaces to implement")
|
| 322 |
+
output_lines.append("3. Choose appropriate dependency libraries and tools")
|
| 323 |
+
output_lines.append(
|
| 324 |
+
"4. Design implementation solution consistent with existing code style"
|
| 325 |
+
)
|
| 326 |
+
output_lines.append("5. Start writing specific code implementation")
|
| 327 |
+
|
| 328 |
+
return "\n".join(output_lines)
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
# ==================== MCP Tool Definitions ====================
|
| 332 |
+
|
| 333 |
+
|
| 334 |
+
@mcp.tool()
|
| 335 |
+
async def search_code_references(
|
| 336 |
+
indexes_path: str, target_file: str, keywords: str = "", max_results: int = 10
|
| 337 |
+
) -> str:
|
| 338 |
+
"""
|
| 339 |
+
**UNIFIED TOOL**: Search relevant reference code from index files for target file implementation.
|
| 340 |
+
This tool combines directory setup, index loading, and searching in a single call.
|
| 341 |
+
|
| 342 |
+
Args:
|
| 343 |
+
indexes_path: Path to the indexes directory containing JSON index files
|
| 344 |
+
target_file: Target file path (file to be implemented)
|
| 345 |
+
keywords: Search keywords, comma-separated
|
| 346 |
+
max_results: Maximum number of results to return
|
| 347 |
+
|
| 348 |
+
Returns:
|
| 349 |
+
Formatted reference code information JSON string
|
| 350 |
+
"""
|
| 351 |
+
try:
|
| 352 |
+
# Step 1: Load index files from specified directory
|
| 353 |
+
logger.info(f"Loading index files from: {indexes_path}")
|
| 354 |
+
index_cache = load_index_files_from_directory(indexes_path)
|
| 355 |
+
|
| 356 |
+
if not index_cache:
|
| 357 |
+
result = {
|
| 358 |
+
"status": "error",
|
| 359 |
+
"message": f"No index files found or failed to load from: {indexes_path}",
|
| 360 |
+
"target_file": target_file,
|
| 361 |
+
"indexes_path": indexes_path,
|
| 362 |
+
}
|
| 363 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 364 |
+
|
| 365 |
+
# Step 2: Parse keywords
|
| 366 |
+
keyword_list = (
|
| 367 |
+
[kw.strip() for kw in keywords.split(",") if kw.strip()] if keywords else []
|
| 368 |
+
)
|
| 369 |
+
|
| 370 |
+
# Step 3: Find relevant reference code
|
| 371 |
+
relevant_refs = find_relevant_references_in_cache(
|
| 372 |
+
target_file, index_cache, keyword_list, max_results
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
# Step 4: Find direct relationships
|
| 376 |
+
relationships = find_direct_relationships_in_cache(target_file, index_cache)
|
| 377 |
+
|
| 378 |
+
# Step 5: Format output
|
| 379 |
+
formatted_output = format_reference_output(
|
| 380 |
+
target_file, relevant_refs, relationships
|
| 381 |
+
)
|
| 382 |
+
|
| 383 |
+
result = {
|
| 384 |
+
"status": "success",
|
| 385 |
+
"target_file": target_file,
|
| 386 |
+
"indexes_path": indexes_path,
|
| 387 |
+
"keywords_used": keyword_list,
|
| 388 |
+
"total_references_found": len(relevant_refs),
|
| 389 |
+
"total_relationships_found": len(relationships),
|
| 390 |
+
"formatted_content": formatted_output,
|
| 391 |
+
"indexes_loaded": list(index_cache.keys()),
|
| 392 |
+
"total_indexes_loaded": len(index_cache),
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
logger.info(
|
| 396 |
+
f"Successfully found {len(relevant_refs)} references and {len(relationships)} relationships for {target_file}"
|
| 397 |
+
)
|
| 398 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 399 |
+
|
| 400 |
+
except Exception as e:
|
| 401 |
+
logger.error(f"Error in search_code_references: {str(e)}")
|
| 402 |
+
result = {
|
| 403 |
+
"status": "error",
|
| 404 |
+
"message": f"Failed to search reference code: {str(e)}",
|
| 405 |
+
"target_file": target_file,
|
| 406 |
+
"indexes_path": indexes_path,
|
| 407 |
+
}
|
| 408 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 409 |
+
|
| 410 |
+
|
| 411 |
+
@mcp.tool()
|
| 412 |
+
async def get_indexes_overview(indexes_path: str) -> str:
|
| 413 |
+
"""
|
| 414 |
+
Get overview of all available reference code index information from specified directory
|
| 415 |
+
|
| 416 |
+
Args:
|
| 417 |
+
indexes_path: Path to the indexes directory containing JSON index files
|
| 418 |
+
|
| 419 |
+
Returns:
|
| 420 |
+
Overview information of all available reference code JSON string
|
| 421 |
+
"""
|
| 422 |
+
try:
|
| 423 |
+
# Load index files from specified directory
|
| 424 |
+
index_cache = load_index_files_from_directory(indexes_path)
|
| 425 |
+
|
| 426 |
+
if not index_cache:
|
| 427 |
+
result = {
|
| 428 |
+
"status": "error",
|
| 429 |
+
"message": f"No index files found in: {indexes_path}",
|
| 430 |
+
"indexes_path": indexes_path,
|
| 431 |
+
}
|
| 432 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 433 |
+
|
| 434 |
+
overview = {"total_repos": len(index_cache), "repositories": {}}
|
| 435 |
+
|
| 436 |
+
for repo_name, index_data in index_cache.items():
|
| 437 |
+
repo_info = {
|
| 438 |
+
"repo_name": index_data.get("repo_name", repo_name),
|
| 439 |
+
"total_files": index_data.get("total_files", 0),
|
| 440 |
+
"file_types": [],
|
| 441 |
+
"main_concepts": [],
|
| 442 |
+
"total_relationships": len(index_data.get("relationships", [])),
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
# Collect file types and concepts
|
| 446 |
+
file_summaries = index_data.get("file_summaries", [])
|
| 447 |
+
file_types = set()
|
| 448 |
+
concepts = set()
|
| 449 |
+
|
| 450 |
+
for file_summary in file_summaries:
|
| 451 |
+
file_types.add(file_summary.get("file_type", "Unknown"))
|
| 452 |
+
concepts.update(file_summary.get("key_concepts", []))
|
| 453 |
+
|
| 454 |
+
repo_info["file_types"] = sorted(list(file_types))
|
| 455 |
+
repo_info["main_concepts"] = sorted(list(concepts))[
|
| 456 |
+
:20
|
| 457 |
+
] # Limit concept count
|
| 458 |
+
|
| 459 |
+
overview["repositories"][repo_name] = repo_info
|
| 460 |
+
|
| 461 |
+
result = {
|
| 462 |
+
"status": "success",
|
| 463 |
+
"overview": overview,
|
| 464 |
+
"indexes_directory": str(Path(indexes_path).resolve()),
|
| 465 |
+
"total_indexes_loaded": len(index_cache),
|
| 466 |
+
}
|
| 467 |
+
|
| 468 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 469 |
+
|
| 470 |
+
except Exception as e:
|
| 471 |
+
result = {
|
| 472 |
+
"status": "error",
|
| 473 |
+
"message": f"Failed to get indexes overview: {str(e)}",
|
| 474 |
+
"indexes_path": indexes_path,
|
| 475 |
+
}
|
| 476 |
+
return json.dumps(result, ensure_ascii=False, indent=2)
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
def main():
|
| 480 |
+
"""Main function"""
|
| 481 |
+
logger.info("Starting unified Code Reference Indexer MCP server")
|
| 482 |
+
logger.info("Available tools:")
|
| 483 |
+
logger.info(
|
| 484 |
+
"1. search_code_references(indexes_path, target_file, keywords, max_results) - UNIFIED TOOL"
|
| 485 |
+
)
|
| 486 |
+
logger.info(
|
| 487 |
+
"2. get_indexes_overview(indexes_path) - Get overview of available indexes"
|
| 488 |
+
)
|
| 489 |
+
|
| 490 |
+
# Run MCP server
|
| 491 |
+
mcp.run()
|
| 492 |
+
|
| 493 |
+
|
| 494 |
+
if __name__ == "__main__":
|
| 495 |
+
main()
|
projects/ui/DeepCode/tools/command_executor.py
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Command Executor MCP Tool / 命令执行器 MCP 工具
|
| 4 |
+
|
| 5 |
+
专门负责执行LLM生成的shell命令来创建文件树结构
|
| 6 |
+
Specialized in executing LLM-generated shell commands to create file tree structures
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import subprocess
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from typing import List, Dict
|
| 12 |
+
from mcp.server.models import InitializationOptions
|
| 13 |
+
import mcp.types as types
|
| 14 |
+
from mcp.server import NotificationOptions, Server
|
| 15 |
+
import mcp.server.stdio
|
| 16 |
+
|
| 17 |
+
# 创建MCP服务器实例 / Create MCP server instance
|
| 18 |
+
app = Server("command-executor")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@app.list_tools()
|
| 22 |
+
async def handle_list_tools() -> list[types.Tool]:
|
| 23 |
+
"""
|
| 24 |
+
列出可用工具 / List available tools
|
| 25 |
+
"""
|
| 26 |
+
return [
|
| 27 |
+
types.Tool(
|
| 28 |
+
name="execute_commands",
|
| 29 |
+
description="""
|
| 30 |
+
执行shell命令列表来创建文件树结构
|
| 31 |
+
Execute shell command list to create file tree structure
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
commands: 要执行的shell命令列表(每行一个命令)
|
| 35 |
+
working_directory: 执行命令的工作目录
|
| 36 |
+
|
| 37 |
+
Returns:
|
| 38 |
+
命令执行结果和详细报告
|
| 39 |
+
""",
|
| 40 |
+
inputSchema={
|
| 41 |
+
"type": "object",
|
| 42 |
+
"properties": {
|
| 43 |
+
"commands": {
|
| 44 |
+
"type": "string",
|
| 45 |
+
"title": "Commands",
|
| 46 |
+
"description": "要执行的shell命令列表,每行一个命令",
|
| 47 |
+
},
|
| 48 |
+
"working_directory": {
|
| 49 |
+
"type": "string",
|
| 50 |
+
"title": "Working Directory",
|
| 51 |
+
"description": "执行命令的工作目录",
|
| 52 |
+
},
|
| 53 |
+
},
|
| 54 |
+
"required": ["commands", "working_directory"],
|
| 55 |
+
},
|
| 56 |
+
),
|
| 57 |
+
types.Tool(
|
| 58 |
+
name="execute_single_command",
|
| 59 |
+
description="""
|
| 60 |
+
执行单个shell命令
|
| 61 |
+
Execute single shell command
|
| 62 |
+
|
| 63 |
+
Args:
|
| 64 |
+
command: 要执行的单个命令
|
| 65 |
+
working_directory: 执行命令的工作目录
|
| 66 |
+
|
| 67 |
+
Returns:
|
| 68 |
+
命令执行结果
|
| 69 |
+
""",
|
| 70 |
+
inputSchema={
|
| 71 |
+
"type": "object",
|
| 72 |
+
"properties": {
|
| 73 |
+
"command": {
|
| 74 |
+
"type": "string",
|
| 75 |
+
"title": "Command",
|
| 76 |
+
"description": "要执行的单个shell命令",
|
| 77 |
+
},
|
| 78 |
+
"working_directory": {
|
| 79 |
+
"type": "string",
|
| 80 |
+
"title": "Working Directory",
|
| 81 |
+
"description": "执行命令的工作目录",
|
| 82 |
+
},
|
| 83 |
+
},
|
| 84 |
+
"required": ["command", "working_directory"],
|
| 85 |
+
},
|
| 86 |
+
),
|
| 87 |
+
]
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
@app.call_tool()
|
| 91 |
+
async def handle_call_tool(name: str, arguments: dict) -> list[types.TextContent]:
|
| 92 |
+
"""
|
| 93 |
+
处理工具调用 / Handle tool calls
|
| 94 |
+
"""
|
| 95 |
+
try:
|
| 96 |
+
if name == "execute_commands":
|
| 97 |
+
return await execute_command_batch(
|
| 98 |
+
arguments.get("commands", ""), arguments.get("working_directory", ".")
|
| 99 |
+
)
|
| 100 |
+
elif name == "execute_single_command":
|
| 101 |
+
return await execute_single_command(
|
| 102 |
+
arguments.get("command", ""), arguments.get("working_directory", ".")
|
| 103 |
+
)
|
| 104 |
+
else:
|
| 105 |
+
raise ValueError(f"未知工具 / Unknown tool: {name}")
|
| 106 |
+
|
| 107 |
+
except Exception as e:
|
| 108 |
+
return [
|
| 109 |
+
types.TextContent(
|
| 110 |
+
type="text",
|
| 111 |
+
text=f"工具执行错误 / Error executing tool {name}: {str(e)}",
|
| 112 |
+
)
|
| 113 |
+
]
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
async def execute_command_batch(
|
| 117 |
+
commands: str, working_directory: str
|
| 118 |
+
) -> list[types.TextContent]:
|
| 119 |
+
"""
|
| 120 |
+
执行多个shell命令 / Execute multiple shell commands
|
| 121 |
+
|
| 122 |
+
Args:
|
| 123 |
+
commands: 命令列表,每行一个命令 / Command list, one command per line
|
| 124 |
+
working_directory: 工作目录 / Working directory
|
| 125 |
+
|
| 126 |
+
Returns:
|
| 127 |
+
执行结果 / Execution results
|
| 128 |
+
"""
|
| 129 |
+
try:
|
| 130 |
+
# 确保工作目录存在 / Ensure working directory exists
|
| 131 |
+
Path(working_directory).mkdir(parents=True, exist_ok=True)
|
| 132 |
+
|
| 133 |
+
# 分割命令行 / Split command lines
|
| 134 |
+
command_lines = [
|
| 135 |
+
cmd.strip() for cmd in commands.strip().split("\n") if cmd.strip()
|
| 136 |
+
]
|
| 137 |
+
|
| 138 |
+
if not command_lines:
|
| 139 |
+
return [
|
| 140 |
+
types.TextContent(
|
| 141 |
+
type="text", text="没有提供有效命令 / No valid commands provided"
|
| 142 |
+
)
|
| 143 |
+
]
|
| 144 |
+
|
| 145 |
+
results = []
|
| 146 |
+
stats = {"successful": 0, "failed": 0, "timeout": 0}
|
| 147 |
+
|
| 148 |
+
for i, command in enumerate(command_lines, 1):
|
| 149 |
+
try:
|
| 150 |
+
# 执行命令 / Execute command
|
| 151 |
+
result = subprocess.run(
|
| 152 |
+
command,
|
| 153 |
+
shell=True,
|
| 154 |
+
cwd=working_directory,
|
| 155 |
+
capture_output=True,
|
| 156 |
+
text=True,
|
| 157 |
+
timeout=30, # 30秒超时
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
if result.returncode == 0:
|
| 161 |
+
results.append(f"✅ Command {i}: {command}")
|
| 162 |
+
if result.stdout.strip():
|
| 163 |
+
results.append(f" 输出 / Output: {result.stdout.strip()}")
|
| 164 |
+
stats["successful"] += 1
|
| 165 |
+
else:
|
| 166 |
+
results.append(f"❌ Command {i}: {command}")
|
| 167 |
+
if result.stderr.strip():
|
| 168 |
+
results.append(f" 错误 / Error: {result.stderr.strip()}")
|
| 169 |
+
stats["failed"] += 1
|
| 170 |
+
|
| 171 |
+
except subprocess.TimeoutExpired:
|
| 172 |
+
results.append(f"⏱️ Command {i} 超时 / timeout: {command}")
|
| 173 |
+
stats["timeout"] += 1
|
| 174 |
+
except Exception as e:
|
| 175 |
+
results.append(f"💥 Command {i} 异常 / exception: {command} - {str(e)}")
|
| 176 |
+
stats["failed"] += 1
|
| 177 |
+
|
| 178 |
+
# 生成执行报告 / Generate execution report
|
| 179 |
+
summary = generate_execution_summary(working_directory, command_lines, stats)
|
| 180 |
+
final_result = summary + "\n" + "\n".join(results)
|
| 181 |
+
|
| 182 |
+
return [types.TextContent(type="text", text=final_result)]
|
| 183 |
+
|
| 184 |
+
except Exception as e:
|
| 185 |
+
return [
|
| 186 |
+
types.TextContent(
|
| 187 |
+
type="text",
|
| 188 |
+
text=f"批量命令执行失败 / Failed to execute command batch: {str(e)}",
|
| 189 |
+
)
|
| 190 |
+
]
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
async def execute_single_command(
|
| 194 |
+
command: str, working_directory: str
|
| 195 |
+
) -> list[types.TextContent]:
|
| 196 |
+
"""
|
| 197 |
+
执行单个shell命令 / Execute single shell command
|
| 198 |
+
|
| 199 |
+
Args:
|
| 200 |
+
command: 要执行的命令 / Command to execute
|
| 201 |
+
working_directory: 工作目录 / Working directory
|
| 202 |
+
|
| 203 |
+
Returns:
|
| 204 |
+
执行结果 / Execution result
|
| 205 |
+
"""
|
| 206 |
+
try:
|
| 207 |
+
# 确保工作目录存在 / Ensure working directory exists
|
| 208 |
+
Path(working_directory).mkdir(parents=True, exist_ok=True)
|
| 209 |
+
|
| 210 |
+
# 执行命令 / Execute command
|
| 211 |
+
result = subprocess.run(
|
| 212 |
+
command,
|
| 213 |
+
shell=True,
|
| 214 |
+
cwd=working_directory,
|
| 215 |
+
capture_output=True,
|
| 216 |
+
text=True,
|
| 217 |
+
timeout=30,
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
# 格式化输出 / Format output
|
| 221 |
+
output = format_single_command_result(command, working_directory, result)
|
| 222 |
+
|
| 223 |
+
return [types.TextContent(type="text", text=output)]
|
| 224 |
+
|
| 225 |
+
except subprocess.TimeoutExpired:
|
| 226 |
+
return [
|
| 227 |
+
types.TextContent(
|
| 228 |
+
type="text", text=f"⏱️ 命令超时 / Command timeout: {command}"
|
| 229 |
+
)
|
| 230 |
+
]
|
| 231 |
+
except Exception as e:
|
| 232 |
+
return [
|
| 233 |
+
types.TextContent(
|
| 234 |
+
type="text", text=f"💥 命令执行错误 / Command execution error: {str(e)}"
|
| 235 |
+
)
|
| 236 |
+
]
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def generate_execution_summary(
|
| 240 |
+
working_directory: str, command_lines: List[str], stats: Dict[str, int]
|
| 241 |
+
) -> str:
|
| 242 |
+
"""
|
| 243 |
+
生成执行总结 / Generate execution summary
|
| 244 |
+
|
| 245 |
+
Args:
|
| 246 |
+
working_directory: 工作目录 / Working directory
|
| 247 |
+
command_lines: 命令列表 / Command list
|
| 248 |
+
stats: 统计信息 / Statistics
|
| 249 |
+
|
| 250 |
+
Returns:
|
| 251 |
+
格式化的总结 / Formatted summary
|
| 252 |
+
"""
|
| 253 |
+
return f"""
|
| 254 |
+
命令执行总结 / Command Execution Summary:
|
| 255 |
+
{'='*50}
|
| 256 |
+
工作目录 / Working Directory: {working_directory}
|
| 257 |
+
总命令数 / Total Commands: {len(command_lines)}
|
| 258 |
+
成功 / Successful: {stats['successful']}
|
| 259 |
+
失败 / Failed: {stats['failed']}
|
| 260 |
+
超时 / Timeout: {stats['timeout']}
|
| 261 |
+
|
| 262 |
+
详细结果 / Detailed Results:
|
| 263 |
+
{'-'*50}"""
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
def format_single_command_result(
|
| 267 |
+
command: str, working_directory: str, result: subprocess.CompletedProcess
|
| 268 |
+
) -> str:
|
| 269 |
+
"""
|
| 270 |
+
格式化单命令执行结果 / Format single command execution result
|
| 271 |
+
|
| 272 |
+
Args:
|
| 273 |
+
command: 执行的命令 / Executed command
|
| 274 |
+
working_directory: 工作目录 / Working directory
|
| 275 |
+
result: 执行结果 / Execution result
|
| 276 |
+
|
| 277 |
+
Returns:
|
| 278 |
+
格式化的结果 / Formatted result
|
| 279 |
+
"""
|
| 280 |
+
output = f"""
|
| 281 |
+
单命令执行 / Single Command Execution:
|
| 282 |
+
{'='*40}
|
| 283 |
+
工作目录 / Working Directory: {working_directory}
|
| 284 |
+
命令 / Command: {command}
|
| 285 |
+
返回码 / Return Code: {result.returncode}
|
| 286 |
+
|
| 287 |
+
"""
|
| 288 |
+
|
| 289 |
+
if result.returncode == 0:
|
| 290 |
+
output += "✅ 状态 / Status: SUCCESS / 成功\n"
|
| 291 |
+
if result.stdout.strip():
|
| 292 |
+
output += f"输出 / Output:\n{result.stdout.strip()}\n"
|
| 293 |
+
else:
|
| 294 |
+
output += "❌ 状态 / Status: FAILED / 失败\n"
|
| 295 |
+
if result.stderr.strip():
|
| 296 |
+
output += f"错误 / Error:\n{result.stderr.strip()}\n"
|
| 297 |
+
|
| 298 |
+
return output
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
async def main():
|
| 302 |
+
"""
|
| 303 |
+
运行MCP服务器 / Run MCP server
|
| 304 |
+
"""
|
| 305 |
+
# 通过stdio运行服务器 / Run server via stdio
|
| 306 |
+
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
|
| 307 |
+
await app.run(
|
| 308 |
+
read_stream,
|
| 309 |
+
write_stream,
|
| 310 |
+
InitializationOptions(
|
| 311 |
+
server_name="command-executor",
|
| 312 |
+
server_version="1.0.0",
|
| 313 |
+
capabilities=app.get_capabilities(
|
| 314 |
+
notification_options=NotificationOptions(),
|
| 315 |
+
experimental_capabilities={},
|
| 316 |
+
),
|
| 317 |
+
),
|
| 318 |
+
)
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
if __name__ == "__main__":
|
| 322 |
+
import asyncio
|
| 323 |
+
|
| 324 |
+
asyncio.run(main())
|
projects/ui/DeepCode/tools/document_segmentation_server.py
ADDED
|
@@ -0,0 +1,1937 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Document Segmentation MCP Server
|
| 4 |
+
|
| 5 |
+
This MCP server provides intelligent document segmentation and retrieval functions for handling
|
| 6 |
+
large research papers and technical documents that exceed LLM token limits.
|
| 7 |
+
|
| 8 |
+
==== CORE FUNCTIONALITY ====
|
| 9 |
+
1. Analyze document structure and type using semantic content analysis
|
| 10 |
+
2. Create intelligent segments based on content semantics, not just structure
|
| 11 |
+
3. Provide query-aware segment retrieval with relevance scoring
|
| 12 |
+
4. Support both structured (papers with headers) and unstructured documents
|
| 13 |
+
5. Configurable segmentation strategies based on document complexity
|
| 14 |
+
|
| 15 |
+
==== MCP TOOLS PROVIDED ====
|
| 16 |
+
|
| 17 |
+
📄 analyze_and_segment_document(paper_dir: str, force_refresh: bool = False)
|
| 18 |
+
Purpose: Analyzes document structure and creates intelligent segments
|
| 19 |
+
- Detects document type (research paper, technical doc, algorithm-focused, etc.)
|
| 20 |
+
- Selects optimal segmentation strategy based on content analysis
|
| 21 |
+
- Creates semantic segments preserving algorithm and concept integrity
|
| 22 |
+
- Stores segmentation index for efficient retrieval
|
| 23 |
+
- Returns: JSON with segmentation status, strategy used, and segment count
|
| 24 |
+
|
| 25 |
+
📖 read_document_segments(paper_dir: str, query_type: str, keywords: List[str] = None,
|
| 26 |
+
max_segments: int = 3, max_total_chars: int = None)
|
| 27 |
+
Purpose: Intelligently retrieves relevant document segments based on query context
|
| 28 |
+
- query_type: "concept_analysis", "algorithm_extraction", or "code_planning"
|
| 29 |
+
- Uses semantic relevance scoring to rank segments
|
| 30 |
+
- Applies query-specific filtering and keyword matching
|
| 31 |
+
- Dynamically calculates optimal character limits based on content complexity
|
| 32 |
+
- Returns: JSON with selected segments optimized for the specific query type
|
| 33 |
+
|
| 34 |
+
📋 get_document_overview(paper_dir: str)
|
| 35 |
+
Purpose: Provides high-level overview of document structure and available segments
|
| 36 |
+
- Shows document type and segmentation strategy used
|
| 37 |
+
- Lists all segments with titles, content types, and relevance scores
|
| 38 |
+
- Displays segment statistics (character counts, keyword summaries)
|
| 39 |
+
- Returns: JSON with complete document analysis metadata
|
| 40 |
+
|
| 41 |
+
==== SEGMENTATION STRATEGIES ====
|
| 42 |
+
- semantic_research_focused: For academic papers with complex algorithmic content
|
| 43 |
+
- algorithm_preserve_integrity: Maintains algorithm blocks and formula chains intact
|
| 44 |
+
- concept_implementation_hybrid: Merges related concepts with implementation details
|
| 45 |
+
- semantic_chunking_enhanced: Advanced boundary detection for long documents
|
| 46 |
+
- content_aware_segmentation: Adaptive chunking based on content density
|
| 47 |
+
|
| 48 |
+
==== INTELLIGENT FEATURES ====
|
| 49 |
+
- Semantic boundary detection (not just structural)
|
| 50 |
+
- Algorithm block identification and preservation
|
| 51 |
+
- Formula chain recognition and grouping
|
| 52 |
+
- Concept-implementation relationship mapping
|
| 53 |
+
- Multi-level relevance scoring (content type, importance, keyword matching)
|
| 54 |
+
- Backward compatibility with existing document indexes
|
| 55 |
+
- Configurable via mcp_agent.config.yaml (enabled/disabled, size thresholds)
|
| 56 |
+
|
| 57 |
+
Usage:
|
| 58 |
+
python tools/document_segmentation_server.py
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
import os
|
| 62 |
+
import re
|
| 63 |
+
import json
|
| 64 |
+
import sys
|
| 65 |
+
import io
|
| 66 |
+
from typing import Dict, List, Tuple
|
| 67 |
+
import hashlib
|
| 68 |
+
import logging
|
| 69 |
+
from datetime import datetime
|
| 70 |
+
from dataclasses import dataclass, asdict
|
| 71 |
+
|
| 72 |
+
# Set standard output encoding to UTF-8
|
| 73 |
+
if sys.stdout.encoding != "utf-8":
|
| 74 |
+
try:
|
| 75 |
+
if hasattr(sys.stdout, "reconfigure"):
|
| 76 |
+
sys.stdout.reconfigure(encoding="utf-8")
|
| 77 |
+
sys.stderr.reconfigure(encoding="utf-8")
|
| 78 |
+
else:
|
| 79 |
+
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding="utf-8")
|
| 80 |
+
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding="utf-8")
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f"Warning: Could not set UTF-8 encoding: {e}")
|
| 83 |
+
|
| 84 |
+
# Import MCP related modules
|
| 85 |
+
from mcp.server.fastmcp import FastMCP
|
| 86 |
+
|
| 87 |
+
# Setup logging
|
| 88 |
+
logging.basicConfig(level=logging.INFO)
|
| 89 |
+
logger = logging.getLogger(__name__)
|
| 90 |
+
|
| 91 |
+
# Create FastMCP server instance
|
| 92 |
+
mcp = FastMCP("document-segmentation-server")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
@dataclass
|
| 96 |
+
class DocumentSegment:
|
| 97 |
+
"""Represents a document segment with metadata"""
|
| 98 |
+
|
| 99 |
+
id: str
|
| 100 |
+
title: str
|
| 101 |
+
content: str
|
| 102 |
+
content_type: str # "introduction", "methodology", "algorithm", "results", etc.
|
| 103 |
+
keywords: List[str]
|
| 104 |
+
char_start: int
|
| 105 |
+
char_end: int
|
| 106 |
+
char_count: int
|
| 107 |
+
relevance_scores: Dict[str, float] # Scores for different query types
|
| 108 |
+
section_path: str # e.g., "3.2.1" for nested sections
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
@dataclass
|
| 112 |
+
class DocumentIndex:
|
| 113 |
+
"""Document index containing all segments and metadata"""
|
| 114 |
+
|
| 115 |
+
document_path: str
|
| 116 |
+
document_type: str # "academic_paper", "technical_doc", "code_doc", "general"
|
| 117 |
+
segmentation_strategy: str
|
| 118 |
+
total_segments: int
|
| 119 |
+
total_chars: int
|
| 120 |
+
segments: List[DocumentSegment]
|
| 121 |
+
created_at: str
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
class DocumentAnalyzer:
|
| 125 |
+
"""Enhanced document analyzer using semantic content analysis instead of mechanical structure detection"""
|
| 126 |
+
|
| 127 |
+
# More precise semantic indicators, weighted by importance
|
| 128 |
+
ALGORITHM_INDICATORS = {
|
| 129 |
+
"high": [
|
| 130 |
+
"algorithm",
|
| 131 |
+
"procedure",
|
| 132 |
+
"method",
|
| 133 |
+
"approach",
|
| 134 |
+
"technique",
|
| 135 |
+
"framework",
|
| 136 |
+
],
|
| 137 |
+
"medium": ["step", "process", "implementation", "computation", "calculation"],
|
| 138 |
+
"low": ["example", "illustration", "demonstration"],
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
TECHNICAL_CONCEPT_INDICATORS = {
|
| 142 |
+
"high": ["formula", "equation", "theorem", "lemma", "proof", "definition"],
|
| 143 |
+
"medium": ["parameter", "variable", "function", "model", "architecture"],
|
| 144 |
+
"low": ["notation", "symbol", "term"],
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
IMPLEMENTATION_INDICATORS = {
|
| 148 |
+
"high": ["code", "implementation", "programming", "software", "system"],
|
| 149 |
+
"medium": ["design", "structure", "module", "component", "interface"],
|
| 150 |
+
"low": ["tool", "library", "package"],
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
# Semantic features of document types (not just based on titles)
|
| 154 |
+
RESEARCH_PAPER_PATTERNS = [
|
| 155 |
+
r"(?i)\babstract\b.*?\n.*?(introduction|motivation|background)",
|
| 156 |
+
r"(?i)(methodology|method).*?(experiment|evaluation|result)",
|
| 157 |
+
r"(?i)(conclusion|future work|limitation).*?(reference|bibliography)",
|
| 158 |
+
r"(?i)(related work|literature review|prior art)",
|
| 159 |
+
]
|
| 160 |
+
|
| 161 |
+
TECHNICAL_DOC_PATTERNS = [
|
| 162 |
+
r"(?i)(getting started|installation|setup).*?(usage|example)",
|
| 163 |
+
r"(?i)(api|interface|specification).*?(parameter|endpoint)",
|
| 164 |
+
r"(?i)(tutorial|guide|walkthrough).*?(step|instruction)",
|
| 165 |
+
r"(?i)(troubleshooting|faq|common issues)",
|
| 166 |
+
]
|
| 167 |
+
|
| 168 |
+
def analyze_document_type(self, content: str) -> Tuple[str, float]:
|
| 169 |
+
"""
|
| 170 |
+
Enhanced document type analysis based on semantic content patterns
|
| 171 |
+
|
| 172 |
+
Returns:
|
| 173 |
+
Tuple[str, float]: (document_type, confidence_score)
|
| 174 |
+
"""
|
| 175 |
+
content_lower = content.lower()
|
| 176 |
+
|
| 177 |
+
# Calculate weighted semantic indicator scores
|
| 178 |
+
algorithm_score = self._calculate_weighted_score(
|
| 179 |
+
content_lower, self.ALGORITHM_INDICATORS
|
| 180 |
+
)
|
| 181 |
+
concept_score = self._calculate_weighted_score(
|
| 182 |
+
content_lower, self.TECHNICAL_CONCEPT_INDICATORS
|
| 183 |
+
)
|
| 184 |
+
implementation_score = self._calculate_weighted_score(
|
| 185 |
+
content_lower, self.IMPLEMENTATION_INDICATORS
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
# Detect semantic patterns of document types
|
| 189 |
+
research_pattern_score = self._detect_pattern_score(
|
| 190 |
+
content, self.RESEARCH_PAPER_PATTERNS
|
| 191 |
+
)
|
| 192 |
+
technical_pattern_score = self._detect_pattern_score(
|
| 193 |
+
content, self.TECHNICAL_DOC_PATTERNS
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
# Comprehensive evaluation of document type
|
| 197 |
+
total_research_score = (
|
| 198 |
+
algorithm_score + concept_score + research_pattern_score * 2
|
| 199 |
+
)
|
| 200 |
+
total_technical_score = implementation_score + technical_pattern_score * 2
|
| 201 |
+
|
| 202 |
+
# Determine document type based on content density and pattern matching
|
| 203 |
+
if research_pattern_score > 0.5 and total_research_score > 3.0:
|
| 204 |
+
return "research_paper", min(0.95, 0.6 + research_pattern_score * 0.35)
|
| 205 |
+
elif algorithm_score > 2.0 and concept_score > 1.5:
|
| 206 |
+
return "algorithm_focused", 0.85
|
| 207 |
+
elif total_technical_score > 2.5:
|
| 208 |
+
return "technical_doc", 0.8
|
| 209 |
+
elif implementation_score > 1.5:
|
| 210 |
+
return "implementation_guide", 0.75
|
| 211 |
+
else:
|
| 212 |
+
return "general_document", 0.5
|
| 213 |
+
|
| 214 |
+
def _calculate_weighted_score(
|
| 215 |
+
self, content: str, indicators: Dict[str, List[str]]
|
| 216 |
+
) -> float:
|
| 217 |
+
"""Calculate weighted semantic indicator scores"""
|
| 218 |
+
score = 0.0
|
| 219 |
+
for weight_level, terms in indicators.items():
|
| 220 |
+
weight = {"high": 3.0, "medium": 2.0, "low": 1.0}[weight_level]
|
| 221 |
+
for term in terms:
|
| 222 |
+
if term in content:
|
| 223 |
+
score += weight * (
|
| 224 |
+
content.count(term) * 0.5 + 1
|
| 225 |
+
) # Consider term frequency
|
| 226 |
+
return score
|
| 227 |
+
|
| 228 |
+
def _detect_pattern_score(self, content: str, patterns: List[str]) -> float:
|
| 229 |
+
"""Detect semantic pattern matching scores"""
|
| 230 |
+
matches = 0
|
| 231 |
+
for pattern in patterns:
|
| 232 |
+
if re.search(pattern, content, re.DOTALL):
|
| 233 |
+
matches += 1
|
| 234 |
+
return matches / len(patterns)
|
| 235 |
+
|
| 236 |
+
def detect_segmentation_strategy(self, content: str, doc_type: str) -> str:
|
| 237 |
+
"""
|
| 238 |
+
Intelligently determine the best segmentation strategy based on content semantics rather than mechanical structure
|
| 239 |
+
"""
|
| 240 |
+
# Analyze content characteristics
|
| 241 |
+
algorithm_density = self._calculate_algorithm_density(content)
|
| 242 |
+
concept_complexity = self._calculate_concept_complexity(content)
|
| 243 |
+
implementation_detail_level = self._calculate_implementation_detail_level(
|
| 244 |
+
content
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
# Select strategy based on document type and content characteristics
|
| 248 |
+
if doc_type == "research_paper" and algorithm_density > 0.3:
|
| 249 |
+
return "semantic_research_focused"
|
| 250 |
+
elif doc_type == "algorithm_focused" or algorithm_density > 0.5:
|
| 251 |
+
return "algorithm_preserve_integrity"
|
| 252 |
+
elif concept_complexity > 0.4 and implementation_detail_level > 0.3:
|
| 253 |
+
return "concept_implementation_hybrid"
|
| 254 |
+
elif len(content) > 15000: # Long documents
|
| 255 |
+
return "semantic_chunking_enhanced"
|
| 256 |
+
else:
|
| 257 |
+
return "content_aware_segmentation"
|
| 258 |
+
|
| 259 |
+
def _calculate_algorithm_density(self, content: str) -> float:
|
| 260 |
+
"""Calculate algorithm content density"""
|
| 261 |
+
total_chars = len(content)
|
| 262 |
+
algorithm_chars = 0
|
| 263 |
+
|
| 264 |
+
# Identify algorithm blocks
|
| 265 |
+
algorithm_patterns = [
|
| 266 |
+
r"(?i)(algorithm\s+\d+|procedure\s+\d+)",
|
| 267 |
+
r"(?i)(step\s+\d+|phase\s+\d+)",
|
| 268 |
+
r"(?i)(input:|output:|return:|initialize:)",
|
| 269 |
+
r"(?i)(for\s+each|while|if.*then|else)",
|
| 270 |
+
r"(?i)(function|method|procedure).*\(",
|
| 271 |
+
]
|
| 272 |
+
|
| 273 |
+
for pattern in algorithm_patterns:
|
| 274 |
+
matches = re.finditer(pattern, content)
|
| 275 |
+
for match in matches:
|
| 276 |
+
# Estimate algorithm block size (expand forward and backward from match point)
|
| 277 |
+
start = max(0, match.start() - 200)
|
| 278 |
+
end = min(len(content), match.end() + 800)
|
| 279 |
+
algorithm_chars += end - start
|
| 280 |
+
|
| 281 |
+
return min(1.0, algorithm_chars / total_chars)
|
| 282 |
+
|
| 283 |
+
def _calculate_concept_complexity(self, content: str) -> float:
|
| 284 |
+
"""Calculate concept complexity"""
|
| 285 |
+
concept_indicators = self.TECHNICAL_CONCEPT_INDICATORS
|
| 286 |
+
complexity_score = 0.0
|
| 287 |
+
|
| 288 |
+
for level, terms in concept_indicators.items():
|
| 289 |
+
weight = {"high": 3.0, "medium": 2.0, "low": 1.0}[level]
|
| 290 |
+
for term in terms:
|
| 291 |
+
complexity_score += content.lower().count(term) * weight
|
| 292 |
+
|
| 293 |
+
# Normalize to 0-1 range
|
| 294 |
+
return min(1.0, complexity_score / 100)
|
| 295 |
+
|
| 296 |
+
def _calculate_implementation_detail_level(self, content: str) -> float:
|
| 297 |
+
"""Calculate implementation detail level"""
|
| 298 |
+
implementation_patterns = [
|
| 299 |
+
r"(?i)(code|implementation|programming)",
|
| 300 |
+
r"(?i)(class|function|method|variable)",
|
| 301 |
+
r"(?i)(import|include|library)",
|
| 302 |
+
r"(?i)(parameter|argument|return)",
|
| 303 |
+
r"(?i)(example|demo|tutorial)",
|
| 304 |
+
]
|
| 305 |
+
|
| 306 |
+
detail_score = 0
|
| 307 |
+
for pattern in implementation_patterns:
|
| 308 |
+
detail_score += len(re.findall(pattern, content))
|
| 309 |
+
|
| 310 |
+
return min(1.0, detail_score / 50)
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
class DocumentSegmenter:
|
| 314 |
+
"""Creates intelligent segments from documents"""
|
| 315 |
+
|
| 316 |
+
def __init__(self):
|
| 317 |
+
self.analyzer = DocumentAnalyzer()
|
| 318 |
+
|
| 319 |
+
def segment_document(self, content: str, strategy: str) -> List[DocumentSegment]:
|
| 320 |
+
"""
|
| 321 |
+
Perform intelligent segmentation using the specified strategy
|
| 322 |
+
"""
|
| 323 |
+
if strategy == "semantic_research_focused":
|
| 324 |
+
return self._segment_research_paper_semantically(content)
|
| 325 |
+
elif strategy == "algorithm_preserve_integrity":
|
| 326 |
+
return self._segment_preserve_algorithm_integrity(content)
|
| 327 |
+
elif strategy == "concept_implementation_hybrid":
|
| 328 |
+
return self._segment_concept_implementation_hybrid(content)
|
| 329 |
+
elif strategy == "semantic_chunking_enhanced":
|
| 330 |
+
return self._segment_by_enhanced_semantic_chunks(content)
|
| 331 |
+
elif strategy == "content_aware_segmentation":
|
| 332 |
+
return self._segment_content_aware(content)
|
| 333 |
+
else:
|
| 334 |
+
# Compatibility with legacy strategies
|
| 335 |
+
return self._segment_by_enhanced_semantic_chunks(content)
|
| 336 |
+
|
| 337 |
+
def _segment_by_headers(self, content: str) -> List[DocumentSegment]:
|
| 338 |
+
"""Segment document based on markdown headers"""
|
| 339 |
+
segments = []
|
| 340 |
+
lines = content.split("\n")
|
| 341 |
+
current_segment = []
|
| 342 |
+
current_header = None
|
| 343 |
+
char_pos = 0
|
| 344 |
+
|
| 345 |
+
for line in lines:
|
| 346 |
+
line_with_newline = line + "\n"
|
| 347 |
+
|
| 348 |
+
# Check if line is a header
|
| 349 |
+
header_match = re.match(r"^(#{1,6})\s+(.+)$", line)
|
| 350 |
+
|
| 351 |
+
if header_match:
|
| 352 |
+
# Save previous segment if exists
|
| 353 |
+
if current_segment and current_header:
|
| 354 |
+
segment_content = "\n".join(current_segment).strip()
|
| 355 |
+
if segment_content:
|
| 356 |
+
# Analyze content type and importance
|
| 357 |
+
content_type = self._classify_content_type(
|
| 358 |
+
current_header, segment_content
|
| 359 |
+
)
|
| 360 |
+
importance_score = (
|
| 361 |
+
0.8 if content_type in ["algorithm", "formula"] else 0.7
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
segment = self._create_enhanced_segment(
|
| 365 |
+
segment_content,
|
| 366 |
+
current_header,
|
| 367 |
+
char_pos - len(segment_content.encode("utf-8")),
|
| 368 |
+
char_pos,
|
| 369 |
+
importance_score,
|
| 370 |
+
content_type,
|
| 371 |
+
)
|
| 372 |
+
segments.append(segment)
|
| 373 |
+
|
| 374 |
+
# Start new segment
|
| 375 |
+
current_header = header_match.group(2).strip()
|
| 376 |
+
current_segment = [line]
|
| 377 |
+
else:
|
| 378 |
+
if current_segment is not None:
|
| 379 |
+
current_segment.append(line)
|
| 380 |
+
|
| 381 |
+
char_pos += len(line_with_newline.encode("utf-8"))
|
| 382 |
+
|
| 383 |
+
# Add final segment
|
| 384 |
+
if current_segment and current_header:
|
| 385 |
+
segment_content = "\n".join(current_segment).strip()
|
| 386 |
+
if segment_content:
|
| 387 |
+
# Analyze content type and importance
|
| 388 |
+
content_type = self._classify_content_type(
|
| 389 |
+
current_header, segment_content
|
| 390 |
+
)
|
| 391 |
+
importance_score = (
|
| 392 |
+
0.8 if content_type in ["algorithm", "formula"] else 0.7
|
| 393 |
+
)
|
| 394 |
+
|
| 395 |
+
segment = self._create_enhanced_segment(
|
| 396 |
+
segment_content,
|
| 397 |
+
current_header,
|
| 398 |
+
char_pos - len(segment_content.encode("utf-8")),
|
| 399 |
+
char_pos,
|
| 400 |
+
importance_score,
|
| 401 |
+
content_type,
|
| 402 |
+
)
|
| 403 |
+
segments.append(segment)
|
| 404 |
+
|
| 405 |
+
return segments
|
| 406 |
+
|
| 407 |
+
def _segment_preserve_algorithm_integrity(
|
| 408 |
+
self, content: str
|
| 409 |
+
) -> List[DocumentSegment]:
|
| 410 |
+
"""Smart segmentation strategy that preserves algorithm integrity"""
|
| 411 |
+
segments = []
|
| 412 |
+
|
| 413 |
+
# 1. Identify algorithm blocks and related descriptions
|
| 414 |
+
algorithm_blocks = self._identify_algorithm_blocks(content)
|
| 415 |
+
|
| 416 |
+
# 2. Identify concept definition groups
|
| 417 |
+
concept_groups = self._identify_concept_groups(content)
|
| 418 |
+
|
| 419 |
+
# 3. Identify formula derivation chains
|
| 420 |
+
formula_chains = self._identify_formula_chains(content)
|
| 421 |
+
|
| 422 |
+
# 4. Merge related content blocks to ensure integrity
|
| 423 |
+
content_blocks = self._merge_related_content_blocks(
|
| 424 |
+
algorithm_blocks, concept_groups, formula_chains, content
|
| 425 |
+
)
|
| 426 |
+
|
| 427 |
+
# 5. Convert to DocumentSegment
|
| 428 |
+
for i, block in enumerate(content_blocks):
|
| 429 |
+
segment = self._create_enhanced_segment(
|
| 430 |
+
block["content"],
|
| 431 |
+
block["title"],
|
| 432 |
+
block["start_pos"],
|
| 433 |
+
block["end_pos"],
|
| 434 |
+
block["importance_score"],
|
| 435 |
+
block["content_type"],
|
| 436 |
+
)
|
| 437 |
+
segments.append(segment)
|
| 438 |
+
|
| 439 |
+
return segments
|
| 440 |
+
|
| 441 |
+
def _segment_research_paper_semantically(
|
| 442 |
+
self, content: str
|
| 443 |
+
) -> List[DocumentSegment]:
|
| 444 |
+
"""Semantic segmentation specifically for research papers"""
|
| 445 |
+
segments = []
|
| 446 |
+
|
| 447 |
+
# Identify semantic structure of research papers
|
| 448 |
+
paper_sections = self._identify_research_paper_sections(content)
|
| 449 |
+
|
| 450 |
+
for section in paper_sections:
|
| 451 |
+
# Ensure each section contains sufficient context
|
| 452 |
+
enhanced_content = self._enhance_section_with_context(section, content)
|
| 453 |
+
|
| 454 |
+
segment = self._create_enhanced_segment(
|
| 455 |
+
enhanced_content["content"],
|
| 456 |
+
enhanced_content["title"],
|
| 457 |
+
enhanced_content["start_pos"],
|
| 458 |
+
enhanced_content["end_pos"],
|
| 459 |
+
enhanced_content["importance_score"],
|
| 460 |
+
enhanced_content["content_type"],
|
| 461 |
+
)
|
| 462 |
+
segments.append(segment)
|
| 463 |
+
|
| 464 |
+
return segments
|
| 465 |
+
|
| 466 |
+
def _segment_concept_implementation_hybrid(
|
| 467 |
+
self, content: str
|
| 468 |
+
) -> List[DocumentSegment]:
|
| 469 |
+
"""Intelligent segmentation combining concepts and implementation"""
|
| 470 |
+
segments = []
|
| 471 |
+
|
| 472 |
+
# Identify concept-implementation correspondence
|
| 473 |
+
concept_impl_pairs = self._identify_concept_implementation_pairs(content)
|
| 474 |
+
|
| 475 |
+
for pair in concept_impl_pairs:
|
| 476 |
+
# Merge related concepts and implementations into one segment
|
| 477 |
+
merged_content = self._merge_concept_with_implementation(pair, content)
|
| 478 |
+
|
| 479 |
+
segment = self._create_enhanced_segment(
|
| 480 |
+
merged_content["content"],
|
| 481 |
+
merged_content["title"],
|
| 482 |
+
merged_content["start_pos"],
|
| 483 |
+
merged_content["end_pos"],
|
| 484 |
+
merged_content["importance_score"],
|
| 485 |
+
merged_content["content_type"],
|
| 486 |
+
)
|
| 487 |
+
segments.append(segment)
|
| 488 |
+
|
| 489 |
+
return segments
|
| 490 |
+
|
| 491 |
+
def _segment_by_enhanced_semantic_chunks(
|
| 492 |
+
self, content: str
|
| 493 |
+
) -> List[DocumentSegment]:
|
| 494 |
+
"""Enhanced semantic chunk segmentation"""
|
| 495 |
+
segments = []
|
| 496 |
+
|
| 497 |
+
# Use improved semantic boundary detection
|
| 498 |
+
semantic_boundaries = self._detect_semantic_boundaries(content)
|
| 499 |
+
|
| 500 |
+
current_start = 0
|
| 501 |
+
for i, boundary in enumerate(semantic_boundaries):
|
| 502 |
+
chunk_content = content[current_start : boundary["position"]]
|
| 503 |
+
|
| 504 |
+
if len(chunk_content.strip()) > 200: # Minimum content threshold
|
| 505 |
+
segment = self._create_enhanced_segment(
|
| 506 |
+
chunk_content,
|
| 507 |
+
boundary["suggested_title"],
|
| 508 |
+
current_start,
|
| 509 |
+
boundary["position"],
|
| 510 |
+
boundary["importance_score"],
|
| 511 |
+
boundary["content_type"],
|
| 512 |
+
)
|
| 513 |
+
segments.append(segment)
|
| 514 |
+
|
| 515 |
+
current_start = boundary["position"]
|
| 516 |
+
|
| 517 |
+
# Handle the final segment
|
| 518 |
+
if current_start < len(content):
|
| 519 |
+
final_content = content[current_start:]
|
| 520 |
+
if len(final_content.strip()) > 200:
|
| 521 |
+
segment = self._create_enhanced_segment(
|
| 522 |
+
final_content,
|
| 523 |
+
"Final Section",
|
| 524 |
+
current_start,
|
| 525 |
+
len(content),
|
| 526 |
+
0.7,
|
| 527 |
+
"general",
|
| 528 |
+
)
|
| 529 |
+
segments.append(segment)
|
| 530 |
+
|
| 531 |
+
return segments
|
| 532 |
+
|
| 533 |
+
def _segment_content_aware(self, content: str) -> List[DocumentSegment]:
|
| 534 |
+
"""Content-aware intelligent segmentation"""
|
| 535 |
+
segments = []
|
| 536 |
+
|
| 537 |
+
# Adaptive segmentation size
|
| 538 |
+
optimal_chunk_size = self._calculate_optimal_chunk_size(content)
|
| 539 |
+
|
| 540 |
+
# Segment based on content density
|
| 541 |
+
content_chunks = self._create_content_aware_chunks(content, optimal_chunk_size)
|
| 542 |
+
|
| 543 |
+
for chunk in content_chunks:
|
| 544 |
+
segment = self._create_enhanced_segment(
|
| 545 |
+
chunk["content"],
|
| 546 |
+
chunk["title"],
|
| 547 |
+
chunk["start_pos"],
|
| 548 |
+
chunk["end_pos"],
|
| 549 |
+
chunk["importance_score"],
|
| 550 |
+
chunk["content_type"],
|
| 551 |
+
)
|
| 552 |
+
segments.append(segment)
|
| 553 |
+
|
| 554 |
+
return segments
|
| 555 |
+
|
| 556 |
+
def _segment_academic_paper(self, content: str) -> List[DocumentSegment]:
|
| 557 |
+
"""Segment academic paper using semantic understanding"""
|
| 558 |
+
# First try header-based segmentation
|
| 559 |
+
headers = re.findall(r"^(#{1,6})\s+(.+)$", content, re.MULTILINE)
|
| 560 |
+
if len(headers) >= 2:
|
| 561 |
+
return self._segment_by_headers(content)
|
| 562 |
+
|
| 563 |
+
# Fallback to semantic detection of academic sections
|
| 564 |
+
sections = self._detect_academic_sections(content)
|
| 565 |
+
segments = []
|
| 566 |
+
|
| 567 |
+
for section in sections:
|
| 568 |
+
# Determine importance based on section type
|
| 569 |
+
section_type = section.get("type", "general")
|
| 570 |
+
content_type = (
|
| 571 |
+
section_type
|
| 572 |
+
if section_type
|
| 573 |
+
in ["algorithm", "formula", "introduction", "conclusion"]
|
| 574 |
+
else "general"
|
| 575 |
+
)
|
| 576 |
+
importance_score = {
|
| 577 |
+
"algorithm": 0.95,
|
| 578 |
+
"formula": 0.9,
|
| 579 |
+
"introduction": 0.85,
|
| 580 |
+
"conclusion": 0.8,
|
| 581 |
+
}.get(content_type, 0.7)
|
| 582 |
+
|
| 583 |
+
segment = self._create_enhanced_segment(
|
| 584 |
+
section["content"],
|
| 585 |
+
section["title"],
|
| 586 |
+
section["start_pos"],
|
| 587 |
+
section["end_pos"],
|
| 588 |
+
importance_score,
|
| 589 |
+
content_type,
|
| 590 |
+
)
|
| 591 |
+
segments.append(segment)
|
| 592 |
+
|
| 593 |
+
return segments
|
| 594 |
+
|
| 595 |
+
def _detect_academic_sections(self, content: str) -> List[Dict]:
|
| 596 |
+
"""Detect academic paper sections even without clear headers"""
|
| 597 |
+
sections = []
|
| 598 |
+
|
| 599 |
+
# Common academic section patterns
|
| 600 |
+
section_patterns = [
|
| 601 |
+
(r"(?i)(abstract|摘要)", "introduction"),
|
| 602 |
+
(r"(?i)(introduction|引言|简介)", "introduction"),
|
| 603 |
+
(r"(?i)(related work|相关工作|背景)", "background"),
|
| 604 |
+
(r"(?i)(method|methodology|approach|方法)", "methodology"),
|
| 605 |
+
(r"(?i)(algorithm|算法)", "algorithm"),
|
| 606 |
+
(r"(?i)(experiment|实验|evaluation|评估)", "experiment"),
|
| 607 |
+
(r"(?i)(result|结果|finding)", "results"),
|
| 608 |
+
(r"(?i)(conclusion|结论|总结)", "conclusion"),
|
| 609 |
+
(r"(?i)(reference|参考文献|bibliography)", "references"),
|
| 610 |
+
]
|
| 611 |
+
|
| 612 |
+
current_pos = 0
|
| 613 |
+
for i, (pattern, section_type) in enumerate(section_patterns):
|
| 614 |
+
match = re.search(pattern, content[current_pos:], re.IGNORECASE)
|
| 615 |
+
if match:
|
| 616 |
+
start_pos = current_pos + match.start()
|
| 617 |
+
|
| 618 |
+
# Find end position (next section or end of document)
|
| 619 |
+
next_pos = len(content)
|
| 620 |
+
for next_pattern, _ in section_patterns[i + 1 :]:
|
| 621 |
+
next_match = re.search(
|
| 622 |
+
next_pattern, content[start_pos + 100 :], re.IGNORECASE
|
| 623 |
+
)
|
| 624 |
+
if next_match:
|
| 625 |
+
next_pos = start_pos + 100 + next_match.start()
|
| 626 |
+
break
|
| 627 |
+
|
| 628 |
+
section_content = content[start_pos:next_pos].strip()
|
| 629 |
+
if len(section_content) > 50: # Minimum content length
|
| 630 |
+
# Calculate importance score and content type
|
| 631 |
+
importance_score = self._calculate_paragraph_importance(
|
| 632 |
+
section_content, section_type
|
| 633 |
+
)
|
| 634 |
+
content_type = self._classify_content_type(
|
| 635 |
+
match.group(1), section_content
|
| 636 |
+
)
|
| 637 |
+
|
| 638 |
+
sections.append(
|
| 639 |
+
{
|
| 640 |
+
"title": match.group(1),
|
| 641 |
+
"content": section_content,
|
| 642 |
+
"start_pos": start_pos,
|
| 643 |
+
"end_pos": next_pos,
|
| 644 |
+
"type": section_type,
|
| 645 |
+
"importance_score": importance_score,
|
| 646 |
+
"content_type": content_type,
|
| 647 |
+
}
|
| 648 |
+
)
|
| 649 |
+
|
| 650 |
+
current_pos = next_pos
|
| 651 |
+
|
| 652 |
+
return sections
|
| 653 |
+
|
| 654 |
+
def _segment_by_semantic_chunks(self, content: str) -> List[DocumentSegment]:
|
| 655 |
+
"""Segment long documents into semantic chunks"""
|
| 656 |
+
# Split into paragraphs first
|
| 657 |
+
paragraphs = [p.strip() for p in content.split("\n\n") if p.strip()]
|
| 658 |
+
|
| 659 |
+
segments = []
|
| 660 |
+
current_chunk = []
|
| 661 |
+
current_chunk_size = 0
|
| 662 |
+
chunk_size_limit = 3000 # characters
|
| 663 |
+
overlap_size = 200
|
| 664 |
+
|
| 665 |
+
char_pos = 0
|
| 666 |
+
|
| 667 |
+
for para in paragraphs:
|
| 668 |
+
para_size = len(para)
|
| 669 |
+
|
| 670 |
+
# If adding this paragraph exceeds limit, create a segment
|
| 671 |
+
if current_chunk_size + para_size > chunk_size_limit and current_chunk:
|
| 672 |
+
chunk_content = "\n\n".join(current_chunk)
|
| 673 |
+
# Analyze semantic chunk content type
|
| 674 |
+
content_type = self._classify_paragraph_type(chunk_content)
|
| 675 |
+
importance_score = self._calculate_paragraph_importance(
|
| 676 |
+
chunk_content, content_type
|
| 677 |
+
)
|
| 678 |
+
|
| 679 |
+
segment = self._create_enhanced_segment(
|
| 680 |
+
chunk_content,
|
| 681 |
+
f"Section {len(segments) + 1}",
|
| 682 |
+
char_pos - len(chunk_content.encode("utf-8")),
|
| 683 |
+
char_pos,
|
| 684 |
+
importance_score,
|
| 685 |
+
content_type,
|
| 686 |
+
)
|
| 687 |
+
segments.append(segment)
|
| 688 |
+
|
| 689 |
+
# Keep last part for overlap
|
| 690 |
+
overlap_content = (
|
| 691 |
+
chunk_content[-overlap_size:]
|
| 692 |
+
if len(chunk_content) > overlap_size
|
| 693 |
+
else ""
|
| 694 |
+
)
|
| 695 |
+
current_chunk = [overlap_content, para] if overlap_content else [para]
|
| 696 |
+
current_chunk_size = len(overlap_content) + para_size
|
| 697 |
+
else:
|
| 698 |
+
current_chunk.append(para)
|
| 699 |
+
current_chunk_size += para_size
|
| 700 |
+
|
| 701 |
+
char_pos += para_size + 2 # +2 for \n\n
|
| 702 |
+
|
| 703 |
+
# Add final chunk
|
| 704 |
+
if current_chunk:
|
| 705 |
+
chunk_content = "\n\n".join(current_chunk)
|
| 706 |
+
# Analyze final chunk content type
|
| 707 |
+
content_type = self._classify_paragraph_type(chunk_content)
|
| 708 |
+
importance_score = self._calculate_paragraph_importance(
|
| 709 |
+
chunk_content, content_type
|
| 710 |
+
)
|
| 711 |
+
|
| 712 |
+
segment = self._create_enhanced_segment(
|
| 713 |
+
chunk_content,
|
| 714 |
+
f"Section {len(segments) + 1}",
|
| 715 |
+
char_pos - len(chunk_content.encode("utf-8")),
|
| 716 |
+
char_pos,
|
| 717 |
+
importance_score,
|
| 718 |
+
content_type,
|
| 719 |
+
)
|
| 720 |
+
segments.append(segment)
|
| 721 |
+
|
| 722 |
+
return segments
|
| 723 |
+
|
| 724 |
+
def _segment_by_paragraphs(self, content: str) -> List[DocumentSegment]:
|
| 725 |
+
"""Simple paragraph-based segmentation for short documents"""
|
| 726 |
+
paragraphs = [p.strip() for p in content.split("\n\n") if p.strip()]
|
| 727 |
+
segments = []
|
| 728 |
+
char_pos = 0
|
| 729 |
+
|
| 730 |
+
for i, para in enumerate(paragraphs):
|
| 731 |
+
if len(para) > 100: # Only include substantial paragraphs
|
| 732 |
+
# Analyze paragraph type and importance
|
| 733 |
+
content_type = self._classify_paragraph_type(para)
|
| 734 |
+
importance_score = self._calculate_paragraph_importance(
|
| 735 |
+
para, content_type
|
| 736 |
+
)
|
| 737 |
+
|
| 738 |
+
segment = self._create_enhanced_segment(
|
| 739 |
+
para,
|
| 740 |
+
f"Paragraph {i + 1}",
|
| 741 |
+
char_pos,
|
| 742 |
+
char_pos + len(para.encode("utf-8")),
|
| 743 |
+
importance_score,
|
| 744 |
+
content_type,
|
| 745 |
+
)
|
| 746 |
+
segments.append(segment)
|
| 747 |
+
char_pos += len(para.encode("utf-8")) + 2
|
| 748 |
+
|
| 749 |
+
return segments
|
| 750 |
+
|
| 751 |
+
# =============== Enhanced intelligent segmentation helper methods ===============
|
| 752 |
+
|
| 753 |
+
def _identify_algorithm_blocks(self, content: str) -> List[Dict]:
|
| 754 |
+
"""Identify algorithm blocks and related descriptions"""
|
| 755 |
+
algorithm_blocks = []
|
| 756 |
+
|
| 757 |
+
# Algorithm block identification patterns
|
| 758 |
+
algorithm_patterns = [
|
| 759 |
+
r"(?i)(algorithm\s+\d+|procedure\s+\d+|method\s+\d+).*?(?=algorithm\s+\d+|procedure\s+\d+|method\s+\d+|$)",
|
| 760 |
+
r"(?i)(input:|output:|returns?:|require:|ensure:).*?(?=\n\s*\n|\n\s*(?:input:|output:|returns?:|require:|ensure:)|$)",
|
| 761 |
+
r"(?i)(for\s+each|while|if.*then|repeat.*until).*?(?=\n\s*\n|$)",
|
| 762 |
+
r"(?i)(step\s+\d+|phase\s+\d+).*?(?=step\s+\d+|phase\s+\d+|\n\s*\n|$)",
|
| 763 |
+
]
|
| 764 |
+
|
| 765 |
+
for pattern in algorithm_patterns:
|
| 766 |
+
matches = re.finditer(pattern, content, re.DOTALL)
|
| 767 |
+
for match in matches:
|
| 768 |
+
# Expand context to include complete descriptions
|
| 769 |
+
start = max(0, match.start() - 300)
|
| 770 |
+
end = min(len(content), match.end() + 500)
|
| 771 |
+
|
| 772 |
+
# Find natural boundaries
|
| 773 |
+
while start > 0 and content[start] not in "\n.!?":
|
| 774 |
+
start -= 1
|
| 775 |
+
while end < len(content) and content[end] not in "\n.!?":
|
| 776 |
+
end += 1
|
| 777 |
+
|
| 778 |
+
algorithm_blocks.append(
|
| 779 |
+
{
|
| 780 |
+
"start_pos": start,
|
| 781 |
+
"end_pos": end,
|
| 782 |
+
"content": content[start:end].strip(),
|
| 783 |
+
"title": self._extract_algorithm_title(
|
| 784 |
+
content[match.start() : match.end()]
|
| 785 |
+
),
|
| 786 |
+
"importance_score": 0.95, # High importance for algorithm blocks
|
| 787 |
+
"content_type": "algorithm",
|
| 788 |
+
}
|
| 789 |
+
)
|
| 790 |
+
|
| 791 |
+
return algorithm_blocks
|
| 792 |
+
|
| 793 |
+
def _identify_concept_groups(self, content: str) -> List[Dict]:
|
| 794 |
+
"""Identify concept definition groups"""
|
| 795 |
+
concept_groups = []
|
| 796 |
+
|
| 797 |
+
# Concept definition patterns
|
| 798 |
+
concept_patterns = [
|
| 799 |
+
r"(?i)(definition|define|let|denote|given).*?(?=\n\s*\n|definition|define|let|denote|$)",
|
| 800 |
+
r"(?i)(theorem|lemma|proposition|corollary).*?(?=\n\s*\n|theorem|lemma|proposition|corollary|$)",
|
| 801 |
+
r"(?i)(notation|symbol|parameter).*?(?=\n\s*\n|notation|symbol|parameter|$)",
|
| 802 |
+
]
|
| 803 |
+
|
| 804 |
+
for pattern in concept_patterns:
|
| 805 |
+
matches = re.finditer(pattern, content, re.DOTALL)
|
| 806 |
+
for match in matches:
|
| 807 |
+
# Expand context
|
| 808 |
+
start = max(0, match.start() - 200)
|
| 809 |
+
end = min(len(content), match.end() + 300)
|
| 810 |
+
|
| 811 |
+
concept_groups.append(
|
| 812 |
+
{
|
| 813 |
+
"start_pos": start,
|
| 814 |
+
"end_pos": end,
|
| 815 |
+
"content": content[start:end].strip(),
|
| 816 |
+
"title": self._extract_concept_title(
|
| 817 |
+
content[match.start() : match.end()]
|
| 818 |
+
),
|
| 819 |
+
"importance_score": 0.85,
|
| 820 |
+
"content_type": "concept",
|
| 821 |
+
}
|
| 822 |
+
)
|
| 823 |
+
|
| 824 |
+
return concept_groups
|
| 825 |
+
|
| 826 |
+
def _identify_formula_chains(self, content: str) -> List[Dict]:
|
| 827 |
+
"""Identify formula derivation chains"""
|
| 828 |
+
formula_chains = []
|
| 829 |
+
|
| 830 |
+
# Formula patterns
|
| 831 |
+
formula_patterns = [
|
| 832 |
+
r"\$\$.*?\$\$", # Block-level mathematical formulas
|
| 833 |
+
r"\$[^$]+\$", # Inline mathematical formulas
|
| 834 |
+
r"(?i)(equation|formula).*?(?=\n\s*\n|equation|formula|$)",
|
| 835 |
+
r"(?i)(where|such that|given that).*?(?=\n\s*\n|where|such that|given that|$)",
|
| 836 |
+
]
|
| 837 |
+
|
| 838 |
+
# Find dense formula regions
|
| 839 |
+
formula_positions = []
|
| 840 |
+
for pattern in formula_patterns:
|
| 841 |
+
matches = re.finditer(pattern, content, re.DOTALL)
|
| 842 |
+
for match in matches:
|
| 843 |
+
formula_positions.append((match.start(), match.end()))
|
| 844 |
+
|
| 845 |
+
# Merge nearby formulas into formula chains
|
| 846 |
+
formula_positions.sort()
|
| 847 |
+
if formula_positions:
|
| 848 |
+
current_chain_start = formula_positions[0][0]
|
| 849 |
+
current_chain_end = formula_positions[0][1]
|
| 850 |
+
|
| 851 |
+
for start, end in formula_positions[1:]:
|
| 852 |
+
if (
|
| 853 |
+
start - current_chain_end < 500
|
| 854 |
+
): # Merge formulas within 500 characters
|
| 855 |
+
current_chain_end = end
|
| 856 |
+
else:
|
| 857 |
+
# Save current chain
|
| 858 |
+
formula_chains.append(
|
| 859 |
+
{
|
| 860 |
+
"start_pos": max(0, current_chain_start - 200),
|
| 861 |
+
"end_pos": min(len(content), current_chain_end + 200),
|
| 862 |
+
"content": content[
|
| 863 |
+
max(0, current_chain_start - 200) : min(
|
| 864 |
+
len(content), current_chain_end + 200
|
| 865 |
+
)
|
| 866 |
+
].strip(),
|
| 867 |
+
"title": "Mathematical Formulation",
|
| 868 |
+
"importance_score": 0.9,
|
| 869 |
+
"content_type": "formula",
|
| 870 |
+
}
|
| 871 |
+
)
|
| 872 |
+
current_chain_start = start
|
| 873 |
+
current_chain_end = end
|
| 874 |
+
|
| 875 |
+
# Add the last chain
|
| 876 |
+
formula_chains.append(
|
| 877 |
+
{
|
| 878 |
+
"start_pos": max(0, current_chain_start - 200),
|
| 879 |
+
"end_pos": min(len(content), current_chain_end + 200),
|
| 880 |
+
"content": content[
|
| 881 |
+
max(0, current_chain_start - 200) : min(
|
| 882 |
+
len(content), current_chain_end + 200
|
| 883 |
+
)
|
| 884 |
+
].strip(),
|
| 885 |
+
"title": "Mathematical Formulation",
|
| 886 |
+
"importance_score": 0.9,
|
| 887 |
+
"content_type": "formula",
|
| 888 |
+
}
|
| 889 |
+
)
|
| 890 |
+
|
| 891 |
+
return formula_chains
|
| 892 |
+
|
| 893 |
+
def _merge_related_content_blocks(
|
| 894 |
+
self,
|
| 895 |
+
algorithm_blocks: List[Dict],
|
| 896 |
+
concept_groups: List[Dict],
|
| 897 |
+
formula_chains: List[Dict],
|
| 898 |
+
content: str,
|
| 899 |
+
) -> List[Dict]:
|
| 900 |
+
"""Merge related content blocks to ensure integrity"""
|
| 901 |
+
all_blocks = algorithm_blocks + concept_groups + formula_chains
|
| 902 |
+
all_blocks.sort(key=lambda x: x["start_pos"])
|
| 903 |
+
|
| 904 |
+
merged_blocks = []
|
| 905 |
+
i = 0
|
| 906 |
+
|
| 907 |
+
while i < len(all_blocks):
|
| 908 |
+
current_block = all_blocks[i]
|
| 909 |
+
|
| 910 |
+
# Check if can merge with the next block
|
| 911 |
+
while i + 1 < len(all_blocks):
|
| 912 |
+
next_block = all_blocks[i + 1]
|
| 913 |
+
|
| 914 |
+
# If blocks are close or content related, merge them
|
| 915 |
+
if next_block["start_pos"] - current_block[
|
| 916 |
+
"end_pos"
|
| 917 |
+
] < 300 or self._are_blocks_related(current_block, next_block):
|
| 918 |
+
# Merge blocks
|
| 919 |
+
merged_content = content[
|
| 920 |
+
current_block["start_pos"] : next_block["end_pos"]
|
| 921 |
+
]
|
| 922 |
+
current_block = {
|
| 923 |
+
"start_pos": current_block["start_pos"],
|
| 924 |
+
"end_pos": next_block["end_pos"],
|
| 925 |
+
"content": merged_content.strip(),
|
| 926 |
+
"title": f"{current_block['title']} & {next_block['title']}",
|
| 927 |
+
"importance_score": max(
|
| 928 |
+
current_block["importance_score"],
|
| 929 |
+
next_block["importance_score"],
|
| 930 |
+
),
|
| 931 |
+
"content_type": "merged",
|
| 932 |
+
}
|
| 933 |
+
i += 1
|
| 934 |
+
else:
|
| 935 |
+
break
|
| 936 |
+
|
| 937 |
+
merged_blocks.append(current_block)
|
| 938 |
+
i += 1
|
| 939 |
+
|
| 940 |
+
return merged_blocks
|
| 941 |
+
|
| 942 |
+
def _are_blocks_related(self, block1: Dict, block2: Dict) -> bool:
|
| 943 |
+
"""Determine if two content blocks are related"""
|
| 944 |
+
# Check content type associations
|
| 945 |
+
related_types = [
|
| 946 |
+
("algorithm", "formula"),
|
| 947 |
+
("concept", "algorithm"),
|
| 948 |
+
("formula", "concept"),
|
| 949 |
+
]
|
| 950 |
+
|
| 951 |
+
for type1, type2 in related_types:
|
| 952 |
+
if (
|
| 953 |
+
block1["content_type"] == type1 and block2["content_type"] == type2
|
| 954 |
+
) or (block1["content_type"] == type2 and block2["content_type"] == type1):
|
| 955 |
+
return True
|
| 956 |
+
|
| 957 |
+
return False
|
| 958 |
+
|
| 959 |
+
def _extract_algorithm_title(self, text: str) -> str:
|
| 960 |
+
"""Extract title from algorithm text"""
|
| 961 |
+
lines = text.split("\n")[:3] # First 3 lines
|
| 962 |
+
for line in lines:
|
| 963 |
+
line = line.strip()
|
| 964 |
+
if line and len(line) < 100: # Reasonable title length
|
| 965 |
+
# Clean title
|
| 966 |
+
title = re.sub(r"[^\w\s-]", "", line)
|
| 967 |
+
if title:
|
| 968 |
+
return title[:50] # Limit title length
|
| 969 |
+
return "Algorithm Block"
|
| 970 |
+
|
| 971 |
+
def _extract_concept_title(self, text: str) -> str:
|
| 972 |
+
"""Extract title from concept text"""
|
| 973 |
+
lines = text.split("\n")[:2]
|
| 974 |
+
for line in lines:
|
| 975 |
+
line = line.strip()
|
| 976 |
+
if line and len(line) < 80:
|
| 977 |
+
title = re.sub(r"[^\w\s-]", "", line)
|
| 978 |
+
if title:
|
| 979 |
+
return title[:50]
|
| 980 |
+
return "Concept Definition"
|
| 981 |
+
|
| 982 |
+
def _create_enhanced_segment(
|
| 983 |
+
self,
|
| 984 |
+
content: str,
|
| 985 |
+
title: str,
|
| 986 |
+
start_pos: int,
|
| 987 |
+
end_pos: int,
|
| 988 |
+
importance_score: float,
|
| 989 |
+
content_type: str,
|
| 990 |
+
) -> DocumentSegment:
|
| 991 |
+
"""Create enhanced document segment"""
|
| 992 |
+
# Generate unique ID
|
| 993 |
+
segment_id = hashlib.md5(
|
| 994 |
+
f"{title}_{start_pos}_{end_pos}_{importance_score}".encode()
|
| 995 |
+
).hexdigest()[:8]
|
| 996 |
+
|
| 997 |
+
# Extract keywords
|
| 998 |
+
keywords = self._extract_enhanced_keywords(content, content_type)
|
| 999 |
+
|
| 1000 |
+
# Calculate enhanced relevance scores
|
| 1001 |
+
relevance_scores = self._calculate_enhanced_relevance_scores(
|
| 1002 |
+
content, content_type, importance_score
|
| 1003 |
+
)
|
| 1004 |
+
|
| 1005 |
+
return DocumentSegment(
|
| 1006 |
+
id=segment_id,
|
| 1007 |
+
title=title,
|
| 1008 |
+
content=content,
|
| 1009 |
+
content_type=content_type,
|
| 1010 |
+
keywords=keywords,
|
| 1011 |
+
char_start=start_pos,
|
| 1012 |
+
char_end=end_pos,
|
| 1013 |
+
char_count=len(content),
|
| 1014 |
+
relevance_scores=relevance_scores,
|
| 1015 |
+
section_path=title,
|
| 1016 |
+
)
|
| 1017 |
+
|
| 1018 |
+
def _extract_enhanced_keywords(self, content: str, content_type: str) -> List[str]:
|
| 1019 |
+
"""Extract enhanced keywords based on content type"""
|
| 1020 |
+
words = re.findall(r"\b[a-zA-Z]{3,}\b", content.lower())
|
| 1021 |
+
|
| 1022 |
+
# Adjust stopwords based on content type
|
| 1023 |
+
if content_type == "algorithm":
|
| 1024 |
+
algorithm_stopwords = {
|
| 1025 |
+
"step",
|
| 1026 |
+
"then",
|
| 1027 |
+
"else",
|
| 1028 |
+
"end",
|
| 1029 |
+
"begin",
|
| 1030 |
+
"start",
|
| 1031 |
+
"stop",
|
| 1032 |
+
}
|
| 1033 |
+
words = [w for w in words if w not in algorithm_stopwords]
|
| 1034 |
+
elif content_type == "formula":
|
| 1035 |
+
formula_keywords = ["equation", "formula", "where", "given", "such", "that"]
|
| 1036 |
+
words.extend(formula_keywords)
|
| 1037 |
+
|
| 1038 |
+
# General stopwords
|
| 1039 |
+
general_stopwords = {
|
| 1040 |
+
"the",
|
| 1041 |
+
"and",
|
| 1042 |
+
"for",
|
| 1043 |
+
"are",
|
| 1044 |
+
"but",
|
| 1045 |
+
"not",
|
| 1046 |
+
"you",
|
| 1047 |
+
"all",
|
| 1048 |
+
"can",
|
| 1049 |
+
"her",
|
| 1050 |
+
"was",
|
| 1051 |
+
"one",
|
| 1052 |
+
"our",
|
| 1053 |
+
"had",
|
| 1054 |
+
"but",
|
| 1055 |
+
"have",
|
| 1056 |
+
"this",
|
| 1057 |
+
"that",
|
| 1058 |
+
"with",
|
| 1059 |
+
"from",
|
| 1060 |
+
"they",
|
| 1061 |
+
"she",
|
| 1062 |
+
"been",
|
| 1063 |
+
"were",
|
| 1064 |
+
"said",
|
| 1065 |
+
"each",
|
| 1066 |
+
"which",
|
| 1067 |
+
"their",
|
| 1068 |
+
}
|
| 1069 |
+
|
| 1070 |
+
keywords = [w for w in set(words) if w not in general_stopwords and len(w) > 3]
|
| 1071 |
+
return keywords[:25] # Increase keyword count
|
| 1072 |
+
|
| 1073 |
+
def _calculate_enhanced_relevance_scores(
|
| 1074 |
+
self, content: str, content_type: str, importance_score: float
|
| 1075 |
+
) -> Dict[str, float]:
|
| 1076 |
+
"""Calculate enhanced relevance scores"""
|
| 1077 |
+
content_lower = content.lower()
|
| 1078 |
+
|
| 1079 |
+
base_scores = {
|
| 1080 |
+
"concept_analysis": 0.5,
|
| 1081 |
+
"algorithm_extraction": 0.5,
|
| 1082 |
+
"code_planning": 0.5,
|
| 1083 |
+
}
|
| 1084 |
+
|
| 1085 |
+
# Adjust base scores based on content type and importance
|
| 1086 |
+
if content_type == "algorithm":
|
| 1087 |
+
base_scores["algorithm_extraction"] = importance_score
|
| 1088 |
+
base_scores["code_planning"] = importance_score * 0.9
|
| 1089 |
+
base_scores["concept_analysis"] = importance_score * 0.7
|
| 1090 |
+
elif content_type == "concept":
|
| 1091 |
+
base_scores["concept_analysis"] = importance_score
|
| 1092 |
+
base_scores["algorithm_extraction"] = importance_score * 0.8
|
| 1093 |
+
base_scores["code_planning"] = importance_score * 0.6
|
| 1094 |
+
elif content_type == "formula":
|
| 1095 |
+
base_scores["algorithm_extraction"] = importance_score
|
| 1096 |
+
base_scores["concept_analysis"] = importance_score * 0.8
|
| 1097 |
+
base_scores["code_planning"] = importance_score * 0.9
|
| 1098 |
+
elif content_type == "merged":
|
| 1099 |
+
# Merged content is usually important
|
| 1100 |
+
base_scores = {k: importance_score * 0.95 for k in base_scores}
|
| 1101 |
+
|
| 1102 |
+
# Additional bonus based on content density
|
| 1103 |
+
algorithm_indicators = ["algorithm", "method", "procedure", "step", "process"]
|
| 1104 |
+
concept_indicators = ["definition", "concept", "framework", "approach"]
|
| 1105 |
+
implementation_indicators = ["implementation", "code", "function", "design"]
|
| 1106 |
+
|
| 1107 |
+
for query_type, indicators in [
|
| 1108 |
+
("algorithm_extraction", algorithm_indicators),
|
| 1109 |
+
("concept_analysis", concept_indicators),
|
| 1110 |
+
("code_planning", implementation_indicators),
|
| 1111 |
+
]:
|
| 1112 |
+
density_bonus = (
|
| 1113 |
+
sum(1 for indicator in indicators if indicator in content_lower) * 0.1
|
| 1114 |
+
)
|
| 1115 |
+
base_scores[query_type] = min(1.0, base_scores[query_type] + density_bonus)
|
| 1116 |
+
|
| 1117 |
+
return base_scores
|
| 1118 |
+
|
| 1119 |
+
# Placeholder methods - can be further implemented later
|
| 1120 |
+
def _identify_research_paper_sections(self, content: str) -> List[Dict]:
|
| 1121 |
+
"""Identify research paper sections - simplified implementation"""
|
| 1122 |
+
# Temporarily use improved semantic detection
|
| 1123 |
+
return self._detect_academic_sections(content)
|
| 1124 |
+
|
| 1125 |
+
def _enhance_section_with_context(self, section: Dict, content: str) -> Dict:
|
| 1126 |
+
"""Add context to sections - simplified implementation"""
|
| 1127 |
+
return section
|
| 1128 |
+
|
| 1129 |
+
def _identify_concept_implementation_pairs(self, content: str) -> List[Dict]:
|
| 1130 |
+
"""Identify concept-implementation pairs - simplified implementation"""
|
| 1131 |
+
return []
|
| 1132 |
+
|
| 1133 |
+
def _merge_concept_with_implementation(self, pair: Dict, content: str) -> Dict:
|
| 1134 |
+
"""Merge concepts with implementation - simplified implementation"""
|
| 1135 |
+
return pair
|
| 1136 |
+
|
| 1137 |
+
def _detect_semantic_boundaries(self, content: str) -> List[Dict]:
|
| 1138 |
+
"""Detect semantic boundaries - based on paragraphs and logical separators"""
|
| 1139 |
+
boundaries = []
|
| 1140 |
+
|
| 1141 |
+
# Split paragraphs by double line breaks
|
| 1142 |
+
paragraphs = content.split("\n\n")
|
| 1143 |
+
current_pos = 0
|
| 1144 |
+
|
| 1145 |
+
for i, para in enumerate(paragraphs):
|
| 1146 |
+
if len(para.strip()) > 100: # Valid paragraph
|
| 1147 |
+
# Analyze paragraph type
|
| 1148 |
+
content_type = self._classify_paragraph_type(para)
|
| 1149 |
+
importance_score = self._calculate_paragraph_importance(
|
| 1150 |
+
para, content_type
|
| 1151 |
+
)
|
| 1152 |
+
|
| 1153 |
+
boundaries.append(
|
| 1154 |
+
{
|
| 1155 |
+
"position": current_pos + len(para),
|
| 1156 |
+
"suggested_title": self._extract_paragraph_title(para, i + 1),
|
| 1157 |
+
"importance_score": importance_score,
|
| 1158 |
+
"content_type": content_type,
|
| 1159 |
+
}
|
| 1160 |
+
)
|
| 1161 |
+
|
| 1162 |
+
current_pos += len(para) + 2 # +2 for \n\n
|
| 1163 |
+
|
| 1164 |
+
return boundaries
|
| 1165 |
+
|
| 1166 |
+
def _classify_paragraph_type(self, paragraph: str) -> str:
|
| 1167 |
+
"""Classify paragraph type"""
|
| 1168 |
+
para_lower = paragraph.lower()
|
| 1169 |
+
|
| 1170 |
+
if "algorithm" in para_lower or "procedure" in para_lower:
|
| 1171 |
+
return "algorithm"
|
| 1172 |
+
elif "formula" in para_lower or "$$" in paragraph:
|
| 1173 |
+
return "formula"
|
| 1174 |
+
elif any(
|
| 1175 |
+
word in para_lower for word in ["introduction", "overview", "abstract"]
|
| 1176 |
+
):
|
| 1177 |
+
return "introduction"
|
| 1178 |
+
elif any(word in para_lower for word in ["conclusion", "summary", "result"]):
|
| 1179 |
+
return "conclusion"
|
| 1180 |
+
else:
|
| 1181 |
+
return "general"
|
| 1182 |
+
|
| 1183 |
+
def _calculate_paragraph_importance(
|
| 1184 |
+
self, paragraph: str, content_type: str
|
| 1185 |
+
) -> float:
|
| 1186 |
+
"""Calculate paragraph importance"""
|
| 1187 |
+
if content_type == "algorithm":
|
| 1188 |
+
return 0.95
|
| 1189 |
+
elif content_type == "formula":
|
| 1190 |
+
return 0.9
|
| 1191 |
+
elif content_type == "introduction":
|
| 1192 |
+
return 0.85
|
| 1193 |
+
elif content_type == "conclusion":
|
| 1194 |
+
return 0.8
|
| 1195 |
+
else:
|
| 1196 |
+
return 0.7
|
| 1197 |
+
|
| 1198 |
+
def _extract_paragraph_title(self, paragraph: str, index: int) -> str:
|
| 1199 |
+
"""Extract paragraph title"""
|
| 1200 |
+
lines = paragraph.split("\n")
|
| 1201 |
+
for line in lines[:2]:
|
| 1202 |
+
if line.startswith("#"):
|
| 1203 |
+
return line.strip("# ")
|
| 1204 |
+
elif len(line) < 80 and line.strip():
|
| 1205 |
+
return line.strip()
|
| 1206 |
+
return f"Section {index}"
|
| 1207 |
+
|
| 1208 |
+
def _calculate_optimal_chunk_size(self, content: str) -> int:
|
| 1209 |
+
"""Calculate optimal chunk size"""
|
| 1210 |
+
# Dynamically adjust based on content complexity
|
| 1211 |
+
complexity = self.analyzer._calculate_concept_complexity(content)
|
| 1212 |
+
if complexity > 0.7:
|
| 1213 |
+
return 4000 # Complex content needs larger chunks
|
| 1214 |
+
elif complexity > 0.4:
|
| 1215 |
+
return 3000
|
| 1216 |
+
else:
|
| 1217 |
+
return 2000
|
| 1218 |
+
|
| 1219 |
+
def _create_content_aware_chunks(self, content: str, chunk_size: int) -> List[Dict]:
|
| 1220 |
+
"""Create content-aware chunks - simplified implementation"""
|
| 1221 |
+
chunks = []
|
| 1222 |
+
paragraphs = [p.strip() for p in content.split("\n\n") if p.strip()]
|
| 1223 |
+
|
| 1224 |
+
current_chunk = []
|
| 1225 |
+
current_size = 0
|
| 1226 |
+
start_pos = 0
|
| 1227 |
+
|
| 1228 |
+
for para in paragraphs:
|
| 1229 |
+
para_size = len(para)
|
| 1230 |
+
|
| 1231 |
+
if current_size + para_size > chunk_size and current_chunk:
|
| 1232 |
+
chunk_content = "\n\n".join(current_chunk)
|
| 1233 |
+
chunks.append(
|
| 1234 |
+
{
|
| 1235 |
+
"content": chunk_content,
|
| 1236 |
+
"title": f"Section {len(chunks) + 1}",
|
| 1237 |
+
"start_pos": start_pos,
|
| 1238 |
+
"end_pos": start_pos + len(chunk_content),
|
| 1239 |
+
"importance_score": 0.7,
|
| 1240 |
+
"content_type": "general",
|
| 1241 |
+
}
|
| 1242 |
+
)
|
| 1243 |
+
|
| 1244 |
+
current_chunk = [para]
|
| 1245 |
+
current_size = para_size
|
| 1246 |
+
start_pos += len(chunk_content) + 2
|
| 1247 |
+
else:
|
| 1248 |
+
current_chunk.append(para)
|
| 1249 |
+
current_size += para_size
|
| 1250 |
+
|
| 1251 |
+
# Add the last chunk
|
| 1252 |
+
if current_chunk:
|
| 1253 |
+
chunk_content = "\n\n".join(current_chunk)
|
| 1254 |
+
chunks.append(
|
| 1255 |
+
{
|
| 1256 |
+
"content": chunk_content,
|
| 1257 |
+
"title": f"Section {len(chunks) + 1}",
|
| 1258 |
+
"start_pos": start_pos,
|
| 1259 |
+
"end_pos": start_pos + len(chunk_content),
|
| 1260 |
+
"importance_score": 0.7,
|
| 1261 |
+
"content_type": "general",
|
| 1262 |
+
}
|
| 1263 |
+
)
|
| 1264 |
+
|
| 1265 |
+
return chunks
|
| 1266 |
+
|
| 1267 |
+
def _create_segment(
|
| 1268 |
+
self, content: str, title: str, start_pos: int, end_pos: int
|
| 1269 |
+
) -> DocumentSegment:
|
| 1270 |
+
"""Create a DocumentSegment with metadata"""
|
| 1271 |
+
# Generate unique ID
|
| 1272 |
+
segment_id = hashlib.md5(f"{title}_{start_pos}_{end_pos}".encode()).hexdigest()[
|
| 1273 |
+
:8
|
| 1274 |
+
]
|
| 1275 |
+
|
| 1276 |
+
# Extract keywords from content
|
| 1277 |
+
keywords = self._extract_keywords(content)
|
| 1278 |
+
|
| 1279 |
+
# Determine content type
|
| 1280 |
+
content_type = self._classify_content_type(title, content)
|
| 1281 |
+
|
| 1282 |
+
# Calculate relevance scores for different query types
|
| 1283 |
+
relevance_scores = self._calculate_relevance_scores(content, content_type)
|
| 1284 |
+
|
| 1285 |
+
return DocumentSegment(
|
| 1286 |
+
id=segment_id,
|
| 1287 |
+
title=title,
|
| 1288 |
+
content=content,
|
| 1289 |
+
content_type=content_type,
|
| 1290 |
+
keywords=keywords,
|
| 1291 |
+
char_start=start_pos,
|
| 1292 |
+
char_end=end_pos,
|
| 1293 |
+
char_count=len(content),
|
| 1294 |
+
relevance_scores=relevance_scores,
|
| 1295 |
+
section_path=title, # Simplified for now
|
| 1296 |
+
)
|
| 1297 |
+
|
| 1298 |
+
def _extract_keywords(self, content: str) -> List[str]:
|
| 1299 |
+
"""Extract relevant keywords from content"""
|
| 1300 |
+
# Simple keyword extraction - could be enhanced with NLP
|
| 1301 |
+
words = re.findall(r"\b[a-zA-Z]{3,}\b", content.lower())
|
| 1302 |
+
|
| 1303 |
+
# Remove common words
|
| 1304 |
+
stopwords = {
|
| 1305 |
+
"the",
|
| 1306 |
+
"and",
|
| 1307 |
+
"for",
|
| 1308 |
+
"are",
|
| 1309 |
+
"but",
|
| 1310 |
+
"not",
|
| 1311 |
+
"you",
|
| 1312 |
+
"all",
|
| 1313 |
+
"can",
|
| 1314 |
+
"her",
|
| 1315 |
+
"was",
|
| 1316 |
+
"one",
|
| 1317 |
+
"our",
|
| 1318 |
+
"had",
|
| 1319 |
+
"but",
|
| 1320 |
+
"have",
|
| 1321 |
+
"this",
|
| 1322 |
+
"that",
|
| 1323 |
+
"with",
|
| 1324 |
+
"from",
|
| 1325 |
+
"they",
|
| 1326 |
+
"she",
|
| 1327 |
+
"been",
|
| 1328 |
+
"were",
|
| 1329 |
+
"said",
|
| 1330 |
+
"each",
|
| 1331 |
+
"which",
|
| 1332 |
+
"their",
|
| 1333 |
+
}
|
| 1334 |
+
|
| 1335 |
+
keywords = [w for w in set(words) if w not in stopwords and len(w) > 3]
|
| 1336 |
+
return keywords[:20] # Top 20 keywords
|
| 1337 |
+
|
| 1338 |
+
def _classify_content_type(self, title: str, content: str) -> str:
|
| 1339 |
+
"""Classify the type of content based on title and content"""
|
| 1340 |
+
title_lower = title.lower()
|
| 1341 |
+
content_lower = content.lower()
|
| 1342 |
+
|
| 1343 |
+
if any(
|
| 1344 |
+
word in title_lower for word in ["introduction", "abstract", "overview"]
|
| 1345 |
+
):
|
| 1346 |
+
return "introduction"
|
| 1347 |
+
elif any(word in title_lower for word in ["method", "approach", "algorithm"]):
|
| 1348 |
+
return "methodology"
|
| 1349 |
+
elif any(
|
| 1350 |
+
word in title_lower for word in ["experiment", "evaluation", "result"]
|
| 1351 |
+
):
|
| 1352 |
+
return "experiment"
|
| 1353 |
+
elif any(
|
| 1354 |
+
word in title_lower for word in ["conclusion", "discussion", "summary"]
|
| 1355 |
+
):
|
| 1356 |
+
return "conclusion"
|
| 1357 |
+
elif any(word in title_lower for word in ["reference", "bibliography"]):
|
| 1358 |
+
return "references"
|
| 1359 |
+
elif "algorithm" in content_lower or "procedure" in content_lower:
|
| 1360 |
+
return "algorithm"
|
| 1361 |
+
else:
|
| 1362 |
+
return "general"
|
| 1363 |
+
|
| 1364 |
+
def _calculate_relevance_scores(
|
| 1365 |
+
self, content: str, content_type: str
|
| 1366 |
+
) -> Dict[str, float]:
|
| 1367 |
+
"""Calculate relevance scores for different query types"""
|
| 1368 |
+
content_lower = content.lower()
|
| 1369 |
+
|
| 1370 |
+
scores = {
|
| 1371 |
+
"concept_analysis": 0.5,
|
| 1372 |
+
"algorithm_extraction": 0.5,
|
| 1373 |
+
"code_planning": 0.5,
|
| 1374 |
+
}
|
| 1375 |
+
|
| 1376 |
+
# Concept analysis relevance
|
| 1377 |
+
concept_indicators = [
|
| 1378 |
+
"introduction",
|
| 1379 |
+
"overview",
|
| 1380 |
+
"architecture",
|
| 1381 |
+
"system",
|
| 1382 |
+
"framework",
|
| 1383 |
+
"concept",
|
| 1384 |
+
"approach",
|
| 1385 |
+
]
|
| 1386 |
+
concept_score = sum(
|
| 1387 |
+
1 for indicator in concept_indicators if indicator in content_lower
|
| 1388 |
+
) / len(concept_indicators)
|
| 1389 |
+
scores["concept_analysis"] = min(
|
| 1390 |
+
1.0, concept_score + (0.8 if content_type == "introduction" else 0)
|
| 1391 |
+
)
|
| 1392 |
+
|
| 1393 |
+
# Algorithm extraction relevance
|
| 1394 |
+
algorithm_indicators = [
|
| 1395 |
+
"algorithm",
|
| 1396 |
+
"method",
|
| 1397 |
+
"procedure",
|
| 1398 |
+
"formula",
|
| 1399 |
+
"equation",
|
| 1400 |
+
"step",
|
| 1401 |
+
"process",
|
| 1402 |
+
]
|
| 1403 |
+
algorithm_score = sum(
|
| 1404 |
+
1 for indicator in algorithm_indicators if indicator in content_lower
|
| 1405 |
+
) / len(algorithm_indicators)
|
| 1406 |
+
scores["algorithm_extraction"] = min(
|
| 1407 |
+
1.0, algorithm_score + (0.9 if content_type == "methodology" else 0)
|
| 1408 |
+
)
|
| 1409 |
+
|
| 1410 |
+
# Code planning relevance
|
| 1411 |
+
code_indicators = [
|
| 1412 |
+
"implementation",
|
| 1413 |
+
"code",
|
| 1414 |
+
"function",
|
| 1415 |
+
"class",
|
| 1416 |
+
"module",
|
| 1417 |
+
"structure",
|
| 1418 |
+
"design",
|
| 1419 |
+
]
|
| 1420 |
+
code_score = sum(
|
| 1421 |
+
1 for indicator in code_indicators if indicator in content_lower
|
| 1422 |
+
) / len(code_indicators)
|
| 1423 |
+
scores["code_planning"] = min(
|
| 1424 |
+
1.0,
|
| 1425 |
+
code_score + (0.7 if content_type in ["methodology", "algorithm"] else 0),
|
| 1426 |
+
)
|
| 1427 |
+
|
| 1428 |
+
return scores
|
| 1429 |
+
|
| 1430 |
+
|
| 1431 |
+
# Global variables
|
| 1432 |
+
DOCUMENT_INDEXES: Dict[str, DocumentIndex] = {}
|
| 1433 |
+
segmenter = DocumentSegmenter()
|
| 1434 |
+
|
| 1435 |
+
|
| 1436 |
+
def get_segments_dir(paper_dir: str) -> str:
|
| 1437 |
+
"""Get the segments directory path"""
|
| 1438 |
+
return os.path.join(paper_dir, "document_segments")
|
| 1439 |
+
|
| 1440 |
+
|
| 1441 |
+
def ensure_segments_dir_exists(segments_dir: str):
|
| 1442 |
+
"""Ensure segments directory exists"""
|
| 1443 |
+
os.makedirs(segments_dir, exist_ok=True)
|
| 1444 |
+
|
| 1445 |
+
|
| 1446 |
+
@mcp.tool()
|
| 1447 |
+
async def analyze_and_segment_document(
|
| 1448 |
+
paper_dir: str, force_refresh: bool = False
|
| 1449 |
+
) -> str:
|
| 1450 |
+
"""
|
| 1451 |
+
Analyze document structure and create intelligent segments
|
| 1452 |
+
|
| 1453 |
+
Args:
|
| 1454 |
+
paper_dir: Path to the paper directory
|
| 1455 |
+
force_refresh: Whether to force re-analysis even if segments exist
|
| 1456 |
+
|
| 1457 |
+
Returns:
|
| 1458 |
+
JSON string with segmentation results
|
| 1459 |
+
"""
|
| 1460 |
+
try:
|
| 1461 |
+
# Find markdown file in paper directory
|
| 1462 |
+
md_files = [f for f in os.listdir(paper_dir) if f.endswith(".md")]
|
| 1463 |
+
if not md_files:
|
| 1464 |
+
return json.dumps(
|
| 1465 |
+
{
|
| 1466 |
+
"status": "error",
|
| 1467 |
+
"message": f"No markdown file found in {paper_dir}",
|
| 1468 |
+
},
|
| 1469 |
+
ensure_ascii=False,
|
| 1470 |
+
indent=2,
|
| 1471 |
+
)
|
| 1472 |
+
|
| 1473 |
+
md_file_path = os.path.join(paper_dir, md_files[0])
|
| 1474 |
+
segments_dir = get_segments_dir(paper_dir)
|
| 1475 |
+
index_file_path = os.path.join(segments_dir, "document_index.json")
|
| 1476 |
+
|
| 1477 |
+
# Check if analysis already exists and is recent
|
| 1478 |
+
if not force_refresh and os.path.exists(index_file_path):
|
| 1479 |
+
try:
|
| 1480 |
+
with open(index_file_path, "r", encoding="utf-8") as f:
|
| 1481 |
+
existing_index = json.load(f)
|
| 1482 |
+
|
| 1483 |
+
# Compatibility handling: ensure segments data structure is correct
|
| 1484 |
+
if "segments" in existing_index:
|
| 1485 |
+
segments_data = []
|
| 1486 |
+
for seg_data in existing_index["segments"]:
|
| 1487 |
+
# Ensure all required fields exist
|
| 1488 |
+
segment_dict = dict(seg_data)
|
| 1489 |
+
|
| 1490 |
+
if "content_type" not in segment_dict:
|
| 1491 |
+
segment_dict["content_type"] = "general"
|
| 1492 |
+
if "keywords" not in segment_dict:
|
| 1493 |
+
segment_dict["keywords"] = []
|
| 1494 |
+
if "relevance_scores" not in segment_dict:
|
| 1495 |
+
segment_dict["relevance_scores"] = {
|
| 1496 |
+
"concept_analysis": 0.5,
|
| 1497 |
+
"algorithm_extraction": 0.5,
|
| 1498 |
+
"code_planning": 0.5,
|
| 1499 |
+
}
|
| 1500 |
+
if "section_path" not in segment_dict:
|
| 1501 |
+
segment_dict["section_path"] = segment_dict.get(
|
| 1502 |
+
"title", "Unknown"
|
| 1503 |
+
)
|
| 1504 |
+
|
| 1505 |
+
segments_data.append(DocumentSegment(**segment_dict))
|
| 1506 |
+
|
| 1507 |
+
existing_index["segments"] = segments_data
|
| 1508 |
+
|
| 1509 |
+
DOCUMENT_INDEXES[paper_dir] = DocumentIndex(**existing_index)
|
| 1510 |
+
return json.dumps(
|
| 1511 |
+
{
|
| 1512 |
+
"status": "success",
|
| 1513 |
+
"message": "Using existing document analysis",
|
| 1514 |
+
"segments_dir": segments_dir,
|
| 1515 |
+
"total_segments": existing_index["total_segments"],
|
| 1516 |
+
},
|
| 1517 |
+
ensure_ascii=False,
|
| 1518 |
+
indent=2,
|
| 1519 |
+
)
|
| 1520 |
+
|
| 1521 |
+
except Exception as e:
|
| 1522 |
+
logger.error(f"Failed to load existing index: {e}")
|
| 1523 |
+
logger.info("Will perform fresh analysis instead")
|
| 1524 |
+
# Remove corrupted index file and continue with new analysis
|
| 1525 |
+
try:
|
| 1526 |
+
os.remove(index_file_path)
|
| 1527 |
+
except Exception as e:
|
| 1528 |
+
pass
|
| 1529 |
+
|
| 1530 |
+
# Read document content
|
| 1531 |
+
with open(md_file_path, "r", encoding="utf-8") as f:
|
| 1532 |
+
content = f.read()
|
| 1533 |
+
|
| 1534 |
+
# Analyze document
|
| 1535 |
+
analyzer = DocumentAnalyzer()
|
| 1536 |
+
doc_type, confidence = analyzer.analyze_document_type(content)
|
| 1537 |
+
strategy = analyzer.detect_segmentation_strategy(content, doc_type)
|
| 1538 |
+
|
| 1539 |
+
# Create segments
|
| 1540 |
+
segments = segmenter.segment_document(content, strategy)
|
| 1541 |
+
|
| 1542 |
+
# Create document index
|
| 1543 |
+
document_index = DocumentIndex(
|
| 1544 |
+
document_path=md_file_path,
|
| 1545 |
+
document_type=doc_type,
|
| 1546 |
+
segmentation_strategy=strategy,
|
| 1547 |
+
total_segments=len(segments),
|
| 1548 |
+
total_chars=len(content),
|
| 1549 |
+
segments=segments,
|
| 1550 |
+
created_at=datetime.now().isoformat(),
|
| 1551 |
+
)
|
| 1552 |
+
|
| 1553 |
+
# Save segments
|
| 1554 |
+
ensure_segments_dir_exists(segments_dir)
|
| 1555 |
+
|
| 1556 |
+
# Save document index
|
| 1557 |
+
with open(index_file_path, "w", encoding="utf-8") as f:
|
| 1558 |
+
json.dump(
|
| 1559 |
+
asdict(document_index), f, ensure_ascii=False, indent=2, default=str
|
| 1560 |
+
)
|
| 1561 |
+
|
| 1562 |
+
# Save individual segment files for fallback
|
| 1563 |
+
for segment in segments:
|
| 1564 |
+
segment_file_path = os.path.join(segments_dir, f"segment_{segment.id}.md")
|
| 1565 |
+
with open(segment_file_path, "w", encoding="utf-8") as f:
|
| 1566 |
+
f.write(f"# {segment.title}\n\n")
|
| 1567 |
+
f.write(f"**Content Type:** {segment.content_type}\n")
|
| 1568 |
+
f.write(f"**Keywords:** {', '.join(segment.keywords[:10])}\n\n")
|
| 1569 |
+
f.write(segment.content)
|
| 1570 |
+
|
| 1571 |
+
# Store in memory
|
| 1572 |
+
DOCUMENT_INDEXES[paper_dir] = document_index
|
| 1573 |
+
|
| 1574 |
+
logger.info(
|
| 1575 |
+
f"Document segmentation completed: {len(segments)} segments created"
|
| 1576 |
+
)
|
| 1577 |
+
|
| 1578 |
+
return json.dumps(
|
| 1579 |
+
{
|
| 1580 |
+
"status": "success",
|
| 1581 |
+
"message": f"Document analysis completed with {strategy} strategy",
|
| 1582 |
+
"document_type": doc_type,
|
| 1583 |
+
"segmentation_strategy": strategy,
|
| 1584 |
+
"segments_dir": segments_dir,
|
| 1585 |
+
"total_segments": len(segments),
|
| 1586 |
+
"total_chars": len(content),
|
| 1587 |
+
},
|
| 1588 |
+
ensure_ascii=False,
|
| 1589 |
+
indent=2,
|
| 1590 |
+
)
|
| 1591 |
+
|
| 1592 |
+
except Exception as e:
|
| 1593 |
+
logger.error(f"Error in analyze_and_segment_document: {e}")
|
| 1594 |
+
return json.dumps(
|
| 1595 |
+
{"status": "error", "message": f"Failed to analyze document: {str(e)}"},
|
| 1596 |
+
ensure_ascii=False,
|
| 1597 |
+
indent=2,
|
| 1598 |
+
)
|
| 1599 |
+
|
| 1600 |
+
|
| 1601 |
+
@mcp.tool()
|
| 1602 |
+
async def read_document_segments(
|
| 1603 |
+
paper_dir: str,
|
| 1604 |
+
query_type: str,
|
| 1605 |
+
keywords: List[str] = None,
|
| 1606 |
+
max_segments: int = 3,
|
| 1607 |
+
max_total_chars: int = None,
|
| 1608 |
+
) -> str:
|
| 1609 |
+
"""
|
| 1610 |
+
Intelligently retrieve relevant document segments based on query type
|
| 1611 |
+
|
| 1612 |
+
Args:
|
| 1613 |
+
paper_dir: Path to the paper directory
|
| 1614 |
+
query_type: Type of query - "concept_analysis", "algorithm_extraction", or "code_planning"
|
| 1615 |
+
keywords: Optional list of keywords to search for
|
| 1616 |
+
max_segments: Maximum number of segments to return
|
| 1617 |
+
max_total_chars: Maximum total characters to return
|
| 1618 |
+
|
| 1619 |
+
Returns:
|
| 1620 |
+
JSON string with selected segments
|
| 1621 |
+
"""
|
| 1622 |
+
try:
|
| 1623 |
+
# Ensure document is analyzed
|
| 1624 |
+
if paper_dir not in DOCUMENT_INDEXES:
|
| 1625 |
+
segments_dir = get_segments_dir(paper_dir)
|
| 1626 |
+
index_file_path = os.path.join(segments_dir, "document_index.json")
|
| 1627 |
+
|
| 1628 |
+
if os.path.exists(index_file_path):
|
| 1629 |
+
with open(index_file_path, "r", encoding="utf-8") as f:
|
| 1630 |
+
index_data = json.load(f)
|
| 1631 |
+
# Convert dict back to DocumentIndex with backward compatibility
|
| 1632 |
+
segments_data = []
|
| 1633 |
+
for seg_data in index_data.get("segments", []):
|
| 1634 |
+
# Ensure all required fields exist, provide default values
|
| 1635 |
+
segment_dict = dict(seg_data)
|
| 1636 |
+
|
| 1637 |
+
# Compatibility handling: add missing fields
|
| 1638 |
+
if "content_type" not in segment_dict:
|
| 1639 |
+
segment_dict["content_type"] = "general"
|
| 1640 |
+
if "keywords" not in segment_dict:
|
| 1641 |
+
segment_dict["keywords"] = []
|
| 1642 |
+
if "relevance_scores" not in segment_dict:
|
| 1643 |
+
segment_dict["relevance_scores"] = {
|
| 1644 |
+
"concept_analysis": 0.5,
|
| 1645 |
+
"algorithm_extraction": 0.5,
|
| 1646 |
+
"code_planning": 0.5,
|
| 1647 |
+
}
|
| 1648 |
+
if "section_path" not in segment_dict:
|
| 1649 |
+
segment_dict["section_path"] = segment_dict.get(
|
| 1650 |
+
"title", "Unknown"
|
| 1651 |
+
)
|
| 1652 |
+
|
| 1653 |
+
segment = DocumentSegment(**segment_dict)
|
| 1654 |
+
segments_data.append(segment)
|
| 1655 |
+
|
| 1656 |
+
index_data["segments"] = segments_data
|
| 1657 |
+
DOCUMENT_INDEXES[paper_dir] = DocumentIndex(**index_data)
|
| 1658 |
+
else:
|
| 1659 |
+
# Auto-analyze if not found
|
| 1660 |
+
await analyze_and_segment_document(paper_dir)
|
| 1661 |
+
|
| 1662 |
+
document_index = DOCUMENT_INDEXES[paper_dir]
|
| 1663 |
+
|
| 1664 |
+
# Dynamically calculate character limit
|
| 1665 |
+
if max_total_chars is None:
|
| 1666 |
+
max_total_chars = _calculate_adaptive_char_limit(document_index, query_type)
|
| 1667 |
+
|
| 1668 |
+
# Score and rank segments with enhanced algorithm
|
| 1669 |
+
scored_segments = []
|
| 1670 |
+
for segment in document_index.segments:
|
| 1671 |
+
# Base relevance score (already enhanced in new system)
|
| 1672 |
+
relevance_score = segment.relevance_scores.get(query_type, 0.5)
|
| 1673 |
+
|
| 1674 |
+
# Enhanced keyword matching with position weighting
|
| 1675 |
+
if keywords:
|
| 1676 |
+
keyword_score = _calculate_enhanced_keyword_score(segment, keywords)
|
| 1677 |
+
relevance_score += keyword_score
|
| 1678 |
+
|
| 1679 |
+
# Content completeness bonus
|
| 1680 |
+
completeness_bonus = _calculate_completeness_bonus(segment, document_index)
|
| 1681 |
+
relevance_score += completeness_bonus
|
| 1682 |
+
|
| 1683 |
+
scored_segments.append((segment, relevance_score))
|
| 1684 |
+
|
| 1685 |
+
# Sort by enhanced relevance score
|
| 1686 |
+
scored_segments.sort(key=lambda x: x[1], reverse=True)
|
| 1687 |
+
|
| 1688 |
+
# Intelligent segment selection with integrity preservation
|
| 1689 |
+
selected_segments = _select_segments_with_integrity(
|
| 1690 |
+
scored_segments, max_segments, max_total_chars, query_type
|
| 1691 |
+
)
|
| 1692 |
+
|
| 1693 |
+
total_chars = sum(seg["char_count"] for seg in selected_segments)
|
| 1694 |
+
|
| 1695 |
+
logger.info(
|
| 1696 |
+
f"Selected {len(selected_segments)} segments for {query_type} query"
|
| 1697 |
+
)
|
| 1698 |
+
|
| 1699 |
+
return json.dumps(
|
| 1700 |
+
{
|
| 1701 |
+
"status": "success",
|
| 1702 |
+
"query_type": query_type,
|
| 1703 |
+
"keywords": keywords or [],
|
| 1704 |
+
"total_segments_available": len(document_index.segments),
|
| 1705 |
+
"segments_selected": len(selected_segments),
|
| 1706 |
+
"total_chars": total_chars,
|
| 1707 |
+
"max_chars_used": max_total_chars,
|
| 1708 |
+
"segments": selected_segments,
|
| 1709 |
+
},
|
| 1710 |
+
ensure_ascii=False,
|
| 1711 |
+
indent=2,
|
| 1712 |
+
)
|
| 1713 |
+
|
| 1714 |
+
except Exception as e:
|
| 1715 |
+
logger.error(f"Error in read_document_segments: {e}")
|
| 1716 |
+
return json.dumps(
|
| 1717 |
+
{
|
| 1718 |
+
"status": "error",
|
| 1719 |
+
"message": f"Failed to read document segments: {str(e)}",
|
| 1720 |
+
},
|
| 1721 |
+
ensure_ascii=False,
|
| 1722 |
+
indent=2,
|
| 1723 |
+
)
|
| 1724 |
+
|
| 1725 |
+
|
| 1726 |
+
@mcp.tool()
|
| 1727 |
+
async def get_document_overview(paper_dir: str) -> str:
|
| 1728 |
+
"""
|
| 1729 |
+
Get overview of document structure and available segments
|
| 1730 |
+
|
| 1731 |
+
Args:
|
| 1732 |
+
paper_dir: Path to the paper directory
|
| 1733 |
+
|
| 1734 |
+
Returns:
|
| 1735 |
+
JSON string with document overview
|
| 1736 |
+
"""
|
| 1737 |
+
try:
|
| 1738 |
+
# Ensure document is analyzed
|
| 1739 |
+
if paper_dir not in DOCUMENT_INDEXES:
|
| 1740 |
+
await analyze_and_segment_document(paper_dir)
|
| 1741 |
+
|
| 1742 |
+
document_index = DOCUMENT_INDEXES[paper_dir]
|
| 1743 |
+
|
| 1744 |
+
# Create overview
|
| 1745 |
+
segment_summaries = []
|
| 1746 |
+
for segment in document_index.segments:
|
| 1747 |
+
segment_summaries.append(
|
| 1748 |
+
{
|
| 1749 |
+
"id": segment.id,
|
| 1750 |
+
"title": segment.title,
|
| 1751 |
+
"content_type": segment.content_type,
|
| 1752 |
+
"char_count": segment.char_count,
|
| 1753 |
+
"keywords": segment.keywords[:5], # Top 5 keywords
|
| 1754 |
+
"relevance_scores": segment.relevance_scores,
|
| 1755 |
+
}
|
| 1756 |
+
)
|
| 1757 |
+
|
| 1758 |
+
return json.dumps(
|
| 1759 |
+
{
|
| 1760 |
+
"status": "success",
|
| 1761 |
+
"document_path": document_index.document_path,
|
| 1762 |
+
"document_type": document_index.document_type,
|
| 1763 |
+
"segmentation_strategy": document_index.segmentation_strategy,
|
| 1764 |
+
"total_segments": document_index.total_segments,
|
| 1765 |
+
"total_chars": document_index.total_chars,
|
| 1766 |
+
"created_at": document_index.created_at,
|
| 1767 |
+
"segments_overview": segment_summaries,
|
| 1768 |
+
},
|
| 1769 |
+
ensure_ascii=False,
|
| 1770 |
+
indent=2,
|
| 1771 |
+
)
|
| 1772 |
+
|
| 1773 |
+
except Exception as e:
|
| 1774 |
+
logger.error(f"Error in get_document_overview: {e}")
|
| 1775 |
+
return json.dumps(
|
| 1776 |
+
{
|
| 1777 |
+
"status": "error",
|
| 1778 |
+
"message": f"Failed to get document overview: {str(e)}",
|
| 1779 |
+
},
|
| 1780 |
+
ensure_ascii=False,
|
| 1781 |
+
indent=2,
|
| 1782 |
+
)
|
| 1783 |
+
|
| 1784 |
+
|
| 1785 |
+
# =============== Enhanced retrieval system helper methods ===============
|
| 1786 |
+
|
| 1787 |
+
|
| 1788 |
+
def _calculate_adaptive_char_limit(
|
| 1789 |
+
document_index: DocumentIndex, query_type: str
|
| 1790 |
+
) -> int:
|
| 1791 |
+
"""Dynamically calculate character limit based on document complexity and query type"""
|
| 1792 |
+
base_limit = 6000
|
| 1793 |
+
|
| 1794 |
+
# Adjust based on document type
|
| 1795 |
+
if document_index.document_type == "research_paper":
|
| 1796 |
+
base_limit = 10000
|
| 1797 |
+
elif document_index.document_type == "algorithm_focused":
|
| 1798 |
+
base_limit = 12000
|
| 1799 |
+
elif document_index.segmentation_strategy == "algorithm_preserve_integrity":
|
| 1800 |
+
base_limit = 15000
|
| 1801 |
+
|
| 1802 |
+
# Adjust based on query type
|
| 1803 |
+
query_multipliers = {
|
| 1804 |
+
"algorithm_extraction": 1.5, # Algorithms need more context
|
| 1805 |
+
"concept_analysis": 1.2,
|
| 1806 |
+
"code_planning": 1.3,
|
| 1807 |
+
}
|
| 1808 |
+
|
| 1809 |
+
multiplier = query_multipliers.get(query_type, 1.0)
|
| 1810 |
+
return int(base_limit * multiplier)
|
| 1811 |
+
|
| 1812 |
+
|
| 1813 |
+
def _calculate_enhanced_keyword_score(
|
| 1814 |
+
segment: DocumentSegment, keywords: List[str]
|
| 1815 |
+
) -> float:
|
| 1816 |
+
"""Calculate enhanced keyword matching score"""
|
| 1817 |
+
score = 0.0
|
| 1818 |
+
content_lower = segment.content.lower()
|
| 1819 |
+
title_lower = segment.title.lower()
|
| 1820 |
+
|
| 1821 |
+
for keyword in keywords:
|
| 1822 |
+
keyword_lower = keyword.lower()
|
| 1823 |
+
|
| 1824 |
+
# Title matching has higher weight
|
| 1825 |
+
if keyword_lower in title_lower:
|
| 1826 |
+
score += 0.3
|
| 1827 |
+
|
| 1828 |
+
# Content matching
|
| 1829 |
+
content_matches = content_lower.count(keyword_lower)
|
| 1830 |
+
if content_matches > 0:
|
| 1831 |
+
# Consider term frequency and position
|
| 1832 |
+
frequency_score = min(0.2, content_matches * 0.05)
|
| 1833 |
+
|
| 1834 |
+
# Check if in important position (first 25% of content)
|
| 1835 |
+
early_content = content_lower[: len(content_lower) // 4]
|
| 1836 |
+
if keyword_lower in early_content:
|
| 1837 |
+
frequency_score += 0.1
|
| 1838 |
+
|
| 1839 |
+
score += frequency_score
|
| 1840 |
+
|
| 1841 |
+
return min(0.6, score) # Limit maximum bonus
|
| 1842 |
+
|
| 1843 |
+
|
| 1844 |
+
def _calculate_completeness_bonus(
|
| 1845 |
+
segment: DocumentSegment, document_index: DocumentIndex
|
| 1846 |
+
) -> float:
|
| 1847 |
+
"""Calculate content completeness bonus"""
|
| 1848 |
+
bonus = 0.0
|
| 1849 |
+
|
| 1850 |
+
# Completeness bonus for algorithm and formula content
|
| 1851 |
+
if segment.content_type in ["algorithm", "formula", "merged"]:
|
| 1852 |
+
bonus += 0.2
|
| 1853 |
+
|
| 1854 |
+
# Long paragraphs usually contain more complete information
|
| 1855 |
+
if segment.char_count > 2000:
|
| 1856 |
+
bonus += 0.1
|
| 1857 |
+
elif segment.char_count > 4000:
|
| 1858 |
+
bonus += 0.15
|
| 1859 |
+
|
| 1860 |
+
# High importance paragraph bonus
|
| 1861 |
+
if segment.relevance_scores.get("algorithm_extraction", 0) > 0.8:
|
| 1862 |
+
bonus += 0.1
|
| 1863 |
+
|
| 1864 |
+
return min(0.3, bonus)
|
| 1865 |
+
|
| 1866 |
+
|
| 1867 |
+
def _select_segments_with_integrity(
|
| 1868 |
+
scored_segments: List[Tuple],
|
| 1869 |
+
max_segments: int,
|
| 1870 |
+
max_total_chars: int,
|
| 1871 |
+
query_type: str,
|
| 1872 |
+
) -> List[Dict]:
|
| 1873 |
+
"""Intelligently select segments while maintaining content integrity"""
|
| 1874 |
+
selected_segments = []
|
| 1875 |
+
total_chars = 0
|
| 1876 |
+
|
| 1877 |
+
# First select the highest scoring segments
|
| 1878 |
+
for segment, score in scored_segments:
|
| 1879 |
+
if len(selected_segments) >= max_segments:
|
| 1880 |
+
break
|
| 1881 |
+
|
| 1882 |
+
if total_chars + segment.char_count <= max_total_chars:
|
| 1883 |
+
selected_segments.append(
|
| 1884 |
+
{
|
| 1885 |
+
"id": segment.id,
|
| 1886 |
+
"title": segment.title,
|
| 1887 |
+
"content": segment.content,
|
| 1888 |
+
"content_type": segment.content_type,
|
| 1889 |
+
"relevance_score": score,
|
| 1890 |
+
"char_count": segment.char_count,
|
| 1891 |
+
}
|
| 1892 |
+
)
|
| 1893 |
+
total_chars += segment.char_count
|
| 1894 |
+
elif len(selected_segments) == 0:
|
| 1895 |
+
# If the first segment exceeds the limit, truncate but preserve it
|
| 1896 |
+
truncated_content = (
|
| 1897 |
+
segment.content[: max_total_chars - 200]
|
| 1898 |
+
+ "\n\n[Content truncated for length...]"
|
| 1899 |
+
)
|
| 1900 |
+
selected_segments.append(
|
| 1901 |
+
{
|
| 1902 |
+
"id": segment.id,
|
| 1903 |
+
"title": segment.title,
|
| 1904 |
+
"content": truncated_content,
|
| 1905 |
+
"content_type": segment.content_type,
|
| 1906 |
+
"relevance_score": score,
|
| 1907 |
+
"char_count": len(truncated_content),
|
| 1908 |
+
}
|
| 1909 |
+
)
|
| 1910 |
+
break
|
| 1911 |
+
|
| 1912 |
+
# If there's remaining space, try to add relevant small segments
|
| 1913 |
+
remaining_chars = max_total_chars - total_chars
|
| 1914 |
+
if remaining_chars > 500 and len(selected_segments) < max_segments:
|
| 1915 |
+
for segment, score in scored_segments[len(selected_segments) :]:
|
| 1916 |
+
if (
|
| 1917 |
+
segment.char_count <= remaining_chars
|
| 1918 |
+
and len(selected_segments) < max_segments
|
| 1919 |
+
):
|
| 1920 |
+
selected_segments.append(
|
| 1921 |
+
{
|
| 1922 |
+
"id": segment.id,
|
| 1923 |
+
"title": segment.title,
|
| 1924 |
+
"content": segment.content,
|
| 1925 |
+
"content_type": segment.content_type,
|
| 1926 |
+
"relevance_score": score,
|
| 1927 |
+
"char_count": segment.char_count,
|
| 1928 |
+
}
|
| 1929 |
+
)
|
| 1930 |
+
remaining_chars -= segment.char_count
|
| 1931 |
+
|
| 1932 |
+
return selected_segments
|
| 1933 |
+
|
| 1934 |
+
|
| 1935 |
+
if __name__ == "__main__":
|
| 1936 |
+
# Run the MCP server
|
| 1937 |
+
mcp.run()
|
projects/ui/DeepCode/tools/git_command.py
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
GitHub Repository Downloader MCP Tool using FastMCP
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import asyncio
|
| 7 |
+
import os
|
| 8 |
+
import re
|
| 9 |
+
from typing import Dict, List, Optional
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
from mcp.server import FastMCP
|
| 13 |
+
|
| 14 |
+
# 创建 FastMCP 实例
|
| 15 |
+
mcp = FastMCP("github-downloader")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class GitHubURLExtractor:
|
| 19 |
+
"""提取GitHub URL的工具类"""
|
| 20 |
+
|
| 21 |
+
@staticmethod
|
| 22 |
+
def extract_github_urls(text: str) -> List[str]:
|
| 23 |
+
"""从文本中提取GitHub URLs"""
|
| 24 |
+
patterns = [
|
| 25 |
+
# 标准HTTPS URL
|
| 26 |
+
r"https?://github\.com/[\w\-\.]+/[\w\-\.]+(?:\.git)?",
|
| 27 |
+
# SSH URL
|
| 28 |
+
r"git@github\.com:[\w\-\.]+/[\w\-\.]+(?:\.git)?",
|
| 29 |
+
# 短格式 owner/repo - 更严格的匹配
|
| 30 |
+
r"(?<!\S)(?<!/)(?<!\.)([\w\-\.]+/[\w\-\.]+)(?!/)(?!\S)",
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
urls = []
|
| 34 |
+
for pattern in patterns:
|
| 35 |
+
matches = re.findall(pattern, text, re.IGNORECASE)
|
| 36 |
+
for match in matches:
|
| 37 |
+
# 处理短格式
|
| 38 |
+
if isinstance(match, tuple):
|
| 39 |
+
match = match[0]
|
| 40 |
+
|
| 41 |
+
# 清理URL
|
| 42 |
+
if match.startswith("git@"):
|
| 43 |
+
url = match.replace("git@github.com:", "https://github.com/")
|
| 44 |
+
elif match.startswith("http"):
|
| 45 |
+
url = match
|
| 46 |
+
else:
|
| 47 |
+
# 处理短格式 (owner/repo) - 添加更多验证
|
| 48 |
+
if "/" in match and not any(
|
| 49 |
+
x in match for x in ["./", "../", "deepcode_lab", "tools"]
|
| 50 |
+
):
|
| 51 |
+
parts = match.split("/")
|
| 52 |
+
if (
|
| 53 |
+
len(parts) == 2
|
| 54 |
+
and all(
|
| 55 |
+
part.replace("-", "").replace("_", "").isalnum()
|
| 56 |
+
for part in parts
|
| 57 |
+
)
|
| 58 |
+
and not any(part.startswith(".") for part in parts)
|
| 59 |
+
):
|
| 60 |
+
url = f"https://github.com/{match}"
|
| 61 |
+
else:
|
| 62 |
+
continue
|
| 63 |
+
else:
|
| 64 |
+
continue
|
| 65 |
+
|
| 66 |
+
# 规范化 URL
|
| 67 |
+
url = url.rstrip(".git")
|
| 68 |
+
url = url.rstrip("/")
|
| 69 |
+
|
| 70 |
+
# 修复重复的 github.com
|
| 71 |
+
if "github.com/github.com/" in url:
|
| 72 |
+
url = url.replace("github.com/github.com/", "github.com/")
|
| 73 |
+
|
| 74 |
+
urls.append(url)
|
| 75 |
+
|
| 76 |
+
return list(set(urls)) # 去重
|
| 77 |
+
|
| 78 |
+
@staticmethod
|
| 79 |
+
def extract_target_path(text: str) -> Optional[str]:
|
| 80 |
+
"""从文本中提取目标路径"""
|
| 81 |
+
# 路径指示词模式
|
| 82 |
+
patterns = [
|
| 83 |
+
r'(?:to|into|in|at)\s+(?:folder|directory|path)?\s*["\']?([^\s"\']+)["\']?',
|
| 84 |
+
r'(?:save|download|clone)\s+(?:to|into|at)\s+["\']?([^\s"\']+)["\']?',
|
| 85 |
+
# 中文支持
|
| 86 |
+
r'(?:到|在|保存到|下载到|克隆到)\s*["\']?([^\s"\']+)["\']?',
|
| 87 |
+
]
|
| 88 |
+
|
| 89 |
+
for pattern in patterns:
|
| 90 |
+
match = re.search(pattern, text, re.IGNORECASE)
|
| 91 |
+
if match:
|
| 92 |
+
path = match.group(1).strip("。,,.")
|
| 93 |
+
# 过滤掉通用词
|
| 94 |
+
if path and path.lower() not in [
|
| 95 |
+
"here",
|
| 96 |
+
"there",
|
| 97 |
+
"current",
|
| 98 |
+
"local",
|
| 99 |
+
"这里",
|
| 100 |
+
"当前",
|
| 101 |
+
"本地",
|
| 102 |
+
]:
|
| 103 |
+
return path
|
| 104 |
+
|
| 105 |
+
return None
|
| 106 |
+
|
| 107 |
+
@staticmethod
|
| 108 |
+
def infer_repo_name(url: str) -> str:
|
| 109 |
+
"""从URL推断仓库名称"""
|
| 110 |
+
url = url.rstrip(".git")
|
| 111 |
+
if "github.com" in url:
|
| 112 |
+
parts = url.split("/")
|
| 113 |
+
if len(parts) >= 2:
|
| 114 |
+
return parts[-1]
|
| 115 |
+
return "repository"
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
async def check_git_installed() -> bool:
|
| 119 |
+
"""检查Git是否安装"""
|
| 120 |
+
try:
|
| 121 |
+
proc = await asyncio.create_subprocess_exec(
|
| 122 |
+
"git",
|
| 123 |
+
"--version",
|
| 124 |
+
stdout=asyncio.subprocess.PIPE,
|
| 125 |
+
stderr=asyncio.subprocess.PIPE,
|
| 126 |
+
)
|
| 127 |
+
await proc.wait()
|
| 128 |
+
return proc.returncode == 0
|
| 129 |
+
except Exception:
|
| 130 |
+
return False
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
async def clone_repository(repo_url: str, target_path: str) -> Dict[str, any]:
|
| 134 |
+
"""执行git clone命令"""
|
| 135 |
+
try:
|
| 136 |
+
proc = await asyncio.create_subprocess_exec(
|
| 137 |
+
"git",
|
| 138 |
+
"clone",
|
| 139 |
+
repo_url,
|
| 140 |
+
target_path,
|
| 141 |
+
stdout=asyncio.subprocess.PIPE,
|
| 142 |
+
stderr=asyncio.subprocess.PIPE,
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
stdout, stderr = await proc.communicate()
|
| 146 |
+
|
| 147 |
+
return {
|
| 148 |
+
"success": proc.returncode == 0,
|
| 149 |
+
"stdout": stdout.decode("utf-8", errors="replace"),
|
| 150 |
+
"stderr": stderr.decode("utf-8", errors="replace"),
|
| 151 |
+
"returncode": proc.returncode,
|
| 152 |
+
}
|
| 153 |
+
except Exception as e:
|
| 154 |
+
return {"success": False, "error": str(e)}
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
@mcp.tool()
|
| 158 |
+
async def download_github_repo(instruction: str) -> str:
|
| 159 |
+
"""
|
| 160 |
+
Download GitHub repositories from natural language instructions.
|
| 161 |
+
|
| 162 |
+
Args:
|
| 163 |
+
instruction: Natural language text containing GitHub URLs and optional target paths
|
| 164 |
+
|
| 165 |
+
Returns:
|
| 166 |
+
Status message about the download operation
|
| 167 |
+
|
| 168 |
+
Examples:
|
| 169 |
+
- "Download https://github.com/openai/gpt-3"
|
| 170 |
+
- "Clone microsoft/vscode to my-projects folder"
|
| 171 |
+
- "Get https://github.com/facebook/react"
|
| 172 |
+
"""
|
| 173 |
+
# 检查Git是否安装
|
| 174 |
+
if not await check_git_installed():
|
| 175 |
+
return "❌ Error: Git is not installed or not in system PATH"
|
| 176 |
+
|
| 177 |
+
extractor = GitHubURLExtractor()
|
| 178 |
+
|
| 179 |
+
# 提取GitHub URLs
|
| 180 |
+
urls = extractor.extract_github_urls(instruction)
|
| 181 |
+
if not urls:
|
| 182 |
+
return "❌ No GitHub URLs found in the instruction"
|
| 183 |
+
|
| 184 |
+
# 提取目标路径
|
| 185 |
+
target_path = extractor.extract_target_path(instruction)
|
| 186 |
+
|
| 187 |
+
# 下载仓库
|
| 188 |
+
results = []
|
| 189 |
+
for url in urls:
|
| 190 |
+
try:
|
| 191 |
+
# 准备目标路径
|
| 192 |
+
if target_path:
|
| 193 |
+
# 判断是否为绝对路径
|
| 194 |
+
if os.path.isabs(target_path):
|
| 195 |
+
# 如果是绝对路径,直接使用
|
| 196 |
+
final_path = target_path
|
| 197 |
+
# 如果目标路径是目录,添加仓库名
|
| 198 |
+
if os.path.basename(target_path) == "" or target_path.endswith("/"):
|
| 199 |
+
final_path = os.path.join(
|
| 200 |
+
target_path, extractor.infer_repo_name(url)
|
| 201 |
+
)
|
| 202 |
+
else:
|
| 203 |
+
# 如果是相对路径,保持相对路径
|
| 204 |
+
final_path = target_path
|
| 205 |
+
# 如果目标路径是目录,添加仓库名
|
| 206 |
+
if os.path.basename(target_path) == "" or target_path.endswith("/"):
|
| 207 |
+
final_path = os.path.join(
|
| 208 |
+
target_path, extractor.infer_repo_name(url)
|
| 209 |
+
)
|
| 210 |
+
else:
|
| 211 |
+
final_path = extractor.infer_repo_name(url)
|
| 212 |
+
|
| 213 |
+
# 如果是相对路径,确保使用相对路径格式
|
| 214 |
+
if not os.path.isabs(final_path):
|
| 215 |
+
final_path = os.path.normpath(final_path)
|
| 216 |
+
if final_path.startswith("/"):
|
| 217 |
+
final_path = final_path.lstrip("/")
|
| 218 |
+
|
| 219 |
+
# 确保父目录存在
|
| 220 |
+
parent_dir = os.path.dirname(final_path)
|
| 221 |
+
if parent_dir:
|
| 222 |
+
os.makedirs(parent_dir, exist_ok=True)
|
| 223 |
+
|
| 224 |
+
# 检查目标路径是否已存在
|
| 225 |
+
if os.path.exists(final_path):
|
| 226 |
+
results.append(
|
| 227 |
+
f"❌ Failed to download {url}: Target path already exists: {final_path}"
|
| 228 |
+
)
|
| 229 |
+
continue
|
| 230 |
+
|
| 231 |
+
# 执行克隆
|
| 232 |
+
result = await clone_repository(url, final_path)
|
| 233 |
+
|
| 234 |
+
if result["success"]:
|
| 235 |
+
msg = f"✅ Successfully downloaded: {url}\n"
|
| 236 |
+
msg += f" Location: {final_path}"
|
| 237 |
+
if result.get("stdout"):
|
| 238 |
+
msg += f"\n {result['stdout'].strip()}"
|
| 239 |
+
else:
|
| 240 |
+
msg = f"❌ Failed to download: {url}\n"
|
| 241 |
+
msg += f" Error: {result.get('error', result.get('stderr', 'Unknown error'))}"
|
| 242 |
+
|
| 243 |
+
except Exception as e:
|
| 244 |
+
msg = f"❌ Failed to download: {url}\n"
|
| 245 |
+
msg += f" Error: {str(e)}"
|
| 246 |
+
|
| 247 |
+
results.append(msg)
|
| 248 |
+
|
| 249 |
+
return "\n\n".join(results)
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
@mcp.tool()
|
| 253 |
+
async def parse_github_urls(text: str) -> str:
|
| 254 |
+
"""
|
| 255 |
+
Extract GitHub URLs and target paths from text.
|
| 256 |
+
|
| 257 |
+
Args:
|
| 258 |
+
text: Text containing GitHub URLs
|
| 259 |
+
|
| 260 |
+
Returns:
|
| 261 |
+
Parsed GitHub URLs and target path information
|
| 262 |
+
"""
|
| 263 |
+
extractor = GitHubURLExtractor()
|
| 264 |
+
|
| 265 |
+
urls = extractor.extract_github_urls(text)
|
| 266 |
+
target_path = extractor.extract_target_path(text)
|
| 267 |
+
|
| 268 |
+
content = "📝 Parsed information:\n\n"
|
| 269 |
+
|
| 270 |
+
if urls:
|
| 271 |
+
content += "GitHub URLs found:\n"
|
| 272 |
+
for url in urls:
|
| 273 |
+
content += f" • {url}\n"
|
| 274 |
+
else:
|
| 275 |
+
content += "No GitHub URLs found\n"
|
| 276 |
+
|
| 277 |
+
if target_path:
|
| 278 |
+
content += f"\nTarget path: {target_path}"
|
| 279 |
+
else:
|
| 280 |
+
content += "\nTarget path: Not specified (will use repository name)"
|
| 281 |
+
|
| 282 |
+
return content
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
@mcp.tool()
|
| 286 |
+
async def git_clone(
|
| 287 |
+
repo_url: str, target_path: Optional[str] = None, branch: Optional[str] = None
|
| 288 |
+
) -> str:
|
| 289 |
+
"""
|
| 290 |
+
Clone a specific GitHub repository.
|
| 291 |
+
|
| 292 |
+
Args:
|
| 293 |
+
repo_url: GitHub repository URL
|
| 294 |
+
target_path: Optional target directory path
|
| 295 |
+
branch: Optional branch name to clone
|
| 296 |
+
|
| 297 |
+
Returns:
|
| 298 |
+
Status message about the clone operation
|
| 299 |
+
"""
|
| 300 |
+
# 检查Git是否安装
|
| 301 |
+
if not await check_git_installed():
|
| 302 |
+
return "❌ Error: Git is not installed or not in system PATH"
|
| 303 |
+
|
| 304 |
+
# 准备目标路径
|
| 305 |
+
if not target_path:
|
| 306 |
+
extractor = GitHubURLExtractor()
|
| 307 |
+
target_path = extractor.infer_repo_name(repo_url)
|
| 308 |
+
|
| 309 |
+
# 转换���绝对路径
|
| 310 |
+
if not os.path.isabs(target_path):
|
| 311 |
+
target_path = str(Path.cwd() / target_path)
|
| 312 |
+
|
| 313 |
+
# 检查目标路径
|
| 314 |
+
if os.path.exists(target_path):
|
| 315 |
+
return f"❌ Error: Target path already exists: {target_path}"
|
| 316 |
+
|
| 317 |
+
# 构建命令
|
| 318 |
+
cmd = ["git", "clone"]
|
| 319 |
+
if branch:
|
| 320 |
+
cmd.extend(["-b", branch])
|
| 321 |
+
cmd.extend([repo_url, target_path])
|
| 322 |
+
|
| 323 |
+
# 执行克隆
|
| 324 |
+
try:
|
| 325 |
+
proc = await asyncio.create_subprocess_exec(
|
| 326 |
+
*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
stdout, stderr = await proc.communicate()
|
| 330 |
+
|
| 331 |
+
if proc.returncode == 0:
|
| 332 |
+
result = "✅ Successfully cloned repository\n"
|
| 333 |
+
result += f"Repository: {repo_url}\n"
|
| 334 |
+
result += f"Location: {target_path}"
|
| 335 |
+
if branch:
|
| 336 |
+
result += f"\nBranch: {branch}"
|
| 337 |
+
return result
|
| 338 |
+
else:
|
| 339 |
+
return f"❌ Clone failed\nError: {stderr.decode('utf-8', errors='replace')}"
|
| 340 |
+
|
| 341 |
+
except Exception as e:
|
| 342 |
+
return f"❌ Clone failed\nError: {str(e)}"
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
# 主程序入口
|
| 346 |
+
if __name__ == "__main__":
|
| 347 |
+
print("🚀 GitHub Repository Downloader MCP Tool")
|
| 348 |
+
print("📝 Starting server with FastMCP...")
|
| 349 |
+
print("\nAvailable tools:")
|
| 350 |
+
print(" • download_github_repo - Download repos from natural language")
|
| 351 |
+
print(" • parse_github_urls - Extract GitHub URLs from text")
|
| 352 |
+
print(" • git_clone - Clone a specific repository")
|
| 353 |
+
print("")
|
| 354 |
+
|
| 355 |
+
# 运行服务器
|
| 356 |
+
mcp.run()
|
projects/ui/DeepCode/tools/indexer_config.yaml
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Code Indexer Configuration File
|
| 2 |
+
# Configure various aspects of the code indexing process
|
| 3 |
+
|
| 4 |
+
# Paths Configuration
|
| 5 |
+
paths:
|
| 6 |
+
code_base_path: "D:/Documents/GitHub/Code-Agent/examples/input/paper1/code_base"
|
| 7 |
+
output_dir: "D:/Documents/GitHub/Code-Agent/examples/input/paper1/indexes"
|
| 8 |
+
|
| 9 |
+
# File Analysis Settings
|
| 10 |
+
file_analysis:
|
| 11 |
+
# Supported file extensions for analysis
|
| 12 |
+
supported_extensions:
|
| 13 |
+
- ".py" # Python
|
| 14 |
+
- ".js" # JavaScript
|
| 15 |
+
- ".ts" # TypeScript
|
| 16 |
+
- ".java" # Java
|
| 17 |
+
- ".cpp" # C++
|
| 18 |
+
- ".c" # C
|
| 19 |
+
- ".h" # C Header
|
| 20 |
+
- ".hpp" # C++ Header
|
| 21 |
+
- ".cs" # C#
|
| 22 |
+
- ".php" # PHP
|
| 23 |
+
- ".rb" # Ruby
|
| 24 |
+
- ".go" # Go
|
| 25 |
+
- ".rs" # Rust
|
| 26 |
+
- ".scala" # Scala
|
| 27 |
+
- ".kt" # Kotlin
|
| 28 |
+
- ".swift" # Swift
|
| 29 |
+
- ".r" # R
|
| 30 |
+
- ".sql" # SQL
|
| 31 |
+
- ".sh" # Shell Script
|
| 32 |
+
- ".bat" # Batch File
|
| 33 |
+
- ".ps1" # PowerShell
|
| 34 |
+
- ".yaml" # YAML
|
| 35 |
+
- ".yml" # YAML
|
| 36 |
+
- ".json" # JSON
|
| 37 |
+
- ".xml" # XML
|
| 38 |
+
- ".toml" # TOML
|
| 39 |
+
|
| 40 |
+
# Directories to skip during traversal
|
| 41 |
+
skip_directories:
|
| 42 |
+
- "__pycache__"
|
| 43 |
+
- "node_modules"
|
| 44 |
+
- "target"
|
| 45 |
+
- "build"
|
| 46 |
+
- "dist"
|
| 47 |
+
- "venv"
|
| 48 |
+
- "env"
|
| 49 |
+
- ".git"
|
| 50 |
+
- ".svn"
|
| 51 |
+
- ".hg"
|
| 52 |
+
- "coverage"
|
| 53 |
+
- ".pytest_cache"
|
| 54 |
+
- ".mypy_cache"
|
| 55 |
+
|
| 56 |
+
# Maximum file size to analyze (in bytes)
|
| 57 |
+
max_file_size: 1048576 # 1MB
|
| 58 |
+
|
| 59 |
+
# Maximum content length to send to LLM (in characters)
|
| 60 |
+
max_content_length: 3000
|
| 61 |
+
|
| 62 |
+
# LLM Configuration
|
| 63 |
+
llm:
|
| 64 |
+
# Model selection: "anthropic" or "openai"
|
| 65 |
+
model_provider: "openai"
|
| 66 |
+
|
| 67 |
+
# Request parameters
|
| 68 |
+
max_tokens: 4000
|
| 69 |
+
temperature: 0.3
|
| 70 |
+
|
| 71 |
+
# System prompt for analysis
|
| 72 |
+
system_prompt: "You are a code analysis expert. Provide precise, structured analysis of code relationships and similarities."
|
| 73 |
+
|
| 74 |
+
# Rate limiting (seconds between requests)
|
| 75 |
+
request_delay: 0.1
|
| 76 |
+
|
| 77 |
+
# Retry configuration
|
| 78 |
+
max_retries: 3
|
| 79 |
+
retry_delay: 1.0
|
| 80 |
+
|
| 81 |
+
# Relationship Analysis Settings
|
| 82 |
+
relationships:
|
| 83 |
+
# Minimum confidence score to include a relationship
|
| 84 |
+
min_confidence_score: 0.3
|
| 85 |
+
|
| 86 |
+
# High confidence threshold for reporting
|
| 87 |
+
high_confidence_threshold: 0.7
|
| 88 |
+
|
| 89 |
+
# Relationship types and their priorities
|
| 90 |
+
relationship_types:
|
| 91 |
+
direct_match: 1.0 # Direct implementation match
|
| 92 |
+
partial_match: 0.8 # Partial functionality match
|
| 93 |
+
reference: 0.6 # Reference or utility function
|
| 94 |
+
utility: 0.4 # General utility or helper
|
| 95 |
+
|
| 96 |
+
# Output Configuration
|
| 97 |
+
output:
|
| 98 |
+
# JSON formatting options
|
| 99 |
+
json_indent: 2
|
| 100 |
+
ensure_ascii: false
|
| 101 |
+
|
| 102 |
+
# Generate additional report files
|
| 103 |
+
generate_summary: true
|
| 104 |
+
generate_statistics: true
|
| 105 |
+
|
| 106 |
+
# Include metadata in output
|
| 107 |
+
include_metadata: true
|
| 108 |
+
|
| 109 |
+
# File naming pattern (use {repo_name} placeholder)
|
| 110 |
+
index_filename_pattern: "{repo_name}_index.json"
|
| 111 |
+
summary_filename: "indexing_summary.json"
|
| 112 |
+
stats_filename: "indexing_statistics.json"
|
| 113 |
+
|
| 114 |
+
# Logging Configuration
|
| 115 |
+
logging:
|
| 116 |
+
level: "INFO" # DEBUG, INFO, WARNING, ERROR
|
| 117 |
+
log_to_file: true
|
| 118 |
+
log_file: "indexer.log"
|
| 119 |
+
log_format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
| 120 |
+
|
| 121 |
+
# Performance Settings
|
| 122 |
+
performance:
|
| 123 |
+
# Enable concurrent processing of files within a repository
|
| 124 |
+
enable_concurrent_analysis: true
|
| 125 |
+
max_concurrent_files: 5
|
| 126 |
+
|
| 127 |
+
# Memory optimization
|
| 128 |
+
enable_content_caching: false
|
| 129 |
+
max_cache_size: 100
|
| 130 |
+
|
| 131 |
+
# Debug and Development Settings
|
| 132 |
+
debug:
|
| 133 |
+
# Save raw LLM responses for debugging
|
| 134 |
+
save_raw_responses: false
|
| 135 |
+
raw_responses_dir: "debug_responses"
|
| 136 |
+
|
| 137 |
+
# Verbose output during processing
|
| 138 |
+
verbose_output: false
|
| 139 |
+
|
| 140 |
+
# Skip LLM calls for testing (uses mock responses)
|
| 141 |
+
mock_llm_responses: false
|