auto-coder-web 0.1.25__py3-none-any.whl → 0.1.27__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.
@@ -45,7 +45,11 @@ class AutoCoderRunnerWrapper:
45
45
  return auto_command(command,params)
46
46
 
47
47
  def configure_wrapper(self,conf: str, skip_print=False ):
48
- return configure(conf, skip_print)
48
+ return configure(conf, skip_print)
49
+
50
+ def get_conf_wrapper(self):
51
+ memory = get_memory()
52
+ return memory["conf"]
49
53
 
50
54
 
51
55
  def coding_wapper(self,query):
@@ -1,42 +1,39 @@
1
1
  from fastapi import APIRouter, Request, HTTPException, Depends
2
+ from autocoder.auto_coder_runner import get_memory, configure
2
3
 
3
4
  router = APIRouter()
4
5
 
5
- async def get_auto_coder_runner(request: Request):
6
- """获取AutoCoderRunner实例作为依赖"""
7
- return request.app.state.auto_coder_runner
8
6
 
9
7
  @router.get("/api/conf")
10
- async def get_conf(
11
- auto_coder_runner = Depends(get_auto_coder_runner)
12
- ):
8
+ async def get_conf():
13
9
  """获取配置信息"""
14
- return {"conf": auto_coder_runner.get_config()}
10
+ memory = get_memory()
11
+ return {"conf": memory["conf"]}
12
+
15
13
 
16
14
  @router.post("/api/conf")
17
15
  async def config(
18
16
  request: Request,
19
- auto_coder_runner = Depends(get_auto_coder_runner)
20
17
  ):
21
18
  """更新配置信息"""
22
19
  data = await request.json()
23
20
  try:
24
21
  for key, value in data.items():
25
- auto_coder_runner.configure(key, str(value))
22
+ configure(f"{key}:{str(value)}")
26
23
  return {"status": "success"}
27
24
  except Exception as e:
28
25
  raise HTTPException(status_code=400, detail=str(e))
29
26
 
27
+
30
28
  @router.delete("/api/conf/{key}")
31
29
  async def delete_config(
32
- key: str,
33
- auto_coder_runner = Depends(get_auto_coder_runner)
30
+ key: str
34
31
  ):
35
32
  """删除配置项"""
36
33
  try:
37
- result = auto_coder_runner.drop_config(key)
38
- return result
34
+ configure(f"/drop {key}")
35
+ return {"status": "success"}
39
36
  except ValueError as e:
40
37
  raise HTTPException(status_code=404, detail=str(e))
41
38
  except Exception as e:
42
- raise HTTPException(status_code=400, detail=str(e))
39
+ raise HTTPException(status_code=400, detail=str(e))
@@ -1,31 +1,39 @@
1
1
  import os
2
2
  import json
3
- from fastapi import APIRouter, HTTPException
3
+ from fastapi import APIRouter, HTTPException, Request, Depends
4
4
  import aiofiles
5
5
  from auto_coder_web.types import ChatList
6
6
 
7
+
8
+ async def get_project_path(request: Request) -> str:
9
+ """
10
+ 从FastAPI请求上下文中获取项目路径
11
+ """
12
+ return request.app.state.project_path
13
+
7
14
  router = APIRouter()
8
15
 
16
+
9
17
  @router.post("/api/chat-lists/save")
10
- async def save_chat_list(chat_list: ChatList):
18
+ async def save_chat_list(chat_list: ChatList, project_path: str = Depends(get_project_path)):
11
19
  try:
12
- chat_lists_dir = os.path.join(
13
- ".auto-coder", "auto-coder.web", "chat-lists")
20
+ chat_lists_dir = os.path.join(project_path,
21
+ ".auto-coder", "auto-coder.web", "chat-lists")
14
22
  os.makedirs(chat_lists_dir, exist_ok=True)
15
23
 
16
- file_path = os.path.join(
17
- chat_lists_dir, f"{chat_list.name}.json")
24
+ file_path = os.path.join(chat_lists_dir, f"{chat_list.name}.json")
18
25
  async with aiofiles.open(file_path, 'w') as f:
