auto-coder 0.1.360__py3-none-any.whl → 0.1.362__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.

Files changed (28) hide show
  1. {auto_coder-0.1.360.dist-info → auto_coder-0.1.362.dist-info}/METADATA +2 -1
  2. {auto_coder-0.1.360.dist-info → auto_coder-0.1.362.dist-info}/RECORD +28 -28
  3. autocoder/agent/auto_learn.py +249 -262
  4. autocoder/auto_coder.py +4 -4
  5. autocoder/auto_coder_runner.py +34 -12
  6. autocoder/commands/auto_command.py +227 -159
  7. autocoder/common/__init__.py +2 -2
  8. autocoder/common/code_auto_generate.py +2 -2
  9. autocoder/common/code_auto_generate_diff.py +2 -1
  10. autocoder/common/code_auto_generate_editblock.py +2 -3
  11. autocoder/common/code_auto_generate_strict_diff.py +2 -1
  12. autocoder/common/ignorefiles/ignore_file_utils.py +12 -8
  13. autocoder/common/result_manager.py +10 -2
  14. autocoder/common/rulefiles/autocoderrules_utils.py +145 -0
  15. autocoder/common/v2/agent/agentic_edit.py +74 -49
  16. autocoder/common/v2/agent/agentic_edit_tools/read_file_tool_resolver.py +15 -12
  17. autocoder/common/v2/code_auto_generate.py +2 -1
  18. autocoder/common/v2/code_auto_generate_diff.py +2 -1
  19. autocoder/common/v2/code_auto_generate_editblock.py +5 -2
  20. autocoder/common/v2/code_auto_generate_strict_diff.py +3 -2
  21. autocoder/index/index.py +14 -8
  22. autocoder/privacy/model_filter.py +297 -35
  23. autocoder/utils/_markitdown.py +22 -3
  24. autocoder/version.py +1 -1
  25. {auto_coder-0.1.360.dist-info → auto_coder-0.1.362.dist-info}/LICENSE +0 -0
  26. {auto_coder-0.1.360.dist-info → auto_coder-0.1.362.dist-info}/WHEEL +0 -0
  27. {auto_coder-0.1.360.dist-info → auto_coder-0.1.362.dist-info}/entry_points.txt +0 -0
  28. {auto_coder-0.1.360.dist-info → auto_coder-0.1.362.dist-info}/top_level.txt +0 -0
@@ -17,6 +17,7 @@ from autocoder.utils import llms as llm_utils
17
17
  from autocoder.common import SourceCodeList
18
18
  from autocoder.memory.active_context_manager import ActiveContextManager
19
19
  from autocoder.common.rulefiles.autocoderrules_utils import get_rules
20
+ from autocoder.run_context import get_run_context,RunMode
20
21
 
21
22
  class CodeAutoGenerateDiff:
22
23
  def __init__(
@@ -404,7 +405,7 @@ class CodeAutoGenerateDiff:
404
405
  generate_mode="diff"
405
406
  )
406
407
 
407
- if not self.args.human_as_model:
408
+ if not self.args.human_as_model or get_run_context().mode == RunMode.WEB:
408
409
  with ThreadPoolExecutor(max_workers=len(self.llms) * self.generate_times_same_model) as executor:
409
410
  futures = []
410
411
  count = 0
@@ -21,8 +21,7 @@ from autocoder.utils import llms as llm_utils
21
21
  from autocoder.common import SourceCodeList
22
22
  from autocoder.memory.active_context_manager import ActiveContextManager
23
23
  from autocoder.common.rulefiles.autocoderrules_utils import get_rules
24
-
25
-
24
+ from autocoder.run_context import get_run_context,RunMode
26
25
 
27
26
  class CodeAutoGenerateEditBlock:
28
27
  def __init__(
@@ -518,7 +517,7 @@ class CodeAutoGenerateEditBlock:
518
517
  generate_mode="editblock"
519
518
  )
520
519
 
521
- if not self.args.human_as_model:
520
+ if not self.args.human_as_model or get_run_context().mode == RunMode.WEB:
522
521
  with ThreadPoolExecutor(max_workers=len(self.llms) * self.generate_times_same_model) as executor:
523
522
  futures = []
