Skip to content

Commit fea8373

Browse files
committed
improve the code planning quality
1 parent 064c3b6 commit fea8373

File tree

3 files changed

+82
-44
lines changed

3 files changed

+82
-44
lines changed

cli/cli_app.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ async def cleanup_mcp_app(self):
4848
"""清理MCP应用 - 使用工作流适配器"""
4949
await self.workflow_adapter.cleanup_mcp_app()
5050

51-
5251
async def process_input(self, input_source: str, input_type: str):
5352
"""处理输入源(URL或文件)- 使用升级版智能体编排引擎"""
5453
try:

cli/cli_interface.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@ def __init__(self):
4040
self.is_running = True
4141
self.processing_history = []
4242
self.enable_indexing = True # Default configuration
43-
43+
4444
# Load segmentation config from the same source as UI
4545
self._load_segmentation_config()
46-
46+
4747
# Initialize tkinter availability
4848
self._init_tkinter()
4949

5050
def _load_segmentation_config(self):
5151
"""Load segmentation configuration from mcp_agent.config.yaml"""
5252
try:
5353
from utils.llm_utils import get_document_segmentation_config
54+
5455
seg_config = get_document_segmentation_config()
5556
self.segmentation_enabled = seg_config.get("enabled", True)
5657
self.segmentation_threshold = seg_config.get("size_threshold_chars", 50000)
@@ -64,7 +65,7 @@ def _save_segmentation_config(self):
6465
"""Save segmentation configuration to mcp_agent.config.yaml"""
6566
import yaml
6667
import os
67-
68+
6869
# Get the project root directory (where mcp_agent.config.yaml is located)
6970
current_file = os.path.abspath(__file__)
7071
cli_dir = os.path.dirname(current_file) # cli directory
@@ -81,16 +82,22 @@ def _save_segmentation_config(self):
8182
config["document_segmentation"] = {}
8283

8384
config["document_segmentation"]["enabled"] = self.segmentation_enabled
84-
config["document_segmentation"]["size_threshold_chars"] = self.segmentation_threshold
85+
config["document_segmentation"]["size_threshold_chars"] = (
86+
self.segmentation_threshold
87+
)
8588

8689
# Write updated config
8790
with open(config_path, "w", encoding="utf-8") as f:
8891
yaml.dump(config, f, default_flow_style=False, allow_unicode=True)
8992

90-
print(f"{Colors.OKGREEN}✅ Document segmentation configuration updated{Colors.ENDC}")
93+
print(
94+
f"{Colors.OKGREEN}✅ Document segmentation configuration updated{Colors.ENDC}"
95+
)
9196

9297
except Exception as e:
93-
print(f"{Colors.WARNING}⚠️ Failed to update segmentation config: {str(e)}{Colors.ENDC}")
98+
print(
99+
f"{Colors.WARNING}⚠️ Failed to update segmentation config: {str(e)}{Colors.ENDC}"
100+
)
94101

95102
def _init_tkinter(self):
96103
"""Initialize tkinter availability check"""

workflows/agent_orchestration_engine.py

Lines changed: 69 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -65,92 +65,116 @@
6565
def _assess_output_completeness(text: str) -> float:
6666
"""
6767
智能评估输出完整性的高级算法
68-
68+
6969
使用多种启发式方法来检测输出是否被截断:
7070
1. 结构化标记完整性检查
71-
2. 句子完整性分析
71+
2. 句子完整性分析
7272
3. 代码块完整性验证
7373
4. 预期内容元素检查
74-
74+
7575
Returns:
7676
float: 完整性分数 (0.0-1.0),越高表示越完整
7777
"""
7878
if not text or len(text.strip()) < 100:
7979
return 0.0
80-
80+
8181
score = 0.0
8282
factors = 0
83-
83+
8484
# 1. 基本长度检查 (权重: 0.2)
8585
if len(text) > 5000: # 期望的最小输出长度
8686
score += 0.2
8787
elif len(text) > 2000:
8888
score += 0.1
8989
factors += 1
90-
90+
9191
# 2. 结构完整性检查 (权重: 0.3)
9292
structure_indicators = [
93-
"## 1.", "## 2.", "## 3.", # 章节标题
94-
"```", "file_structure", "implementation",
95-
"algorithm", "method", "function"
93+
"## 1.",
94+
"## 2.",
95+
"## 3.", # 章节标题
96+
"```",
97+
"file_structure",
98+
"implementation",
99+
"algorithm",
100+
"method",
101+
"function",
96102
]
97-
structure_count = sum(1 for indicator in structure_indicators if indicator.lower() in text.lower())
103+
structure_count = sum(
104+
1 for indicator in structure_indicators if indicator.lower() in text.lower()
105+
)
98106
if structure_count >= 6:
99107
score += 0.3
100108
elif structure_count >= 3:
101109
score += 0.15
102110
factors += 1
103-
111+
104112
# 3. 句子完整性检查 (权重: 0.2)
105-
lines = text.strip().split('\n')
113+
lines = text.strip().split("\n")
106114
if lines:
107115
last_line = lines[-1].strip()
108116
# 检查最后一行是否是完整的句子或结构化内容
109-
if (last_line.endswith(('.', ':', '```', '!', '?')) or
110-
last_line.startswith(('##', '-', '*', '`')) or
111-
len(last_line) < 10): # 很短的行可能是列表项
117+
if (
118+
last_line.endswith((".", ":", "```", "!", "?"))
119+
or last_line.startswith(("##", "-", "*", "`"))
120+
or len(last_line) < 10
121+
): # 很短的行可能是列表项
112122
score += 0.2
113-
elif len(last_line) > 50 and not last_line.endswith(('.', ':', '```', '!', '?')):
123+
elif len(last_line) > 50 and not last_line.endswith(
124+
(".", ":", "```", "!", "?")
125+
):
114126
# 长行但没有适当结尾,可能被截断
115127
score += 0.05
116128
factors += 1
117-
129+
118130
# 4. 代码实现计划完整性 (权重: 0.3)
119131
implementation_keywords = [
120-
"file structure", "architecture", "implementation",
121-
"requirements", "dependencies", "setup", "main",
122-
"class", "function", "method", "algorithm"
132+
"file structure",
133+
"architecture",
134+
"implementation",
135+
"requirements",
136+
"dependencies",
137+
"setup",
138+
"main",
139+
"class",
140+
"function",
141+
"method",
142+
"algorithm",
123143
]
124-
impl_count = sum(1 for keyword in implementation_keywords if keyword.lower() in text.lower())
144+
impl_count = sum(
145+
1 for keyword in implementation_keywords if keyword.lower() in text.lower()
146+
)
125147
if impl_count >= 8:
126148
score += 0.3
127149
elif impl_count >= 4:
128150
score += 0.15
129151
factors += 1
130-
152+
131153
return min(score, 1.0) # 确保不超过1.0
132154