19
- await f.write(json.dumps({"messages": chat_list.messages}, indent=2))
26
+ await f.write(json.dumps({"messages": chat_list.messages}, indent=2, ensure_ascii=False))
20
27
  return {"status": "success", "message": f"Chat list {chat_list.name} saved successfully"}
21
28
  except Exception as e:
22
29
  raise HTTPException(status_code=500, detail=str(e))
23
30
 
31
+
24
32
  @router.get("/api/chat-lists")
25
- async def get_chat_lists():
33
+ async def get_chat_lists(project_path: str = Depends(get_project_path)):
26
34
  try:
27
35
  chat_lists_dir = os.path.join(
28
- ".auto-coder", "auto-coder.web", "chat-lists")
36
+ project_path, ".auto-coder", "auto-coder.web", "chat-lists")
29
37
  os.makedirs(chat_lists_dir, exist_ok=True)
30
38
 
31
39
  # Get files with their modification times
@@ -45,11 +53,12 @@ async def get_chat_lists():
45
53
  except Exception as e:
46
54
  raise HTTPException(status_code=500, detail=str(e))
47
55
 
56
+
48
57
  @router.get("/api/chat-lists/{name}")
49
- async def get_chat_list(name: str):
58
+ async def get_chat_list(name: str, project_path: str = Depends(get_project_path)):
50
59
  try:
51
60
  file_path = os.path.join(
52
- ".auto-coder", "auto-coder.web", "chat-lists", f"{name}.json")
61
+ project_path, ".auto-coder", "auto-coder.web", "chat-lists", f"{name}.json")
53
62
  if not os.path.exists(file_path):
54
63
  raise HTTPException(
55
64
  status_code=404, detail=f"Chat list {name} not found")
@@ -60,11 +69,12 @@ async def get_chat_list(name: str):
60
69
  except Exception as e:
61
70
  raise HTTPException(status_code=500, detail=str(e))
62
71
 
72
+
63
73
  @router.delete("/api/chat-lists/{name}")
64
- async def delete_chat_list(name: str):
74
+ async def delete_chat_list(name: str, project_path: str = Depends(get_project_path)):
65
75
  try:
66
76
  file_path = os.path.join(
67
- ".auto-coder", "auto-coder.web", "chat-lists", f"{name}.json")
77
+ project_path, ".auto-coder", "auto-coder.web", "chat-lists", f"{name}.json")
68
78
  if not os.path.exists(file_path):
69
79
  raise HTTPException(
70
80
  status_code=404, detail=f"Chat list {name} not found")
@@ -72,4 +82,4 @@ async def delete_chat_list(name: str):
72
82
  os.remove(file_path)
73
83
  return {"status": "success", "message": f"Chat list {name} deleted successfully"}
74
84
  except Exception as e:
75
- raise HTTPException(status_code=500, detail=str(e))
85
+ raise HTTPException(status_code=500, detail=str(e))
@@ -1,22 +1,135 @@
1
1
  import os
2
+ import glob
3
+ import json
4
+ from typing import List
5
+ from pydantic import BaseModel
2
6
  from fastapi import APIRouter, Query, Request, Depends
3
7
  from auto_coder_web.types import CompletionItem, CompletionResponse
8
+ from autocoder.index.symbols_utils import (
9
+ extract_symbols,
10
+ symbols_info_to_str,
11
+ SymbolsInfo,
12
+ SymbolType,
13
+ )
14
+
15
+ from autocoder.auto_coder_runner import get_memory
16
+ import json
17
+ import asyncio
18
+
4
19
 
5
20
  router = APIRouter()
6
21
 
22
+ class SymbolItem(BaseModel):
23
+ symbol_name: str
24
+ symbol_type: SymbolType
25
+ file_name: str
26
+
7
27
  async def get_auto_coder_runner(request: Request):
8
28
  """获取AutoCoderRunner实例作为依赖"""
9
29
  return request.app.state.auto_coder_runner
10
30
 
