auto-coder 0.1.361__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.

@@ -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
@@ -692,6 +691,15 @@ class AgenticEdit:
692
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.
693
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.
694
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 %}
696
+ ====
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>
702
+ {% endif %}
695
703
  """
696
704
  import os
697
705
  extra_docs = get_rules()
@@ -702,6 +710,8 @@ class AgenticEdit:
702
710
  shell_type = "cmd"
703
711
  elif shells.is_running_in_powershell():
704
712
  shell_type = "powershell"
713
+
714
+ file_paths_str = "\n".join([file_source.module_name for file_source in self.files.sources])
705
715
  return {
706
716
  "conversation_history": self.conversation_history,
707
717
  "env_info": env_info,
@@ -716,6 +726,7 @@ class AgenticEdit:
716
726
  "mcp_server_info": self.mcp_server_info,
717
727
  "enable_active_context_in_generate": self.args.enable_active_context_in_generate,
718
728
  "extra_docs": extra_docs,
729
+ "file_paths_str": file_paths_str,
719
730
  }
720
731
 
721
732
  # Removed _execute_command_result and execute_auto_command methods
@@ -773,27 +784,15 @@ class AgenticEdit:
773
784
  conversations = [
774
785
  {"role": "system", "content": system_prompt},
775
786
  ]
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")
787
+
791
788
  conversations.append({
792
789
  "role": "user", "content": request.user_input
793
790
  })
794
791
 
795
792
  logger.info(
796
793
  f"Initial conversation history size: {len(conversations)}")
794
+
795
+ logger.info(f"Conversation history: {json.dumps(conversations, indent=2,ensure_ascii=False)}")
797
796
 
798
797
  iteration_count = 0
799
798
  tool_executed = False
@@ -960,12 +959,17 @@ The following are context files that the user is currently focusing on. These fi
960
959
  elif last_message["role"] == "assistant":
961
960
  logger.info("Appending to existing assistant message")
962
961
  last_message["content"] += assistant_buffer
963
- # If the loop ends without AttemptCompletion, it means the LLM finished talking
964
- # without signaling completion. We might just stop or yield a final message.
965
- # Let's assume it stops here.
966
- logger.info("No tool executed and LLM finished. Breaking out of main loop.")
967
- break
968
-
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
+
969
973
  logger.info(f"AgenticEdit analyze loop finished after {iteration_count} iterations.")
970
974
 
971
975
  def stream_and_parse_llm_response(
@@ -1264,6 +1268,9 @@ The following are context files that the user is currently focusing on. These fi
1264
1268
  output_cost = (
1265
1269
  last_meta.generated_tokens_count * output_price) / 1000000
1266
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
+
1267
1274
  get_event_manager(self.args.event_file).write_result(
1268
1275
  EventContentCreator.create_result(content=EventContentCreator.ResultTokenStatContent(
1269
1276
  model_name=model_name,
@@ -1447,6 +1454,9 @@ The following are context files that the user is currently focusing on. These fi
1447
1454
  output_cost = (
1448
1455
  last_meta.generated_tokens_count * output_price) / 1000000
1449
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
+
1450
1460
  self.printer.print_in_terminal(
1451
1461
  "code_generation_complete",
1452
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}")
@@ -215,8 +215,10 @@ class CodeAutoGenerateEditBlock:
215
215
 
216
216
  ====
217
217
  下面是用户的需求:
218
-
218
+
219
+ <user_instruction>
219
220
  {{ instruction }}
221
+ </user_instruction>
220
222
 
221
223
  """
222
224
  if not self.args.include_project_structure:
autocoder/index/index.py CHANGED
@@ -31,6 +31,7 @@ from autocoder.rag.token_counter import count_tokens
31
31
  from autocoder.common.stream_out_type import IndexStreamOutType
32
32
  from autocoder.events.event_manager_singleton import get_event_manager
33
33
  from autocoder.events import event_content as EventContentCreator
34
+ from loguru import logger
34
35
 
35
36
  class IndexManager:
36
37
  def __init__(
@@ -220,7 +221,7 @@ class IndexManager:
220
221
  5. 导入语句中需要包含 jsp:include 整个标签,类似 <jsp:include page="/jspf/prelude.jspf" />
221
222
  6. 导入语句中需要包含 form 标签,类似 <form name="ActionPlanLinkedForm" action="/ri/ActionPlanController.do" method="post">
222
223
  7. 导入语句中需要包含 有 src 属性的 script 标签。比如 <script language="script" src="xxx">
223
- 8. 导入语句中需要包含 有 src 属性的 link 标签。 比如 <link rel="stylesheet" type="text/css" href="/ri/ui/styles/xptheme.css">
224
+ 8. 导入语句中需要包含 有 src 属性的 link 标签。 比如 <link rel="stylesheet" type="text/css" href="/ri/ui/styles/xptheme.css">
224
225
  9. 导入语句中需要包含 ajax 请求里的url,比如 $.ajax({
225
226
  type : "post",
226
227
  url : "admWorkingDay!updateAdmWorkingDayList.action", 中,那么对应的为 <ajax method="post" url="admWorkingDay!updateAdmWorkingDayList.action">
@@ -656,13 +657,18 @@ class IndexManager:
656
657
 
657
658
  index_items = []
658
659
  for module_name, data in index_data.items():
659
- index_item = IndexItem(
660
- module_name=module_name,
661
- symbols=data["symbols"],
662
- last_modified=data["last_modified"],
663
- md5=data["md5"],
664
- )
665
- index_items.append(index_item)
660
+ try:
661
+ index_item = IndexItem(
662
+ module_name=module_name,
663
+ symbols=data["symbols"],
664
+ last_modified=data["last_modified"],
665
+ md5=data["md5"],
666
+ )
667
+ index_items.append(index_item)
668
+ except (KeyError, TypeError) as e:
669
+ logger.warning(f"处理索引条目 {module_name} 时出错: {str(e)}")
670
+ logger.exception(e)
671
+ continue
666
672
 
667
673
  return index_items
668
674