auto-coder 0.1.361__py3-none-any.whl → 0.1.363__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of auto-coder might be problematic. Click here for more details.
- {auto_coder-0.1.361.dist-info → auto_coder-0.1.363.dist-info}/METADATA +2 -1
- {auto_coder-0.1.361.dist-info → auto_coder-0.1.363.dist-info}/RECORD +57 -29
- autocoder/agent/auto_learn.py +249 -262
- autocoder/agent/base_agentic/__init__.py +0 -0
- autocoder/agent/base_agentic/agent_hub.py +169 -0
- autocoder/agent/base_agentic/agentic_lang.py +112 -0
- autocoder/agent/base_agentic/agentic_tool_display.py +180 -0
- autocoder/agent/base_agentic/base_agent.py +1582 -0
- autocoder/agent/base_agentic/default_tools.py +683 -0
- autocoder/agent/base_agentic/test_base_agent.py +82 -0
- autocoder/agent/base_agentic/tool_registry.py +425 -0
- autocoder/agent/base_agentic/tools/__init__.py +12 -0
- autocoder/agent/base_agentic/tools/ask_followup_question_tool_resolver.py +72 -0
- autocoder/agent/base_agentic/tools/attempt_completion_tool_resolver.py +37 -0
- autocoder/agent/base_agentic/tools/base_tool_resolver.py +35 -0
- autocoder/agent/base_agentic/tools/example_tool_resolver.py +46 -0
- autocoder/agent/base_agentic/tools/execute_command_tool_resolver.py +72 -0
- autocoder/agent/base_agentic/tools/list_files_tool_resolver.py +110 -0
- autocoder/agent/base_agentic/tools/plan_mode_respond_tool_resolver.py +35 -0
- autocoder/agent/base_agentic/tools/read_file_tool_resolver.py +54 -0
- autocoder/agent/base_agentic/tools/replace_in_file_tool_resolver.py +156 -0
- autocoder/agent/base_agentic/tools/search_files_tool_resolver.py +134 -0
- autocoder/agent/base_agentic/tools/talk_to_group_tool_resolver.py +96 -0
- autocoder/agent/base_agentic/tools/talk_to_tool_resolver.py +79 -0
- autocoder/agent/base_agentic/tools/use_mcp_tool_resolver.py +44 -0
- autocoder/agent/base_agentic/tools/write_to_file_tool_resolver.py +58 -0
- autocoder/agent/base_agentic/types.py +189 -0
- autocoder/agent/base_agentic/utils.py +100 -0
- autocoder/auto_coder.py +1 -1
- autocoder/auto_coder_runner.py +36 -14
- autocoder/chat/conf_command.py +11 -10
- autocoder/commands/auto_command.py +227 -159
- autocoder/common/__init__.py +2 -2
- autocoder/common/ignorefiles/ignore_file_utils.py +12 -8
- autocoder/common/result_manager.py +10 -2
- autocoder/common/rulefiles/autocoderrules_utils.py +169 -0
- autocoder/common/save_formatted_log.py +1 -1
- autocoder/common/v2/agent/agentic_edit.py +53 -41
- autocoder/common/v2/agent/agentic_edit_tools/read_file_tool_resolver.py +15 -12
- autocoder/common/v2/agent/agentic_edit_tools/replace_in_file_tool_resolver.py +73 -1
- autocoder/common/v2/agent/agentic_edit_tools/write_to_file_tool_resolver.py +132 -4
- autocoder/common/v2/agent/agentic_edit_types.py +1 -2
- autocoder/common/v2/agent/agentic_tool_display.py +2 -3
- autocoder/common/v2/code_auto_generate_editblock.py +3 -1
- autocoder/index/index.py +14 -8
- autocoder/privacy/model_filter.py +297 -35
- autocoder/rag/long_context_rag.py +424 -397
- autocoder/rag/test_doc_filter.py +393 -0
- autocoder/rag/test_long_context_rag.py +473 -0
- autocoder/rag/test_token_limiter.py +342 -0
- autocoder/shadows/shadow_manager.py +1 -3
- autocoder/utils/_markitdown.py +22 -3
- autocoder/version.py +1 -1
- {auto_coder-0.1.361.dist-info → auto_coder-0.1.363.dist-info}/LICENSE +0 -0
- {auto_coder-0.1.361.dist-info → auto_coder-0.1.363.dist-info}/WHEEL +0 -0
- {auto_coder-0.1.361.dist-info → auto_coder-0.1.363.dist-info}/entry_points.txt +0 -0
- {auto_coder-0.1.361.dist-info → auto_coder-0.1.363.dist-info}/top_level.txt +0 -0
autocoder/common/__init__.py
CHANGED
|
@@ -272,13 +272,13 @@ class AutoCoderArgs(pydantic.BaseModel):
|
|
|
272
272
|
index_model_anti_quota_limit: Optional[int] = 0
|
|
273
273
|
|
|
274
274
|
enable_agentic_filter: Optional[bool] = False
|
|
275
|
-
enable_agentic_edit: Optional[bool] =
|
|
275
|
+
enable_agentic_edit: Optional[bool] = True
|
|
276
276
|
|
|
277
277
|
|
|
278
278
|
index_filter_level: Optional[int] = 0
|
|
279
279
|
index_filter_enable_relevance_verification: Optional[bool] = True
|
|
280
280
|
index_filter_workers: Optional[int] = 1
|
|
281
|
-
index_filter_file_num: Optional[int] =
|
|
281
|
+
index_filter_file_num: Optional[int] = 10
|
|
282
282
|
index_build_workers: Optional[int] = 1
|
|
283
283
|
|
|
284
284
|
planner_model: Optional[str] = ""
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
2
1
|
import os
|
|
3
2
|
from pathlib import Path
|
|
4
3
|
from threading import Lock
|
|
5
4
|
import pathspec
|
|
6
5
|
import threading
|
|
6
|
+
from typing import Optional # 添加Optional导入
|
|
7
7
|
|
|
8
8
|
# 尝试导入 FileMonitor
|
|
9
9
|
try:
|
|
@@ -24,7 +24,7 @@ class IgnoreFileManager:
|
|
|
24
24
|
_instance = None
|
|
25
25
|
_lock = Lock()
|
|
26
26
|
|
|
27
|
-
def __new__(cls):
|
|
27
|
+
def __new__(cls, project_root: Optional[str] = None):
|
|
28
28
|
if not cls._instance:
|
|
29
29
|
with cls._lock:
|
|
30
30
|
if not cls._instance:
|
|
@@ -32,20 +32,21 @@ class IgnoreFileManager:
|
|
|
32
32
|
cls._instance._initialized = False
|
|
33
33
|
return cls._instance
|
|
34
34
|
|
|
35
|
-
def __init__(self):
|
|
35
|
+
def __init__(self, project_root: Optional[str] = None):
|
|
36
36
|
if self._initialized:
|
|
37
37
|
return
|
|
38
38
|
self._initialized = True
|
|
39
39
|
self._spec = None
|
|
40
40
|
self._ignore_file_path = None
|
|
41
41
|
self._file_monitor = None
|
|
42
|
+
self._project_root = project_root if project_root is not None else os.getcwd()
|
|
42
43
|
self._load_ignore_spec()
|
|
43
44
|
self._setup_file_monitor()
|
|
44
45
|
|
|
45
46
|
def _load_ignore_spec(self):
|
|
46
47
|
"""加载忽略规则文件并解析规则"""
|
|
47
48
|
ignore_patterns = []
|
|
48
|
-
project_root = Path(
|
|
49
|
+
project_root = Path(self._project_root)
|
|
49
50
|
|
|
50
51
|
ignore_file_paths = [
|
|
51
52
|
project_root / '.autocoderignore',
|
|
@@ -89,15 +90,18 @@ class IgnoreFileManager:
|
|
|
89
90
|
|
|
90
91
|
def should_ignore(self, path: str) -> bool:
|
|
91
92
|
"""判断指定路径是否应该被忽略"""
|
|
92
|
-
rel_path = os.path.relpath(path,
|
|
93
|
+
rel_path = os.path.relpath(path, self._project_root)
|
|
93
94
|
# 标准化分隔符
|
|
94
95
|
rel_path = rel_path.replace(os.sep, '/')
|
|
95
96
|
return self._spec.match_file(rel_path)
|
|
96
97
|
|
|
97
98
|
|
|
98
|
-
#
|
|
99
|
-
_ignore_manager =
|
|
99
|
+
# 对外提供的单例管理器
|
|
100
|
+
_ignore_manager = None
|
|
100
101
|
|
|
101
|
-
def should_ignore(path: str) -> bool:
|
|
102
|
+
def should_ignore(path: str, project_root: Optional[str] = None) -> bool:
|
|
102
103
|
"""判断指定路径是否应该被忽略"""
|
|
104
|
+
global _ignore_manager
|
|
105
|
+
if _ignore_manager is None:
|
|
106
|
+
_ignore_manager = IgnoreFileManager(project_root=project_root)
|
|
103
107
|
return _ignore_manager.should_ignore(path)
|
|
@@ -16,16 +16,24 @@ class ResultItem(BaseModel):
|
|
|
16
16
|
class ResultManager:
|
|
17
17
|
"""结果管理器,用于维护一个追加写入的jsonl文件"""
|
|
18
18
|
|
|
19
|
-
def __init__(self, source_dir: Optional[str] = None):
|
|
19
|
+
def __init__(self, source_dir: Optional[str] = None, event_file: Optional[str] = None):
|
|
20
20
|
"""
|
|
21
21
|
初始化结果管理器
|
|
22
22
|
|
|
23
23
|
Args:
|
|
24
24
|
source_dir: 可选的源目录,如果不提供则使用当前目录
|
|
25
|
+
event_file: 可选的事件文件路径,用于生成结果文件名
|
|
25
26
|
"""
|
|
26
27
|
self.source_dir = source_dir or os.getcwd()
|
|
27
28
|
self.result_dir = os.path.join(self.source_dir, ".auto-coder", "results")
|
|
28
|
-
|
|
29
|
+
|
|
30
|
+
if event_file:
|
|
31
|
+
# 获取文件名并去掉后缀
|
|
32
|
+
event_file_name = os.path.splitext(os.path.basename(event_file))[0]
|
|
33
|
+
self.result_file = os.path.join(self.result_dir, f"{event_file_name}.jsonl")
|
|
34
|
+
else:
|
|
35
|
+
self.result_file = os.path.join(self.result_dir, "results.jsonl")
|
|
36
|
+
|
|
29
37
|
os.makedirs(self.result_dir, exist_ok=True)
|
|
30
38
|
|
|
31
39
|
def append(self, content: str, meta: Optional[Dict[str, Any]] = None) -> ResultItem:
|
|
@@ -6,7 +6,10 @@ from typing import Dict, List, Optional
|
|
|
6
6
|
from loguru import logger
|
|
7
7
|
import re
|
|
8
8
|
import yaml
|
|
9
|
+
import byzerllm # Added import
|
|
9
10
|
from pydantic import BaseModel, Field
|
|
11
|
+
from typing import List, Dict, Optional, Any # Added Any
|
|
12
|
+
from autocoder.common import AutoCoderArgs
|
|
10
13
|
|
|
11
14
|
# 尝试导入 FileMonitor
|
|
12
15
|
try:
|
|
@@ -230,6 +233,25 @@ class AutocoderRulesManager:
|
|
|
230
233
|
parsed_rules.append(parsed_rule)
|
|
231
234
|
return parsed_rules
|
|
232
235
|
|
|
236
|
+
@classmethod
|
|
237
|
+
def reset_instance(cls):
|
|
238
|
+
"""
|
|
239
|
+
重置单例实例。
|
|
240
|
+
如果当前实例正在运行,则先取消注册监控的目录,然后重置实例。
|
|
241
|
+
"""
|
|
242
|
+
with cls._lock:
|
|
243
|
+
if cls._instance is not None:
|
|
244
|
+
# 取消注册监控的目录
|
|
245
|
+
if cls._instance._file_monitor:
|
|
246
|
+
for dir_path in cls._instance._monitored_dirs:
|
|
247
|
+
try:
|
|
248
|
+
cls._instance._file_monitor.unregister(dir_path)
|
|
249
|
+
logger.info(f"已取消注册目录监控: {dir_path}")
|
|
250
|
+
except Exception as e:
|
|
251
|
+
logger.warning(f"取消注册目录 {dir_path} 时出错: {e}")
|
|
252
|
+
cls._instance = None
|
|
253
|
+
logger.info("AutocoderRulesManager单例已被重置")
|
|
254
|
+
|
|
233
255
|
|
|
234
256
|
# 对外提供单例
|
|
235
257
|
_rules_manager = None
|
|
@@ -254,3 +276,150 @@ def parse_rule_file(file_path: str, project_root: Optional[str] = None) -> RuleF
|
|
|
254
276
|
if _rules_manager is None:
|
|
255
277
|
_rules_manager = AutocoderRulesManager(project_root=project_root)
|
|
256
278
|
return _rules_manager.parse_rule_file(file_path)
|
|
279
|
+
|
|
280
|
+
def reset_rules_manager():
|
|
281
|
+
"""重置AutocoderRulesManager单例实例"""
|
|
282
|
+
AutocoderRulesManager.reset_instance()
|
|
283
|
+
global _rules_manager
|
|
284
|
+
_rules_manager = None
|
|
285
|
+
|
|
286
|
+
# 添加用于返回类型的Pydantic模型
|
|
287
|
+
class RuleRelevance(BaseModel):
|
|
288
|
+
"""用于规则相关性判断的返回模型"""
|
|
289
|
+
is_relevant: bool = Field(description="规则是否与当前任务相关")
|
|
290
|
+
reason: str = Field(default="", description="判断理由")
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
class RuleSelector:
|
|
294
|
+
"""
|
|
295
|
+
根据LLM的判断和规则元数据选择适用的规则。
|
|
296
|
+
"""
|
|
297
|
+
def __init__(self, llm: Optional[byzerllm.ByzerLLM], args: Optional[AutoCoderArgs] = None):
|
|
298
|
+
"""
|
|
299
|
+
初始化RuleSelector。
|
|
300
|
+
|
|
301
|
+
Args:
|
|
302
|
+
llm: ByzerLLM 实例,用于判断规则是否适用。如果为 None,则只选择 always_apply=True 的规则。
|
|
303
|
+
args: 传递给 Agent 的参数,可能包含用于规则选择的上下文信息。
|
|
304
|
+
"""
|
|
305
|
+
self.llm = llm
|
|
306
|
+
self.args = args
|
|
307
|
+
|
|
308
|
+
@byzerllm.prompt()
|
|
309
|
+
def _build_selection_prompt(self, rule: RuleFile, context: str = "") -> str:
|
|
310
|
+
"""
|
|
311
|
+
判断规则是否适用于当前任务。
|
|
312
|
+
|
|
313
|
+
规则描述:
|
|
314
|
+
{{ rule.description }}
|
|
315
|
+
|
|
316
|
+
规则内容摘要 (前200字符):
|
|
317
|
+
{{ rule.content[:200] }}
|
|
318
|
+
|
|
319
|
+
{% if context %}
|
|
320
|
+
任务上下文:
|
|
321
|
+
{{ context }}
|
|
322
|
+
{% endif %}
|
|
323
|
+
|
|
324
|
+
基于以上信息,判断这条规则 (路径: {{ rule.file_path }}) 是否与当前任务相关并应该被应用?
|
|
325
|
+
|
|
326
|
+
请以JSON格式返回结果:
|
|
327
|
+
```json
|
|
328
|
+
{
|
|
329
|
+
"is_relevant": true或false,
|
|
330
|
+
"reason": "判断理由"
|
|
331
|
+
}
|
|
332
|
+
```
|
|
333
|
+
"""
|
|
334
|
+
# 注意:确保 rule 对象和 context 字典能够被 Jinja2 正确访问。
|
|
335
|
+
# Pydantic模型可以直接在Jinja2中使用其属性。
|
|
336
|
+
return {
|
|
337
|
+
"rule": rule,
|
|
338
|
+
"context": context
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
def select_rules(self, context: str, rules: List[RuleFile]) -> List[RuleFile]:
|
|
342
|
+
"""
|
|
343
|
+
选择适用于当前上下文的规则。
|
|
344
|
+
|
|
345
|
+
Args:
|
|
346
|
+
context: 可选的字典,包含用于规则选择的上下文信息 (例如,用户指令、目标文件等)。
|
|
347
|
+
|
|
348
|
+
Returns:
|
|
349
|
+
List[RuleFile]: 选定的规则列表。
|
|
350
|
+
"""
|
|
351
|
+
selected_rules: List[RuleFile] = []
|
|
352
|
+
logger.info(f"开始选择规则,总规则数: {len(rules)}")
|
|
353
|
+
|
|
354
|
+
for rule in rules:
|
|
355
|
+
if rule.always_apply:
|
|
356
|
+
selected_rules.append(rule)
|
|
357
|
+
logger.debug(f"规则 '{os.path.basename(rule.file_path)}' (AlwaysApply=True) 已自动选择。")
|
|
358
|
+
continue
|
|
359
|
+
|
|
360
|
+
if self.llm is None:
|
|
361
|
+
logger.debug(f"规则 '{os.path.basename(rule.file_path)}' (AlwaysApply=False) 已跳过,因为未提供 LLM。")
|
|
362
|
+
continue
|
|
363
|
+
|
|
364
|
+
# 对于 alwaysApply=False 的规则,使用 LLM 判断
|
|
365
|
+
try:
|
|
366
|
+
prompt = self._build_selection_prompt.prompt(rule=rule, context=context)
|
|
367
|
+
logger.debug(f"为规则 '{os.path.basename(rule.file_path)}' 生成的判断 Prompt (片段): {prompt[:200]}...")
|
|
368
|
+
|
|
369
|
+
# **** 实际LLM调用 ****
|
|
370
|
+
# 确保 self.llm 实例已正确初始化并可用
|
|
371
|
+
if self.llm: # Check if llm is not None
|
|
372
|
+
result = None
|
|
373
|
+
try:
|
|
374
|
+
# 使用with_return_type方法获取结构化结果
|
|
375
|
+
result = self._build_selection_prompt.with_llm(self.llm).with_return_type(RuleRelevance).run(rule=rule, context=context)
|
|
376
|
+
if result and result.is_relevant:
|
|
377
|
+
selected_rules.append(rule)
|
|
378
|
+
logger.info(f"规则 '{os.path.basename(rule.file_path)}' (AlwaysApply=False) 已被 LLM 选择,原因: {result.reason}")
|
|
379
|
+
else:
|
|
380
|
+
logger.debug(f"规则 '{os.path.basename(rule.file_path)}' (AlwaysApply=False) 未被 LLM 选择,原因: {result.reason if result else '未提供'}")
|
|
381
|
+
except Exception as e:
|
|
382
|
+
logger.warning(f"LLM 未能为规则 '{os.path.basename(rule.file_path)}' 提供有效响应。")
|
|
383
|
+
# 根据需要决定是否跳过或默认不选
|
|
384
|
+
continue # 跳过此规则
|
|
385
|
+
else: # Handle case where self.llm is None after the initial check
|
|
386
|
+
logger.warning(f"LLM instance became None unexpectedly for rule '{os.path.basename(rule.file_path)}'.")
|
|
387
|
+
continue
|
|
388
|
+
|
|
389
|
+
# **** 模拟LLM调用 (用于测试/开发) ****
|
|
390
|
+
# 注释掉模拟部分,使用上面的实际调用
|
|
391
|
+
# simulated_response = "yes" if "always" in rule.description.lower() or "index" in rule.description.lower() else "no"
|
|
392
|
+
# logger.warning(f"模拟LLM判断规则 '{os.path.basename(rule.file_path)}': {simulated_response}")
|
|
393
|
+
# response_text = simulated_response
|
|
394
|
+
# **** 结束模拟 ****
|
|
395
|
+
|
|
396
|
+
except Exception as e:
|
|
397
|
+
logger.error(f"使用 LLM 判断规则 '{os.path.basename(rule.file_path)}' 时出错: {e}", exc_info=True)
|
|
398
|
+
# 根据策略决定是否包含出错的规则,这里选择跳过
|
|
399
|
+
continue
|
|
400
|
+
|
|
401
|
+
logger.info(f"规则选择完成,选中规则数: {len(selected_rules)}")
|
|
402
|
+
return selected_rules
|
|
403
|
+
|
|
404
|
+
def get_selected_rules_content(self, context: Optional[Dict] = None) -> Dict[str, str]:
|
|
405
|
+
"""
|
|
406
|
+
获取选定规则的文件路径和内容字典。
|
|
407
|
+
|
|
408
|
+
Args:
|
|
409
|
+
context: 传递给 select_rules 的上下文。
|
|
410
|
+
|
|
411
|
+
Returns:
|
|
412
|
+
Dict[str, str]: 选定规则的 {file_path: content} 字典。
|
|
413
|
+
"""
|
|
414
|
+
selected_rules = self.select_rules(context=context)
|
|
415
|
+
# 使用 os.path.basename 获取文件名作为 key,如果需要的话
|
|
416
|
+
# return {os.path.basename(rule.file_path): rule.content for rule in selected_rules}
|
|
417
|
+
# 保持 file_path 作为 key
|
|
418
|
+
return {rule.file_path: rule.content for rule in selected_rules}
|
|
419
|
+
|
|
420
|
+
def auto_select_rules(context: str, rules: List[RuleFile], llm: Optional[byzerllm.ByzerLLM] = None,args:Optional[AutoCoderArgs] = None) -> List[RuleFile]:
|
|
421
|
+
"""
|
|
422
|
+
根据LLM的判断和规则元数据选择适用的规则。
|
|
423
|
+
"""
|
|
424
|
+
selector = RuleSelector(llm=llm, args=args)
|
|
425
|
+
return selector.select_rules(context=context, rules=rules)
|
|
@@ -38,7 +38,7 @@ def save_formatted_log(project_root, json_text, suffix):
|
|
|
38
38
|
md_content = "\n".join(md_lines)
|
|
39
39
|
|
|
40
40
|
# Prepare directory
|
|
41
|
-
logs_dir = os.path.join(project_root, ".
|
|
41
|
+
logs_dir = os.path.join(project_root, ".auto-coder", "logs","agentic")
|
|
42
42
|
os.makedirs(logs_dir, exist_ok=True)
|
|
43
43
|
|
|
44
44
|
# Prepare filename
|
|
@@ -10,7 +10,6 @@ from rich.panel import Panel
|
|
|
10
10
|
from pydantic import SkipValidation
|
|
11
11
|
from byzerllm.utils.types import SingleOutputMeta
|
|
12
12
|
|
|
13
|
-
# Removed ResultManager, stream_out, git_utils, AutoCommandTools, count_tokens, global_cancel, ActionYmlFileManager, get_event_manager, EventContentCreator, get_run_context, AgenticFilterStreamOutType
|
|
14
13
|
from autocoder.common import AutoCoderArgs, git_utils, SourceCodeList, SourceCode
|
|
15
14
|
from autocoder.common.global_cancel import global_cancel
|
|
16
15
|
from autocoder.common import detect_env
|
|
@@ -73,7 +72,6 @@ from autocoder.common.v2.agent.agentic_edit_types import (AgenticEditRequest, To
|
|
|
73
72
|
AskFollowupQuestionTool, UseMcpTool, AttemptCompletionTool
|
|
74
73
|
)
|
|
75
74
|
|
|
76
|
-
|
|
77
75
|
# Map Pydantic Tool Models to their Resolver Classes
|
|
78
76
|
TOOL_RESOLVER_MAP: Dict[Type[BaseTool], Type[BaseToolResolver]] = {
|
|
79
77
|
ExecuteCommandTool: ExecuteCommandToolResolver,
|
|
@@ -87,7 +85,7 @@ TOOL_RESOLVER_MAP: Dict[Type[BaseTool], Type[BaseToolResolver]] = {
|
|
|
87
85
|
AskFollowupQuestionTool: AskFollowupQuestionToolResolver,
|
|
88
86
|
AttemptCompletionTool: AttemptCompletionToolResolver, # Will stop the loop anyway
|
|
89
87
|
PlanModeRespondTool: PlanModeRespondToolResolver,
|
|
90
|
-
UseMcpTool: UseMcpToolResolver
|
|
88
|
+
UseMcpTool: UseMcpToolResolver
|
|
91
89
|
}
|
|
92
90
|
|
|
93
91
|
|
|
@@ -234,7 +232,7 @@ class AgenticEdit:
|
|
|
234
232
|
# Tools
|
|
235
233
|
|
|
236
234
|
## execute_command
|
|
237
|
-
Description: Request to execute a CLI command on the system. Use this when you need to perform system operations or run specific commands to accomplish any step in the user's task. You must tailor your command to the user's system and provide a clear explanation of what the command does. For command chaining, use the appropriate chaining syntax for the user's shell. Prefer to execute complex CLI commands over creating executable scripts, as they are more flexible and easier to run. Commands will be executed in the current working directory:
|
|
235
|
+
Description: Request to execute a CLI command on the system. Use this when you need to perform system operations or run specific commands to accomplish any step in the user's task. You must tailor your command to the user's system and provide a clear explanation of what the command does. For command chaining, use the appropriate chaining syntax for the user's shell. Prefer to execute complex CLI commands over creating executable scripts, as they are more flexible and easier to run. Commands will be executed in the current working directory: {{current_project}}
|
|
238
236
|
Parameters:
|
|
239
237
|
- command: (required) The CLI command to execute. This should be valid for the current operating system. Ensure the command is properly formatted and does not contain any harmful instructions.
|
|
240
238
|
- requires_approval: (required) A boolean indicating whether this command requires explicit user approval before execution in case the user has auto-approve mode enabled. Set to 'true' for potentially impactful operations like installing/uninstalling packages, deleting/overwriting files, system configuration changes, network operations, or any commands that could have unintended side effects. Set to 'false' for safe operations like reading files/directories, running development servers, building projects, and other non-destructive operations.
|
|
@@ -256,7 +254,7 @@ class AgenticEdit:
|
|
|
256
254
|
## read_file
|
|
257
255
|
Description: Request to read the contents of a file at the specified path. Use this when you need to examine the contents of an existing file you do not know the contents of, for example to analyze code, review text files, or extract information from configuration files. Automatically extracts raw text from PDF and DOCX files. May not be suitable for other types of binary files, as it returns the raw content as a string.
|
|
258
256
|
Parameters:
|
|
259
|
-
- path: (required) The path of the file to read (relative to the current working directory
|
|
257
|
+
- path: (required) The path of the file to read (relative to the current working directory {{ current_project }})
|
|
260
258
|
Usage:
|
|
261
259
|
<read_file>
|
|
262
260
|
<path>File path here</path>
|
|
@@ -265,7 +263,7 @@ class AgenticEdit:
|
|
|
265
263
|
## write_to_file
|
|
266
264
|
Description: Request to write content to a file at the specified path. If the file exists, it will be overwritten with the provided content. If the file doesn't exist, it will be created. This tool will automatically create any directories needed to write the file.
|
|
267
265
|
Parameters:
|
|
268
|
-
- path: (required) The path of the file to write to (relative to the current working directory
|
|
266
|
+
- path: (required) The path of the file to write to (relative to the current working directory {{ current_project }})
|
|
269
267
|
- content: (required) The content to write to the file. ALWAYS provide the COMPLETE intended content of the file, without any truncation or omissions. You MUST include ALL parts of the file, even if they haven't been modified.
|
|
270
268
|
Usage:
|
|
271
269
|
<write_to_file>
|
|
@@ -278,15 +276,15 @@ class AgenticEdit:
|
|
|
278
276
|
## replace_in_file
|
|
279
277
|
Description: Request to replace sections of content in an existing file using SEARCH/REPLACE blocks that define exact changes to specific parts of the file. This tool should be used when you need to make targeted changes to specific parts of a file.
|
|
280
278
|
Parameters:
|
|
281
|
-
- path: (required) The path of the file to modify (relative to the current working directory
|
|
279
|
+
- path: (required) The path of the file to modify (relative to the current working directory {{ current_project }})
|
|
282
280
|
- diff: (required) One or more SEARCH/REPLACE blocks following this exact format:
|
|
283
|
-
|
|
281
|
+
```
|
|
284
282
|
<<<<<<< SEARCH
|
|
285
283
|
[exact content to find]
|
|
286
284
|
=======
|
|
287
285
|
[new content to replace with]
|
|
288
286
|
>>>>>>> REPLACE
|
|
289
|
-
|
|
287
|
+
```
|
|
290
288
|
Critical rules:
|
|
291
289
|
1. SEARCH content must match the associated file section to find EXACTLY:
|
|
292
290
|
* Match character-for-character including whitespace, indentation, line endings
|
|
@@ -314,7 +312,7 @@ class AgenticEdit:
|
|
|
314
312
|
## search_files
|
|
315
313
|
Description: Request to perform a regex search across files in a specified directory, providing context-rich results. This tool searches for patterns or specific content across multiple files, displaying each match with encapsulating context.
|
|
316
314
|
Parameters:
|
|
317
|
-
- path: (required) The path of the directory to search in (relative to the current working directory
|
|
315
|
+
- path: (required) The path of the directory to search in (relative to the current working directory {{ current_project }}). This directory will be recursively searched.
|
|
318
316
|
- regex: (required) The regular expression pattern to search for. Uses Rust regex syntax.
|
|
319
317
|
- file_pattern: (optional) Glob pattern to filter files (e.g., '*.ts' for TypeScript files). If not provided, it will search all files (*).
|
|
320
318
|
Usage:
|
|
@@ -327,7 +325,7 @@ class AgenticEdit:
|
|
|
327
325
|
## list_files
|
|
328
326
|
Description: Request to list files and directories within the specified directory. If recursive is true, it will list all files and directories recursively. If recursive is false or not provided, it will only list the top-level contents. Do not use this tool to confirm the existence of files you may have created, as the user will let you know if the files were created successfully or not.
|
|
329
327
|
Parameters:
|
|
330
|
-
- path: (required) The path of the directory to list contents for (relative to the current working directory
|
|
328
|
+
- path: (required) The path of the directory to list contents for (relative to the current working directory {{ current_project }})
|
|
331
329
|
- recursive: (optional) Whether to list files recursively. Use true for recursive listing, false or omit for top-level only.
|
|
332
330
|
Usage:
|
|
333
331
|
<list_files>
|
|
@@ -338,7 +336,7 @@ class AgenticEdit:
|
|
|
338
336
|
## list_code_definition_names
|
|
339
337
|
Description: Request to list definition names (classes, functions, methods, etc.) used in source code files at the top level of the specified directory. This tool provides insights into the codebase structure and important constructs, encapsulating high-level concepts and relationships that are crucial for understanding the overall architecture.
|
|
340
338
|
Parameters:
|
|
341
|
-
- path: (required) The path of the directory (relative to the current working directory
|
|
339
|
+
- path: (required) The path of the directory (relative to the current working directory {{ current_project }}) to list top level source code definitions for.
|
|
342
340
|
Usage:
|
|
343
341
|
<list_code_definition_names>
|
|
344
342
|
<path>Directory path here</path>
|
|
@@ -637,7 +635,7 @@ class AgenticEdit:
|
|
|
637
635
|
- Your current working directory is: {{current_project}}
|
|
638
636
|
- You cannot \`cd\` into a different directory to complete a task. You are stuck operating from '{{ current_project }}', so be sure to pass in the correct 'path' parameter when using tools that require a path.
|
|
639
637
|
- Do not use the ~ character or $HOME to refer to the home directory.
|
|
640
|
-
- Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '
|
|
638
|
+
- Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '{{ current_project }}', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '{{ current_project }}'). For example, if you needed to run \`npm install\` in a project outside of '{{ current_project }}', you would need to prepend with a \`cd\` i.e. pseudocode for this would be \`cd (path to project) && (command, in this case npm install)\`.
|
|
641
639
|
- When using the search_files tool, craft your regex patterns carefully to balance specificity and flexibility. Based on the user's task you may use it to find code patterns, TODO comments, function definitions, or any text-based information across the project. The results include context, so analyze the surrounding code to better understand the matches. Leverage the search_files tool in combination with other tools for more comprehensive analysis. For example, use it to find specific code patterns, then use read_file to examine the full context of interesting matches before using replace_in_file to make informed changes.
|
|
642
640
|
- When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when creating files, as the write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser.
|
|
643
641
|
- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write.
|
|
@@ -692,6 +690,15 @@ class AgenticEdit:
|
|
|
692
690
|
3. Remember, you have extensive capabilities with access to a wide range of tools that can be used in powerful and clever ways as necessary to accomplish each goal. Before calling a tool, do some analysis within <thinking></thinking> tags. First, analyze the file structure provided in environment_details to gain context and insights for proceeding effectively. Then, think about which of the provided tools is the most relevant tool to accomplish the user's task. Next, go through each of the required parameters of the relevant tool and determine if the user has directly provided or given enough information to infer a value. When deciding if the parameter can be inferred, carefully consider all the context to see if it supports a specific value. If all of the required parameters are present or can be reasonably inferred, close the thinking tag and proceed with the tool use. BUT, if one of the values for a required parameter is missing, DO NOT invoke the tool (not even with fillers for the missing params) and instead, ask the user to provide the missing parameters using the ask_followup_question tool. DO NOT ask for more information on optional parameters if it is not provided.
|
|
693
691
|
4. Once you've completed the user's task, you must use the attempt_completion tool to present the result of the task to the user. You may also provide a CLI command to showcase the result of your task; this can be particularly useful for web development tasks, where you can run e.g. \`open index.html\` to show the website you've built.
|
|
694
692
|
5. The user may provide feedback, which you can use to make improvements and try again. But DO NOT continue in pointless back and forth conversations, i.e. don't end your responses with questions or offers for further assistance.
|
|
693
|
+
|
|
694
|
+
{% if file_paths_str %}
|
|
695
|
+
====
|
|
696
|
+
The following are files that the user is currently focusing on.
|
|
697
|
+
Make sure you always start your analysis by using the read_file tool to get the content of the files.
|
|
698
|
+
<files>
|
|
699
|
+
{{file_paths_str}}
|
|
700
|
+
</files>
|
|
701
|
+
{% endif %}
|
|
695
702
|
"""
|
|
696
703
|
import os
|
|
697
704
|
extra_docs = get_rules()
|
|
@@ -702,6 +709,8 @@ class AgenticEdit:
|
|
|
702
709
|
shell_type = "cmd"
|
|
703
710
|
elif shells.is_running_in_powershell():
|
|
704
711
|
shell_type = "powershell"
|
|
712
|
+
|
|
713
|
+
file_paths_str = "\n".join([file_source.module_name for file_source in self.files.sources])
|
|
705
714
|
return {
|
|
706
715
|
"conversation_history": self.conversation_history,
|
|
707
716
|
"env_info": env_info,
|
|
@@ -716,6 +725,7 @@ class AgenticEdit:
|
|
|
716
725
|
"mcp_server_info": self.mcp_server_info,
|
|
717
726
|
"enable_active_context_in_generate": self.args.enable_active_context_in_generate,
|
|
718
727
|
"extra_docs": extra_docs,
|
|
728
|
+
"file_paths_str": file_paths_str,
|
|
719
729
|
}
|
|
720
730
|
|
|
721
731
|
# Removed _execute_command_result and execute_auto_command methods
|
|
@@ -773,27 +783,15 @@ class AgenticEdit:
|
|
|
773
783
|
conversations = [
|
|
774
784
|
{"role": "system", "content": system_prompt},
|
|
775
785
|
]
|
|
776
|
-
|
|
777
|
-
logger.info("Adding initial files context to conversation")
|
|
778
|
-
conversations.append({
|
|
779
|
-
"role":"user","content":f'''
|
|
780
|
-
The following are context files that the user is currently focusing on. These files are presented with their complete paths and up-to-date content, providing essential context to help you better understand the user's needs. If you need more detailed information about specific files or directories not shown here, you can use tools like read_file, search_files, or list_files to explore the codebase further.
|
|
781
|
-
<files>
|
|
782
|
-
{self.files.to_str()}
|
|
783
|
-
</files>'''
|
|
784
|
-
})
|
|
785
|
-
|
|
786
|
-
conversations.append({
|
|
787
|
-
"role":"assistant","content":"Ok"
|
|
788
|
-
})
|
|
789
|
-
|
|
790
|
-
logger.info("Adding conversation history")
|
|
786
|
+
|
|
791
787
|
conversations.append({
|
|
792
788
|
"role": "user", "content": request.user_input
|
|
793
789
|
})
|
|
794
790
|
|
|
795
791
|
logger.info(
|
|
796
792
|
f"Initial conversation history size: {len(conversations)}")
|
|
793
|
+
|
|
794
|
+
logger.info(f"Conversation history: {json.dumps(conversations, indent=2,ensure_ascii=False)}")
|
|
797
795
|
|
|
798
796
|
iteration_count = 0
|
|
799
797
|
tool_executed = False
|
|
@@ -814,17 +812,18 @@ The following are context files that the user is currently focusing on. These fi
|
|
|
814
812
|
|
|
815
813
|
assistant_buffer = ""
|
|
816
814
|
logger.info("Initializing stream chat with LLM")
|
|
815
|
+
|
|
816
|
+
## 实际请求大模型
|
|
817
817
|
llm_response_gen = stream_chat_with_continue(
|
|
818
818
|
llm=self.llm,
|
|
819
819
|
conversations=conversations,
|
|
820
820
|
llm_config={}, # Placeholder for future LLM configs
|
|
821
821
|
args=self.args
|
|
822
822
|
)
|
|
823
|
-
|
|
824
|
-
meta_holder = byzerllm.MetaHolder()
|
|
823
|
+
|
|
825
824
|
logger.info("Starting to parse LLM response stream")
|
|
826
825
|
parsed_events = self.stream_and_parse_llm_response(
|
|
827
|
-
llm_response_gen
|
|
826
|
+
llm_response_gen)
|
|
828
827
|
|
|
829
828
|
event_count = 0
|
|
830
829
|
for event in parsed_events:
|
|
@@ -943,8 +942,9 @@ The following are context files that the user is currently focusing on. These fi
|
|
|
943
942
|
# logger.error("Stopping analyze loop due to parsing error.")
|
|
944
943
|
# return
|
|
945
944
|
|
|
946
|
-
|
|
947
|
-
|
|
945
|
+
elif isinstance(event, TokenUsageEvent):
|
|
946
|
+
logger.info("Yielding token usage event")
|
|
947
|
+
yield event
|
|
948
948
|
|
|
949
949
|
if not tool_executed:
|
|
950
950
|
# No tool executed in this LLM response cycle
|
|
@@ -960,16 +960,21 @@ The following are context files that the user is currently focusing on. These fi
|
|
|
960
960
|
elif last_message["role"] == "assistant":
|
|
961
961
|
logger.info("Appending to existing assistant message")
|
|
962
962
|
last_message["content"] += assistant_buffer
|
|
963
|
-
|
|
964
|
-
#
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
963
|
+
|
|
964
|
+
# 添加系统提示,要求LLM必须使用工具或明确结束,而不是直接退出
|
|
965
|
+
logger.info("Adding system reminder to use tools or attempt completion")
|
|
966
|
+
conversations.append({
|
|
967
|
+
"role": "user",
|
|
968
|
+
"content": "NOTE: You must use an appropriate tool (such as read_file, write_to_file, execute_command, etc.) or explicitly complete the task (using attempt_completion). Do not provide text responses without taking concrete actions. Please select a suitable tool to continue based on the user's task."
|
|
969
|
+
})
|
|
970
|
+
# 继续循环,让 LLM 再思考,而不是 break
|
|
971
|
+
logger.info("Continuing the LLM interaction loop without breaking")
|
|
972
|
+
continue
|
|
973
|
+
|
|
969
974
|
logger.info(f"AgenticEdit analyze loop finished after {iteration_count} iterations.")
|
|
970
975
|
|
|
971
976
|
def stream_and_parse_llm_response(
|
|
972
|
-
self, generator: Generator[Tuple[str, Any], None, None]
|
|
977
|
+
self, generator: Generator[Tuple[str, Any], None, None]
|
|
973
978
|
) -> Generator[Union[LLMOutputEvent, LLMThinkingEvent, ToolCallEvent, ErrorEvent], None, None]:
|
|
974
979
|
"""
|
|
975
980
|
Streamingly parses the LLM response generator, distinguishing between
|
|
@@ -1046,7 +1051,8 @@ The following are context files that the user is currently focusing on. These fi
|
|
|
1046
1051
|
logger.exception(
|
|
1047
1052
|
f"Failed to parse tool XML for <{tool_tag}>: {e}\nXML:\n{tool_xml}")
|
|
1048
1053
|
return None
|
|
1049
|
-
|
|
1054
|
+
|
|
1055
|
+
meta_holder = byzerllm.MetaHolder()
|
|
1050
1056
|
for content_chunk, metadata in generator:
|
|
1051
1057
|
global_cancel.check_and_raise(token=self.args.event_file)
|
|
1052
1058
|
meta_holder.meta = metadata
|
|
@@ -1264,6 +1270,9 @@ The following are context files that the user is currently focusing on. These fi
|
|
|
1264
1270
|
output_cost = (
|
|
1265
1271
|
last_meta.generated_tokens_count * output_price) / 1000000
|
|
1266
1272
|
|
|
1273
|
+
# 添加日志记录
|
|
1274
|
+
logger.info(f"Token Usage Details: Model={model_name}, Input Tokens={last_meta.input_tokens_count}, Output Tokens={last_meta.generated_tokens_count}, Input Cost=${input_cost:.6f}, Output Cost=${output_cost:.6f}")
|
|
1275
|
+
|
|
1267
1276
|
get_event_manager(self.args.event_file).write_result(
|
|
1268
1277
|
EventContentCreator.create_result(content=EventContentCreator.ResultTokenStatContent(
|
|
1269
1278
|
model_name=model_name,
|
|
@@ -1447,6 +1456,9 @@ The following are context files that the user is currently focusing on. These fi
|
|
|
1447
1456
|
output_cost = (
|
|
1448
1457
|
last_meta.generated_tokens_count * output_price) / 1000000
|
|
1449
1458
|
|
|
1459
|
+
# 添加日志记录
|
|
1460
|
+
logger.info(f"Token Usage: Model={model_name}, Input Tokens={last_meta.input_tokens_count}, Output Tokens={last_meta.generated_tokens_count}, Input Cost=${input_cost:.6f}, Output Cost=${output_cost:.6f}")
|
|
1461
|
+
|
|
1450
1462
|
self.printer.print_in_terminal(
|
|
1451
1463
|
"code_generation_complete",
|
|
1452
1464
|
duration=0.0,
|
|
@@ -22,20 +22,23 @@ class ReadFileToolResolver(BaseToolResolver):
|
|
|
22
22
|
abs_project_dir = os.path.abspath(source_dir)
|
|
23
23
|
abs_file_path = os.path.abspath(os.path.join(source_dir, file_path))
|
|
24
24
|
|
|
25
|
-
# Security check: ensure the path is within the source directory
|
|
26
|
-
if not abs_file_path.startswith(abs_project_dir):
|
|
27
|
-
|
|
25
|
+
# # Security check: ensure the path is within the source directory
|
|
26
|
+
# if not abs_file_path.startswith(abs_project_dir):
|
|
27
|
+
# return ToolResult(success=False, message=f"Error: Access denied. Attempted to read file outside the project directory: {file_path}")
|
|
28
28
|
|
|
29
29
|
try:
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
try:
|
|
31
|
+
if self.shadow_manager:
|
|
32
|
+
shadow_path = self.shadow_manager.to_shadow_path(abs_file_path)
|
|
33
|
+
# If shadow file exists, read from it
|
|
34
|
+
if os.path.exists(shadow_path) and os.path.isfile(shadow_path):
|
|
35
|
+
with open(shadow_path, 'r', encoding='utf-8', errors='replace') as f:
|
|
36
|
+
content = f.read()
|
|
37
|
+
logger.info(f"[Shadow] Successfully read shadow file: {shadow_path}")
|
|
38
|
+
return ToolResult(success=True, message=f"Successfully read file (shadow): {file_path}", content=content)
|
|
39
|
+
except Exception as e:
|
|
40
|
+
pass
|
|
41
|
+
# else fallback to original file
|
|
39
42
|
# Fallback to original file
|
|
40
43
|
if not os.path.exists(abs_file_path):
|
|
41
44
|
return ToolResult(success=False, message=f"Error: File not found at path: {file_path}")
|