31
+
32
+ async def get_project_path(request: Request):
33
+ """获取项目路径作为依赖"""
34
+ return request.app.state.project_path
35
+
36
+ def find_files_in_project(patterns: List[str], project_path: str) -> List[str]:
37
+ memory = get_memory()
38
+ default_exclude_dirs = [".git", "node_modules", "dist", "build", "__pycache__",".venv"]
39
+ active_file_list = memory["current_files"]["files"]
40
+
41
+ project_root = project_path
42
+ matched_files = []
43
+
44
+ if len(patterns) == 1 and patterns[0] == "":
45
+ return active_file_list
46
+
47
+ for pattern in patterns:
48
+ for file_path in active_file_list:
49
+ if pattern in os.path.basename(file_path):
50
+ matched_files.append(file_path)
51
+
52
+ final_exclude_dirs = default_exclude_dirs + \
53
+ memory.get("exclude_dirs", [])
54
+
55
+ for pattern in patterns:
56
+ if "*" in pattern or "?" in pattern:
57
+ for file_path in glob.glob(pattern, recursive=True):
58
+ if os.path.isfile(file_path):
59
+ abs_path = os.path.abspath(file_path)
60
+ if not any(
61
+ exclude_dir in abs_path.split(os.sep)
62
+ for exclude_dir in final_exclude_dirs
63
+ ):
64
+ matched_files.append(abs_path)
65
+ else:
66
+ is_added = False
67
+ for root, dirs, files in os.walk(project_root, followlinks=True):
68
+ dirs[:] = [d for d in dirs if d not in final_exclude_dirs]
69
+ if pattern in files:
70
+ matched_files.append(os.path.join(root, pattern))
71
+ is_added = True
72
+ else:
73
+ for file in files:
74
+ if pattern in os.path.join(root, file):
75
+ matched_files.append(os.path.join(root, file))
76
+ is_added = True
77
+ if not is_added:
78
+ matched_files.append(pattern)
79
+
80
+ return list(set(matched_files))
81
+
82
+ def get_symbol_list(project_path: str) -> List[SymbolItem]:
83
+ list_of_symbols = []
84
+ index_file = os.path.join(
85
+ project_path, ".auto-coder", "index.json")
86
+
87
+ if os.path.exists(index_file):
88
+ with open(index_file, "r") as file:
89
+ index_data = json.load(file)
90
+ else:
91
+ index_data = {}
92
+
93
+ for item in index_data.values():
94
+ symbols_str = item["symbols"]
95
+ module_name = item["module_name"]
96
+ info1 = extract_symbols(symbols_str)
97
+ for name in info1.classes:
98
+ list_of_symbols.append(
99
+ SymbolItem(
100
+ symbol_name=name,
101
+ symbol_type=SymbolType.CLASSES,
102
+ file_name=module_name,
103
+ )
104
+ )
105
+ for name in info1.functions:
106
+ list_of_symbols.append(
107
+ SymbolItem(
108
+ symbol_name=name,
109
+ symbol_type=SymbolType.FUNCTIONS,
110
+ file_name=module_name,
111
+ )
112
+ )
113
+ for name in info1.variables:
114
+ list_of_symbols.append(
115
+ SymbolItem(
116
+ symbol_name=name,
117
+ symbol_type=SymbolType.VARIABLES,
118
+ file_name=module_name,
119
+ )
120
+ )
121
+ return list_of_symbols
122
+
11
123
  @router.get("/api/completions/files")
12
124
  async def get_file_completions(
13
125
  name: str = Query(...),
14
- auto_coder_runner = Depends(get_auto_coder_runner)
126
+ project_path: str = Depends(get_project_path)
15
127
  ):
16
128
  """获取文件名补全"""
17
- matches = auto_coder_runner.find_files_in_project([name])
129
+ patterns = [name]
130
+ matches = await asyncio.to_thread(find_files_in_project, patterns,project_path)
18
131
  completions = []
19
- project_root = auto_coder_runner.project_path
132
+ project_root = project_path
20
133
  for file_name in matches:
21
134
  path_parts = file_name.split(os.sep)
22
135
  # 只显示最后三层路径,让显示更简洁