133155

134156
def _adjust_params_for_retry(params: RequestParams, retry_count: int) -> RequestParams:
135157
"""
136158
动态调整请求参数以提高成功率
137-
159+
138160
基于重试次数智能调整参数:
139161
- 增加token限制
140162
- 调整temperature
141163
- 优化其他参数
142164
"""
143165
# 基础token增量:每次重试增加更多tokens
144166
token_increment = 4096 * (retry_count + 1)
145-
new_max_tokens = min(params.max_tokens + token_increment, 32768) # 不超过32K的合理限制
146-
167+
new_max_tokens = min(
168+
params.max_tokens + token_increment, 32768
169+
) # 不超过32K的合理限制
170+
147171
# 随着重试次数增加,降低temperature以获得更一致的输出
148172
new_temperature = max(params.temperature - (retry_count * 0.1), 0.1)
149-
173+
150174
print(f"🔧 Adjusting parameters for retry {retry_count + 1}:")
151175
print(f" Token limit: {params.max_tokens}{new_max_tokens}")
152176
print(f" Temperature: {params.temperature}{new_temperature}")
153-
177+
154178
return RequestParams(
155179
max_tokens=new_max_tokens,
156180
temperature=new_temperature,
@@ -483,13 +507,15 @@ async def run_code_analyzer(
483507
# 分段模式:可以使用更高的token限制,因为输入已经被优化
484508
max_tokens_limit = 16384 # 使用更高限制,因为分段减少了输入复杂性
485509
temperature = 0.2 # 稍微降低temperature以提高一致性
486-
print("🧠 Using SEGMENTED mode: Higher token limit (16384) with optimized inputs")
510+
print(
511+
"🧠 Using SEGMENTED mode: Higher token limit (16384) with optimized inputs"
512+
)
487513
else:
488514
# 传统模式:使用保守的token限制并启用增量生成
489515
max_tokens_limit = 12288 # 中等限制,为聚合输出留出空间
490516
temperature = 0.3
491517
print("🧠 Using TRADITIONAL mode: Moderate token limit (12288)")
492-
518+
493519
enhanced_params = RequestParams(
494520
max_tokens=max_tokens_limit,
495521
temperature=temperature,
@@ -509,33 +535,39 @@ async def run_code_analyzer(
509535
# 智能输出完整性检查和重试机制
510536
max_retries = 3
511537
retry_count = 0
512-
538+
513539
while retry_count < max_retries:
514540
try:
515-
print(f"🚀 Attempting code analysis (attempt {retry_count + 1}/{max_retries})")
541+
print(
542+
f"🚀 Attempting code analysis (attempt {retry_count + 1}/{max_retries})"
543+
)
516544
result = await code_aggregator_agent.generate_str(
517545
message=message, request_params=enhanced_params
518546
)
519-
547+
520548
# 检查输出完整性的高级指标
521549
completeness_score = _assess_output_completeness(result)
522550
print(f"📊 Output completeness score: {completeness_score:.2f}/1.0")
523-
551+
524552
if completeness_score >= 0.8: # 输出被认为是完整的
525-
print(f"✅ Code analysis completed successfully (length: {len(result)} chars)")
553+
print(
554+
f"✅ Code analysis completed successfully (length: {len(result)} chars)"
555+
)
526556
return result
527557
else:
528-
print(f"⚠️ Output appears truncated (score: {completeness_score:.2f}), retrying with enhanced parameters...")
558+
print(
559+
f"⚠️ Output appears truncated (score: {completeness_score:.2f}), retrying with enhanced parameters..."
560+
)
529561
# 动态调整参数进行重试
530562
enhanced_params = _adjust_params_for_retry(enhanced_params, retry_count)
531563
retry_count += 1
532-
564+
533565
except Exception as e:
534566
print(f"❌ Error in code analysis attempt {retry_count + 1}: {e}")
535567
retry_count += 1
536568
if retry_count >= max_retries:
537569
raise
538-
570+
539571
# 如果所有重试都失败,返回最后一次的结果
540572
print(f"⚠️ Returning potentially incomplete result after {max_retries} attempts")
541573
return result

0 commit comments

Comments
 (0)