524
523
  count = 0
@@ -17,6 +17,7 @@ from autocoder.common import SourceCodeList
17
17
  from autocoder.privacy.model_filter import ModelPathFilter
18
18
  from autocoder.memory.active_context_manager import ActiveContextManager
19
19
  from autocoder.common.rulefiles.autocoderrules_utils import get_rules
20
+ from autocoder.run_context import get_run_context,RunMode
20
21
 
21
22
  class CodeAutoGenerateStrictDiff:
22
23
  def __init__(
@@ -379,7 +380,7 @@ class CodeAutoGenerateStrictDiff:
379
380
  generate_mode="strict_diff"
380
381
  )
381
382
 
382
- if not self.args.human_as_model:
383
+ if not self.args.human_as_model or get_run_context().mode == RunMode.WEB:
383
384
  with ThreadPoolExecutor(max_workers=len(self.llms) * self.generate_times_same_model) as executor:
384
385
  futures = []
385
386
  count = 0
@@ -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(os.getcwd())
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, os.getcwd())
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 = IgnoreFileManager()
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
- self.result_file = os.path.join(self.result_dir, "results.jsonl")
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:
@@ -254,3 +257,145 @@ def parse_rule_file(file_path: str, project_root: Optional[str] = None) -> RuleF
254
257
  if _rules_manager is None:
255
258
  _rules_manager = AutocoderRulesManager(project_root=project_root)
256
259
  return _rules_manager.parse_rule_file(file_path)