@@ -35,19 +148,19 @@ async def get_file_completions(
35
148
  @router.get("/api/completions/symbols")
36
149
  async def get_symbol_completions(
37
150
  name: str = Query(...),
38
- auto_coder_runner = Depends(get_auto_coder_runner)
151
+ project_path: str = Depends(get_project_path)
39
152
  ):
40
153
  """获取符号补全"""
41
- symbols = auto_coder_runner.get_symbol_list()
154
+ symbols = await asyncio.to_thread(get_symbol_list, project_path)
42
155
  matches = []
43
156
 
44
157
  for symbol in symbols:
45
158
  if name.lower() in symbol.symbol_name.lower():
46
159
  relative_path = os.path.relpath(
47
- symbol.file_name, auto_coder_runner.project_path)
160
+ symbol.file_name, project_path)
48
161
  matches.append(CompletionItem(
49
162
  name=symbol.symbol_name,
50
- path=f"{symbol.symbol_name} ({relative_path}/{symbol.symbol_type.value})",
163
+ path=relative_path,
51
164
  display=f"{symbol.symbol_name}(location: {relative_path})"
52
165
  ))
53
166
  return CompletionResponse(completions=matches)
@@ -1,40 +1,116 @@
1
+ from typing import List
2
+ import asyncio
1
3
  from fastapi import APIRouter, Request, HTTPException, Depends
2
4
  from autocoder.agent.auto_filegroup import AutoFileGroup
3
5
  from autocoder.utils import operate_config_api
6
+ from autocoder.auto_coder_runner import get_memory,save_memory, load_memory
7
+ import json
8
+ import os
4
9
 
5
10
  router = APIRouter()
6
11
 
7
- async def get_file_group_manager(request: Request):
8
- """获取FileGroupManager实例作为依赖"""
9
- return request.app.state.file_group_manager
10
12
 
11
13
  async def get_project_path(request: Request):
12
14
  """获取项目路径作为依赖"""
13
15
  return request.app.state.project_path
14
16
 
15
- async def get_auto_coder_runner(request: Request):
16
- """获取AutoCoderRunner实例作为依赖"""
17
- return request.app.state.auto_coder_runner
17
+
18
+ def _create_file_group(group_name: str, description: str):
19
+ memory = get_memory()
20
+ if group_name in memory["current_files"]["groups"]:
21
+ return None
22
+
23
+ memory["current_files"]["groups"][group_name] = []
24
+
25
+ if "groups_info" not in memory["current_files"]:
26
+ memory["current_files"]["groups_info"] = {}
27
+
28
+ memory["current_files"]["groups_info"][group_name] = {
29
+ "query_prefix": description
30
+ }
31
+ save_memory()
32
+
33
+
34
+ def _add_files_to_group(project_path: str, name: str, files: List[str]):
35
+ memory = get_memory()
36
+ for file in files:
37
+ memory["current_files"]["groups"][name].append(
38
+ os.path.join(project_path, file))
39
+ save_memory()
40
+
41
+
42
+ def _remove_file_from_group(project_path: str, name: str, files: List[str]):
43
+ memory = get_memory()
44
+ for file in files:
45
+ memory["current_files"]["groups"][name].remove(
46
+ os.path.join(project_path, file))
47
+ save_memory()
48
+
49
+
50
+ def _update_group_description(name: str, description: str):
51
+ memory = get_memory()
52
+ memory["current_files"]["groups_info"][name]["query_prefix"] = description
53
+ save_memory()
54
+
55
+
56
+ def _get_groups(project_path: str):
57
+ memory = get_memory()
58
+ v = [
59
+ {
60
+ "name": group_name,
61
+ "files": memory["current_files"]["groups"][group_name],
62
+ "description": memory["current_files"]["groups_info"][group_name]["query_prefix"]
63
+ } for group_name in memory["current_files"]["groups"]
64
+ ]
65
+ return v
66
+
67
+ def _switch_groups(group_names: List[str], file_paths: List[str] = None):
68
+ memory = get_memory()
69
+ new_files = []
70
+ for group_name in group_names:
71
+ files = memory["current_files"]["groups"][group_name]
72
+ new_files.extend(files)
73
+
74
+ # Add individual file paths if provided
75
+ if file_paths:
76
+ for file_path in file_paths:
77
+ # Only add unique paths
78
+ if file_path not in new_files:
79
+ new_files.append(file_path)
80
+
81
+ memory["current_files"]["files"] = new_files
82
+ memory["current_files"]["current_groups"] = group_names
83
+ save_memory()
84
+
85
+
86
+ def _delete_file_group(project_path: str, group_name: str):
87
+ memory = get_memory()
88
+ if group_name not in memory["current_files"]["groups"]:
89
+ return None
90
+ del memory["current_files"]["groups"][group_name]
91
+ if group_name in memory["current_files"]["groups_info"]:
92
+ del memory["current_files"]["groups_info"][group_name]
93
+ save_memory()
94
+
18
95
 
19
96
  @router.post("/api/file-groups")
20
97
  async def create_file_group(
21
- request: Request,
22
- file_group_manager = Depends(get_file_group_manager)
98
+ request: Request
23
99
  ):
24
100
  data = await request.json()
25
- name = data.get("name")
101
+ group_name = data.get("name")
26
102
  description = data.get("description", "")
27
- group = await file_group_manager.create_group(name, description)
28
- return group
103
+ await asyncio.to_thread(_create_file_group, group_name, description)
104
+ return {"status": "success", "message": f"Created group: {group_name}"}
105
+
29
106
 
30
107
  @router.post("/api/file-groups/auto")
31
108
  async def auto_create_groups(
32
109
  request: Request,
33
- file_group_manager = Depends(get_file_group_manager),
34
- project_path: str = Depends(get_project_path),
35
- auto_coder_runner = Depends(get_auto_coder_runner)
36
- ):
110
+ project_path: str = Depends(get_project_path)
111
+ ):
37
112
  try:
113
+ memory = get_memory()
38
114
  data = await request.json()
39
115
  file_size_limit = data.get("file_size_limit", 100)
40
116
  skip_diff = data.get("skip_diff", False)
@@ -42,7 +118,7 @@ async def auto_create_groups(
42
118
 
43
119
  # Create AutoFileGroup instance
44
120
  auto_grouper = AutoFileGroup(
45
- operate_config_api.get_llm(auto_coder_runner.memory),
121
+ operate_config_api.get_llm(memory),
46
122
  project_path,
47
123
  skip_diff=skip_diff,
48
124
  file_size_limit=file_size_limit,
@@ -54,67 +130,80 @@ async def auto_create_groups(
54
130
 
55
131
  # Create groups using file_group_manager
56
132
  for group in groups:
57
- await file_group_manager.create_group(
58
- name=group.name,
59
- description=group.description
60
- )
133
+ await asyncio.to_thread(_create_file_group,
134
+ name=group.name,
135
+ description=group.description
136
+ )
61
137
  # Add files to the group
62
- await file_group_manager.add_files_to_group(
63
- group.name,
64
- group.urls
65
- )
138
+ await asyncio.to_thread(_add_files_to_group,
139
+ group.name,
140
+ group.urls
141
+ )
66
142
 
67
143
  return {"status": "success", "message": f"Created {len(groups)} groups"}
68
144
  except Exception as e:
69
145
  raise HTTPException(status_code=500, detail=str(e))
70
146
 
147
+
71
148
  @router.post("/api/file-groups/switch")
72
149
  async def switch_file_groups(
73
150
  request: Request,
74
- file_group_manager = Depends(get_file_group_manager)
151
+ project_path: str = Depends(get_project_path)
75
152
  ):
76
153
  data = await request.json()
77
154
  group_names = data.get("group_names", [])
78
- result = await file_group_manager.switch_groups(group_names)
79
- return result
155
+ file_paths = data.get("file_paths", [])
156
+
157
+ # Convert relative file paths to absolute paths
158
+ absolute_file_paths = []
159
+ for file_path in file_paths:
160
+ absolute_path = os.path.join(project_path, file_path)
161
+ absolute_file_paths.append(absolute_path)
162
+
163
+ await asyncio.to_thread(_switch_groups, group_names, absolute_file_paths)
164
+ return {"status": "success", "message": f"Switched to groups: {group_names} and additional files"}
165
+
80
166
 
81
167
  @router.delete("/api/file-groups/{name}")
82
168
  async def delete_file_group(
83
169
  name: str,
84
- file_group_manager = Depends(get_file_group_manager)
170
+ project_path: str = Depends(get_project_path)
85
171
  ):
86
- await file_group_manager.delete_group(name)
87
- return {"status": "success"}
172
+ await asyncio.to_thread(_delete_file_group, project_path, name)
173
+ return {"status": "success", "message": f"Deleted group: {name}"}
174
+
88
175
 
89
176
  @router.post("/api/file-groups/{name}/files")
90
177
  async def add_files_to_group(
91
- name: str,
178
+ name: str,
92
179
  request: Request,
93
- file_group_manager = Depends(get_file_group_manager)
180
+ project_path: str = Depends(get_project_path)
94
181
  ):
95
182
  data = await request.json()
96
183
  files = data.get("files", [])
97
184
  description = data.get("description")
98
185
  if description is not None:
99
- group = await file_group_manager.update_group_description(name, description)
186
+ await asyncio.to_thread(_update_group_description, name, description)
100
187
  else:
101
- group = await file_group_manager.add_files_to_group(name, files)
102
- return group
188
+ await asyncio.to_thread(_add_files_to_group, project_path, name, files)
189
+ return {"status": "success", "message": f"Added files to group: {name}"}
190
+
103
191
 
104
192
  @router.delete("/api/file-groups/{name}/files")
105
193
  async def remove_files_from_group(
106
- name: str,
194
+ name: str,
107
195
  request: Request,
108
- file_group_manager = Depends(get_file_group_manager)
196
+ project_path: str = Depends(get_project_path)
109
197
  ):
110
198
  data = await request.json()
111
199
  files = data.get("files", [])
112
- group = await file_group_manager.remove_files_from_group(name, files)
113
- return group
200
+ await asyncio.to_thread(_remove_file_from_group, project_path, name, files)
201
+ return {"status": "success", "message": f"Removed files from group: {name}"}
202
+
114
203
 
115
204
  @router.get("/api/file-groups")
116
205
  async def get_file_groups(
117
- file_group_manager = Depends(get_file_group_manager)
206
+ project_path: str = Depends(get_project_path)
118
207
  ):
119
- groups = await file_group_manager.get_groups()
120
- return {"groups": groups}
208
+ groups = await asyncio.to_thread(_get_groups, project_path)
209
+ return {"groups": groups}
@@ -37,6 +37,7 @@ async def get_files(
37
37
  project_path: str = Depends(get_project_path)
38
38
  ):
39
39
  tree = get_directory_tree(project_path)
40
+ # print(tree)
40
41
  return {"tree": tree}
41
42
 
42
43
  @router.put("/api/file/{path:path}")
auto_coder_web/lang.py ADDED
@@ -0,0 +1,35 @@
1
+ import locale
2
+ from byzerllm.utils import format_str_jinja2
3
+
4
+ MESSAGES = {
5
+ "project_not_initialized": {
6
+ "en":"Warning: Project not initialized.",
7
+ "zh":"警告:项目未初始化。"
8
+ },
9
+ "run_auto_coder_chat": {
10
+ "en":"Please run 'auto-coder.chat' to initialize the project first.",
11
+ "zh":"请先运行 'auto-coder.chat' 来初始化项目。"
12
+ },
13
+ "human_as_model_warning": {
14
+ "en":"Warning: Project is configured to use human as model, auto-coder.web will not work, we will set human_as_model to false",
15
+ "zh":"警告:项目配置为使用人类作为模型,auto-coder.web 将无法工作,我们将设置 human_as_model 为 false"
16
+ }
17
+ }
18
+
19
+
20
+ def get_system_language():
21
+ try:
22
+ return locale.getdefaultlocale()[0][:2]
23
+ except:
24
+ return 'en'
25
+
26
+
27
+ def get_message(key):
28
+ lang = get_system_language()
29
+ if key in MESSAGES:
30
+ return MESSAGES[key].get(lang, MESSAGES[key].get("en", ""))
31
+ return ""
32
+
33
+
34
+ def get_message_with_format(msg_key: str, **kwargs):
35
+ return format_str_jinja2(get_message(msg_key), **kwargs)