260
+
261
+
262
+ # 添加用于返回类型的Pydantic模型
263
+ class RuleRelevance(BaseModel):
264
+ """用于规则相关性判断的返回模型"""
265
+ is_relevant: bool = Field(description="规则是否与当前任务相关")
266
+ reason: str = Field(default="", description="判断理由")
267
+
268
+
269
+ class RuleSelector:
270
+ """
271
+ 根据LLM的判断和规则元数据选择适用的规则。
272
+ """
273
+ def __init__(self, llm: Optional[byzerllm.ByzerLLM], args: Optional[AutoCoderArgs] = None):
274
+ """
275
+ 初始化RuleSelector。
276
+
277
+ Args:
278
+ llm: ByzerLLM 实例,用于判断规则是否适用。如果为 None,则只选择 always_apply=True 的规则。
279
+ args: 传递给 Agent 的参数,可能包含用于规则选择的上下文信息。
280
+ """
281
+ self.llm = llm
282
+ self.args = args
283
+
284
+ @byzerllm.prompt()
285
+ def _build_selection_prompt(self, rule: RuleFile, context: str = "") -> str:
286
+ """
287
+ 判断规则是否适用于当前任务。
288
+
289
+ 规则描述:
290
+ {{ rule.description }}
291
+
292
+ 规则内容摘要 (前200字符):
293
+ {{ rule.content[:200] }}
294
+
295
+ {% if context %}
296
+ 任务上下文:
297
+ {{ context }}
298
+ {% endif %}
299
+
300
+ 基于以上信息,判断这条规则 (路径: {{ rule.file_path }}) 是否与当前任务相关并应该被应用?
301
+
302
+ 请以JSON格式返回结果:
303
+ ```json
304
+ {
305
+ "is_relevant": true或false,
306
+ "reason": "判断理由"
307
+ }
308
+ ```
309
+ """
310
+ # 注意:确保 rule 对象和 context 字典能够被 Jinja2 正确访问。
311
+ # Pydantic模型可以直接在Jinja2中使用其属性。
312
+ return {
313
+ "rule": rule,
314
+ "context": context
315
+ }
316
+
317
+ def select_rules(self, context: str, rules: List[RuleFile]) -> List[RuleFile]:
318
+ """
319
+ 选择适用于当前上下文的规则。
320
+
321
+ Args:
322
+ context: 可选的字典,包含用于规则选择的上下文信息 (例如,用户指令、目标文件等)。
323
+
324
+ Returns:
325
+ List[RuleFile]: 选定的规则列表。
326
+ """
327
+ selected_rules: List[RuleFile] = []
328
+ logger.info(f"开始选择规则,总规则数: {len(rules)}")
329
+
330
+ for rule in rules:
331
+ if rule.always_apply:
332
+ selected_rules.append(rule)
333
+ logger.debug(f"规则 '{os.path.basename(rule.file_path)}' (AlwaysApply=True) 已自动选择。")
334
+ continue
335
+
336
+ if self.llm is None:
337
+ logger.debug(f"规则 '{os.path.basename(rule.file_path)}' (AlwaysApply=False) 已跳过,因为未提供 LLM。")
338
+ continue
339
+
340
+ # 对于 alwaysApply=False 的规则,使用 LLM 判断
341
+ try:
342
+ prompt = self._build_selection_prompt.prompt(rule=rule, context=context)
343
+ logger.debug(f"为规则 '{os.path.basename(rule.file_path)}' 生成的判断 Prompt (片段): {prompt[:200]}...")
344
+
345
+ # **** 实际LLM调用 ****
346
+ # 确保 self.llm 实例已正确初始化并可用
347
+ if self.llm: # Check if llm is not None
348
+ result = None
349
+ try:
350
+ # 使用with_return_type方法获取结构化结果
351
+ result = self._build_selection_prompt.with_llm(self.llm).with_return_type(RuleRelevance).run(rule=rule, context=context)
352
+ if result and result.is_relevant:
353
+ selected_rules.append(rule)
354
+ logger.info(f"规则 '{os.path.basename(rule.file_path)}' (AlwaysApply=False) 已被 LLM 选择,原因: {result.reason}")
355
+ else:
356
+ logger.debug(f"规则 '{os.path.basename(rule.file_path)}' (AlwaysApply=False) 未被 LLM 选择,原因: {result.reason if result else '未提供'}")
357
+ except Exception as e:
358
+ logger.warning(f"LLM 未能为规则 '{os.path.basename(rule.file_path)}' 提供有效响应。")
359
+ # 根据需要决定是否跳过或默认不选
360
+ continue # 跳过此规则
361
+ else: # Handle case where self.llm is None after the initial check
362
+ logger.warning(f"LLM instance became None unexpectedly for rule '{os.path.basename(rule.file_path)}'.")
363
+ continue
364
+
365
+ # **** 模拟LLM调用 (用于测试/开发) ****
366
+ # 注释掉模拟部分,使用上面的实际调用
367
+ # simulated_response = "yes" if "always" in rule.description.lower() or "index" in rule.description.lower() else "no"
368
+ # logger.warning(f"模拟LLM判断规则 '{os.path.basename(rule.file_path)}': {simulated_response}")
369
+ # response_text = simulated_response
370
+ # **** 结束模拟 ****
371
+
372
+ except Exception as e:
373
+ logger.error(f"使用 LLM 判断规则 '{os.path.basename(rule.file_path)}' 时出错: {e}", exc_info=True)
374
+ # 根据策略决定是否包含出错的规则,这里选择跳过
375
+ continue
376
+
377
+ logger.info(f"规则选择完成,选中规则数: {len(selected_rules)}")
378
+ return selected_rules
379
+
380
+ def get_selected_rules_content(self, context: Optional[Dict] = None) -> Dict[str, str]:
381
+ """
382
+ 获取选定规则的文件路径和内容字典。
383
+
384
+ Args:
385
+ context: 传递给 select_rules 的上下文。
386
+
387
+ Returns:
388
+ Dict[str, str]: 选定规则的 {file_path: content} 字典。
389
+ """
390
+ selected_rules = self.select_rules(context=context)
391
+ # 使用 os.path.basename 获取文件名作为 key,如果需要的话
392
+ # return {os.path.basename(rule.file_path): rule.content for rule in selected_rules}
393
+ # 保持 file_path 作为 key
394
+ return {rule.file_path: rule.content for rule in selected_rules}
395
+
396
+ def auto_select_rules(context: str, rules: List[RuleFile], llm: Optional[byzerllm.ByzerLLM] = None,args:Optional[AutoCoderArgs] = None) -> List[RuleFile]:
397
+ """
398
+ 根据LLM的判断和规则元数据选择适用的规则。
399
+ """
400
+ selector = RuleSelector(llm=llm, args=args)
401
+ return selector.select_rules(context=context, rules=rules)
@@ -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
@@ -595,14 +594,36 @@ class AgenticEdit:
595
594
  - If at any point a mermaid diagram would make your plan clearer to help the user quickly see the structure, you are encouraged to include a Mermaid code block in the response. (Note: if you use colors in your mermaid diagrams, be sure to use high contrast colors so the text is readable.)
596
595
  - Finally once it seems like you've reached a good plan, ask the user to switch you back to ACT MODE to implement the solution.
597
596
 
597
+ {% if enable_active_context_in_generate %}
598
+ ====
599
+
600
+ PROJECT PACKAGE CONTEXT
601
+
602
+ Each directory can contain a short **`active.md`** summary file located under the mirrored path inside
603
+ `{{ current_project }}/.auto-coder/active-context/`.
604
+
605
+ * **Purpose** – captures only the files that have **recently changed** in that directory. It is *not* a full listing.
606
+ * **Example** – for `{{ current_project }}/src/abc/bbc`, the summary is
607
+ `{{ current_project }}/.auto-coder/active-context/src/abc/bbc/active.md`.
608
+
609
+ **Reading a summary**
610
+
611
+ ```xml
612
+ <read_file>
613
+ <path>.auto-coder/active-context/src/abc/bbc/active.md</path>
614
+ </read_file>
615
+ ```
616
+
617
+ Use these summaries to quickly decide which files deserve a deeper look with tools like
618
+ `read_file`, `search_files`, or `list_code_definition_names`.
619
+
620
+ {% endif %}
598
621
  ====
599
622
 
600
623
  CAPABILITIES
601
624
 
602
- - You have access to tools that let you execute CLI commands on the user's computer, list files, view source code definitions, regex search${
603
- supportsComputerUse ? ", use the browser" : ""
604
- }, read and edit files, and ask follow-up questions. These tools help you effectively accomplish a wide range of tasks, such as writing code, making edits or improvements to existing files, understanding the current state of a project, performing system operations, and much more.
605
- - When the user initially gives you a task, a recursive list of all filepaths in the current working directory ('${cwd.toPosix()}') will be included in environment_details. This provides an overview of the project's file structure, offering key insights into the project from directory/file names (how developers conceptualize and organize their code) and file extensions (the language used). This can also guide decision-making on which files to explore further. If you need to further explore directories such as outside the current working directory, you can use the list_files tool. If you pass 'true' for the recursive parameter, it will list files recursively. Otherwise, it will list files at the top level, which is better suited for generic directories where you don't necessarily need the nested structure, like the Desktop.
625
+ - You have access to tools that let you execute CLI commands on the user's computer, list files, view source code definitions, regex search, read and edit files, and ask follow-up questions. These tools help you effectively accomplish a wide range of tasks, such as writing code, making edits or improvements to existing files, understanding the current state of a project, performing system operations, and much more.
626
+ - When the user initially gives you a task, a recursive list of all filepaths in the current working directory ('{{ current_project }}') will be included in environment_details. This provides an overview of the project's file structure, offering key insights into the project from directory/file names (how developers conceptualize and organize their code) and file extensions (the language used). This can also guide decision-making on which files to explore further. If you need to further explore directories such as outside the current working directory, you can use the list_files tool. If you pass 'true' for the recursive parameter, it will list files recursively. Otherwise, it will list files at the top level, which is better suited for generic directories where you don't necessarily need the nested structure, like the Desktop.
606
627
  - You can use search_files to perform regex searches across files in a specified directory, outputting context-rich results that include surrounding lines. This is particularly useful for understanding code patterns, finding specific implementations, or identifying areas that need refactoring.
607
628
  - You can use the list_code_definition_names tool to get an overview of source code definitions for all files at the top level of a specified directory. This can be particularly useful when you need to understand the broader context and relationships between certain parts of the code. You may need to call this tool multiple times to understand various parts of the codebase related to the task.
608
629
  - For example, when asked to make edits or improvements you might analyze the file structure in the initial environment_details to get an overview of the project, then use list_code_definition_names to get further insight using source code definitions for files located in relevant directories, then read_file to examine the contents of relevant files, analyze the code and suggest improvements or make necessary edits, then use the replace_in_file tool to implement changes. If you refactored code that could affect other parts of the codebase, you could use search_files to ensure you update other files as needed.
@@ -613,7 +634,7 @@ class AgenticEdit:
613
634
  RULES
614
635
 
615
636
  - Your current working directory is: {{current_project}}
616
- - You cannot \`cd\` into a different directory to complete a task. You are stuck operating from '${cwd.toPosix()}', so be sure to pass in the correct 'path' parameter when using tools that require a path.
637
+ - 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.
617
638
  - Do not use the ~ character or $HOME to refer to the home directory.
618
639
  - 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 '${cwd.toPosix()}', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '${cwd.toPosix()}'). For example, if you needed to run \`npm install\` in a project outside of '${cwd.toPosix()}', 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)\`.
619
640
  - 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.
@@ -634,6 +655,21 @@ class AgenticEdit:
634
655
  - When using the replace_in_file tool, you must include complete lines in your SEARCH blocks, not partial lines. The system requires exact line matches and cannot match partial lines. For example, if you want to match a line containing "const x = 5;", your SEARCH block must include the entire line, not just "x = 5" or other fragments.
635
656
  - When using the replace_in_file tool, if you use multiple SEARCH/REPLACE blocks, list them in the order they appear in the file. For example if you need to make changes to both line 10 and line 50, first include the SEARCH/REPLACE block for line 10, followed by the SEARCH/REPLACE block for line 50.
636
657
  - It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc.
658
+
659
+ {% if extra_docs %}
660
+ ====
661
+
662
+ RULES PROVIDED BY USER
663
+
664
+ The following rules are provided by the user, and you must follow them strictly.
665
+
666
+ {% for key, value in extra_docs.items() %}
667
+ <user_rule>
668
+ ##File: {{ key }}
669
+ {{ value }}
670
+ </user_rule>
671
+ {% endfor %}
672
+ {% endif %}
637
673
 
638
674
  ====
639
675
 
@@ -654,28 +690,15 @@ class AgenticEdit:
654
690
  2. Work through these goals sequentially, utilizing available tools one at a time as necessary. Each goal should correspond to a distinct step in your problem-solving process. You will be informed on the work completed and what's remaining as you go.
655
691
  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.
656
692
  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.
657
- 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.
658
-
659
- {% if enable_active_context %}
660
- **Very Important Notice**
661
- Each directory has a description file stored separately. For example, the description for the directory `{{ current_project }}/src/abc/bbc` can be found in the file `{{ current_project }}/.auto-coder/active-context/src/abc/bbc/active.md`.
662
- You can use the tool `read_file` to read these description files, which helps you decide exactly which files need detailed attention. Note that the `active.md` file does not contain information about all files within the directory—it only includes information
663
- about the files that were recently changed.
664
- {% endif %}
665
-
666
- {% if extra_docs %}
693
+ 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.
694
+
695
+ {% if file_paths_str %}
667
696
  ====
668
-
669
- RULES PROVIDED BY USER
670
-
671
- The following rules are provided by the user, and you must follow them strictly.
672
-
673
- {% for key, value in extra_docs.items() %}
674
- <user_rule>
675
- ##File: {{ key }}
676
- {{ value }}
677
- </user_rule>
678
- {% endfor %}
697
+ The following are files that the user is currently focusing on.
698
+ Make sure you always start your analysis by using the read_file tool to get the content of the files.
699
+ <files>
700
+ {{file_paths_str}}
701
+ </files>
679
702
  {% endif %}
680
703
  """
681
704
  import os
@@ -687,6 +710,8 @@ class AgenticEdit:
687
710
  shell_type = "cmd"
688
711
  elif shells.is_running_in_powershell():
689
712
  shell_type = "powershell"
713
+
714
+ file_paths_str = "\n".join([file_source.module_name for file_source in self.files.sources])
690
715
  return {
691
716
  "conversation_history": self.conversation_history,
692
717
  "env_info": env_info,
@@ -699,8 +724,9 @@ class AgenticEdit:
699
724
  "home_dir": os.path.expanduser("~"),
700
725
  "files": self.files.to_str(),
701
726
  "mcp_server_info": self.mcp_server_info,
702
- "enable_active_context": self.args.enable_active_context,
727
+ "enable_active_context_in_generate": self.args.enable_active_context_in_generate,
703
728
  "extra_docs": extra_docs,
729
+ "file_paths_str": file_paths_str,
704
730
  }
705
731
 
706
732
  # Removed _execute_command_result and execute_auto_command methods
@@ -758,27 +784,15 @@ class AgenticEdit:
758
784
  conversations = [
759
785
  {"role": "system", "content": system_prompt},
760
786
  ]
761
-
762
- logger.info("Adding initial files context to conversation")
763
- conversations.append({
764
- "role":"user","content":f'''
765
- Below are some files the user is focused on, and the content is up to date. These entries show the file paths along with their full text content, which can help you better understand the user's needs. If the information is insufficient, you can use tools such as read_file to retrieve more details.
766
- <files>
767
- {self.files.to_str()}
768
- </files>'''
769
- })
770
-
771
- conversations.append({
772
- "role":"assistant","content":"Ok"
773
- })
774
-
775
- logger.info("Adding conversation history")
787
+
776
788
  conversations.append({
777
789
  "role": "user", "content": request.user_input
778
790
  })
779
791
 
780
792
  logger.info(
781
793
  f"Initial conversation history size: {len(conversations)}")
794
+
795
+ logger.info(f"Conversation history: {json.dumps(conversations, indent=2,ensure_ascii=False)}")
782
796
 
783
797
  iteration_count = 0
784
798
  tool_executed = False
@@ -945,12 +959,17 @@ Below are some files the user is focused on, and the content is up to date. Thes
945
959
  elif last_message["role"] == "assistant":
946
960
  logger.info("Appending to existing assistant message")
947
961
  last_message["content"] += assistant_buffer
948
- # If the loop ends without AttemptCompletion, it means the LLM finished talking
949
- # without signaling completion. We might just stop or yield a final message.
950
- # Let's assume it stops here.
951
- logger.info("No tool executed and LLM finished. Breaking out of main loop.")
952
- break
953
-
962
+
963
+ # 添加系统提示,要求LLM必须使用工具或明确结束,而不是直接退出
964
+ logger.info("Adding system reminder to use tools or attempt completion")
965
+ conversations.append({
966
+ "role": "user",
967
+ "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."
968
+ })
969
+ # 继续循环,让 LLM 再思考,而不是 break
970
+ logger.info("Continuing the LLM interaction loop without breaking")
971
+ continue
972
+
954
973
  logger.info(f"AgenticEdit analyze loop finished after {iteration_count} iterations.")
955
974
 
956
975
  def stream_and_parse_llm_response(
@@ -1249,6 +1268,9 @@ Below are some files the user is focused on, and the content is up to date. Thes
1249
1268
  output_cost = (
1250
1269
  last_meta.generated_tokens_count * output_price) / 1000000
1251
1270
 
1271
+ # 添加日志记录
1272
+ 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}")
1273
+
1252
1274
  get_event_manager(self.args.event_file).write_result(
1253
1275
  EventContentCreator.create_result(content=EventContentCreator.ResultTokenStatContent(
1254
1276
  model_name=model_name,
@@ -1432,6 +1454,9 @@ Below are some files the user is focused on, and the content is up to date. Thes
1432
1454
  output_cost = (
1433
1455
  last_meta.generated_tokens_count * output_price) / 1000000
1434
1456
 
1457
+ # 添加日志记录
1458
+ 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}")
1459
+
1435
1460
  self.printer.print_in_terminal(
1436
1461
  "code_generation_complete",
1437
1462
  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
- return ToolResult(success=False, message=f"Error: Access denied. Attempted to read file outside the project directory: {file_path}")
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
- if self.shadow_manager:
31
- shadow_path = self.shadow_manager.to_shadow_path(abs_file_path)
32
- # If shadow file exists, read from it
33
- if os.path.exists(shadow_path) and os.path.isfile(shadow_path):
34
- with open(shadow_path, 'r', encoding='utf-8', errors='replace') as f:
35
- content = f.read()
36
- logger.info(f"[Shadow] Successfully read shadow file: {shadow_path}")
37
- return ToolResult(success=True, message=f"Successfully read file (shadow): {file_path}", content=content)
38
- # else fallback to original file
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}")
@@ -17,6 +17,7 @@ from autocoder.common import SourceCodeList
17
17
  from autocoder.memory.active_context_manager import ActiveContextManager
18
18
  from loguru import logger
19
19
  from autocoder.common.rulefiles.autocoderrules_utils import get_rules
20
+ from autocoder.run_context import get_run_context,RunMode
20
21
 
21
22
  class CodeAutoGenerate:
22
23
  def __init__(
@@ -199,7 +200,7 @@ class CodeAutoGenerate:
199
200
  generate_mode="diff"
200
201
  )
201
202
 
202
- if not self.args.human_as_model:
203
+ if not self.args.human_as_model or get_run_context().mode == RunMode.WEB:
203
204
  with ThreadPoolExecutor(max_workers=len(self.llms) * self.generate_times_same_model) as executor:
204
205
  futures = []
205
206
  count = 0
@@ -16,6 +16,7 @@ from autocoder.utils import llms as llm_utils
16
16
  from autocoder.common import SourceCodeList
17
17
  from autocoder.memory.active_context_manager import ActiveContextManager
18
18
  from autocoder.common.rulefiles.autocoderrules_utils import get_rules
19
+ from autocoder.run_context import get_run_context,RunMode
19
20
 
20
21
  class CodeAutoGenerateDiff:
21
22
  def __init__(
@@ -301,7 +302,7 @@ class CodeAutoGenerateDiff:
301
302
  generate_mode="diff"
302
303
  )
303
304
 
304
- if not self.args.human_as_model:
305
+ if not self.args.human_as_model or get_run_context().mode == RunMode.WEB:
305
306
  with ThreadPoolExecutor(max_workers=len(self.llms) * self.generate_times_same_model) as executor:
306
307
  futures = []
307
308
  count = 0
@@ -17,6 +17,7 @@ from autocoder.common import SourceCodeList
17
17
  from autocoder.memory.active_context_manager import ActiveContextManager
18
18
  from autocoder.common.rulefiles.autocoderrules_utils import get_rules
19
19
  from loguru import logger
20
+ from autocoder.run_context import get_run_context,RunMode
20
21
 
21
22
 
22
23
 
@@ -214,8 +215,10 @@ class CodeAutoGenerateEditBlock:
214
215
 
215
216
  ====
216
217
  下面是用户的需求:
217
-
218
+
219
+ <user_instruction>
218
220
  {{ instruction }}
221
+ </user_instruction>
219
222
 
220
223
  """
221
224
  if not self.args.include_project_structure:
@@ -318,7 +321,7 @@ class CodeAutoGenerateEditBlock:
318
321
  generate_mode="editblock"
319
322
  )
320
323
 
321
- if not self.args.human_as_model:
324
+ if not self.args.human_as_model or get_run_context().mode == RunMode.WEB:
322
325
  with ThreadPoolExecutor(max_workers=len(self.llms) * self.generate_times_same_model) as executor:
323
326
  futures = []
324
327
  count = 0
@@ -16,7 +16,8 @@ from autocoder.utils import llms as llm_utils
16
16
  from autocoder.common import SourceCodeList
17
17
  from autocoder.privacy.model_filter import ModelPathFilter
18
18
  from autocoder.memory.active_context_manager import ActiveContextManager
19
- from autocoder.common.rulefiles.autocoderrules_utils import get_rules
19
+ from autocoder.common.rulefiles.autocoderrules_utils import get_rule
20
+ from autocoder.run_context import get_run_context,RunMode
20
21
  class CodeAutoGenerateStrictDiff:
21
22
  def __init__(
22
23
  self, llm: byzerllm.ByzerLLM, args: AutoCoderArgs, action=None
@@ -375,7 +376,7 @@ class CodeAutoGenerateStrictDiff:
375
376
  generate_mode="strict_diff"
376
377
  )
377
378
 
378
- if not self.args.human_as_model:
379
+ if not self.args.human_as_model or get_run_context().mode == RunMode.WEB:
379
380
  with ThreadPoolExecutor(max_workers=len(self.llms) * self.generate_times_same_model) as executor:
380
381
  futures = []
381
382
  count = 0