auto-coder-web 0.1.26__py3-none-any.whl → 0.1.29__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,31 +1,44 @@
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
+ from pydantic import BaseModel
7
+
8
+
9
+ class SessionNameRequest(BaseModel):
10
+ session_name: str
11
+
12
+
13
+ async def get_project_path(request: Request) -> str:
14
+ """
15
+ 从FastAPI请求上下文中获取项目路径
16
+ """
17
+ return request.app.state.project_path
6
18
 
7
19
  router = APIRouter()
8
20
 
21
+
9
22
  @router.post("/api/chat-lists/save")
10
- async def save_chat_list(chat_list: ChatList):
23
+ async def save_chat_list(chat_list: ChatList, project_path: str = Depends(get_project_path)):
11
24
  try:
12
- chat_lists_dir = os.path.join(
13
- ".auto-coder", "auto-coder.web", "chat-lists")
25
+ chat_lists_dir = os.path.join(project_path,
26
+ ".auto-coder", "auto-coder.web", "chat-lists")
14
27
  os.makedirs(chat_lists_dir, exist_ok=True)
15
28
 
16
- file_path = os.path.join(
17
- chat_lists_dir, f"{chat_list.name}.json")
29
+ file_path = os.path.join(chat_lists_dir, f"{chat_list.name}.json")
18
30
  async with aiofiles.open(file_path, 'w') as f:
19
- await f.write(json.dumps({"messages": chat_list.messages}, indent=2))
31
+ await f.write(json.dumps({"messages": chat_list.messages}, indent=2, ensure_ascii=False))
20
32
  return {"status": "success", "message": f"Chat list {chat_list.name} saved successfully"}
21
33
  except Exception as e:
22
34
  raise HTTPException(status_code=500, detail=str(e))
23
35
 
36
+
24
37
  @router.get("/api/chat-lists")
25
- async def get_chat_lists():
38
+ async def get_chat_lists(project_path: str = Depends(get_project_path)):
26
39
  try:
27
40
  chat_lists_dir = os.path.join(
28
- ".auto-coder", "auto-coder.web", "chat-lists")
41
+ project_path, ".auto-coder", "auto-coder.web", "chat-lists")
29
42
  os.makedirs(chat_lists_dir, exist_ok=True)
30
43
 
31
44
  # Get files with their modification times
@@ -45,11 +58,12 @@ async def get_chat_lists():
45
58
  except Exception as e:
46
59
  raise HTTPException(status_code=500, detail=str(e))
47
60
 
61
+
48
62
  @router.get("/api/chat-lists/{name}")
49
- async def get_chat_list(name: str):
63
+ async def get_chat_list(name: str, project_path: str = Depends(get_project_path)):
50
64
  try:
51
65
  file_path = os.path.join(
52
- ".auto-coder", "auto-coder.web", "chat-lists", f"{name}.json")
66
+ project_path, ".auto-coder", "auto-coder.web", "chat-lists", f"{name}.json")
53
67
  if not os.path.exists(file_path):
54
68
  raise HTTPException(
55
69
  status_code=404, detail=f"Chat list {name} not found")
@@ -60,11 +74,12 @@ async def get_chat_list(name: str):
60
74
  except Exception as e:
61
75
  raise HTTPException(status_code=500, detail=str(e))
62
76
 
77
+
63
78
  @router.delete("/api/chat-lists/{name}")
64
- async def delete_chat_list(name: str):
79
+ async def delete_chat_list(name: str, project_path: str = Depends(get_project_path)):
65
80
  try:
66
81
  file_path = os.path.join(
67
- ".auto-coder", "auto-coder.web", "chat-lists", f"{name}.json")
82
+ project_path, ".auto-coder", "auto-coder.web", "chat-lists", f"{name}.json")
68
83
  if not os.path.exists(file_path):
69
84
  raise HTTPException(
70
85
  status_code=404, detail=f"Chat list {name} not found")
@@ -72,4 +87,57 @@ async def delete_chat_list(name: str):
72
87
  os.remove(file_path)
73
88
  return {"status": "success", "message": f"Chat list {name} deleted successfully"}
74
89
  except Exception as e:
75
- raise HTTPException(status_code=500, detail=str(e))
90
+ raise HTTPException(status_code=500, detail=str(e))
91
+
92
+
93
+ @router.get("/api/chat-session/name")
94
+ async def get_current_session_name(project_path: str = Depends(get_project_path)):
95
+ """
96
+ 获取当前会话名称
97
+ """
98
+ try:
99
+ # 创建存储会话信息的目录
100
+ session_dir = os.path.join(project_path, ".auto-coder", "auto-coder.web")
101
+ os.makedirs(session_dir, exist_ok=True)
102
+
103
+ # 会话信息文件路径
104
+ session_file = os.path.join(session_dir, "current-session.json")
105
+
106
+ # 如果文件不存在,返回空会话名称
107
+ if not os.path.exists(session_file):
108
+ return {"session_name": ""}
109
+
110
+ # 读取当前会话信息
111
+ async with aiofiles.open(session_file, 'r') as f:
112
+ content = await f.read()
113
+ session_data = json.loads(content)
114
+ return {"session_name": session_data.get("session_name", "")}
115
+
116
+ except Exception as e:
117
+ # 如果发生错误,记录错误但返回空会话名
118
+ print(f"Error getting current session name: {str(e)}")
119
+ return {"session_name": ""}
120
+
121
+
122
+ @router.post("/api/chat-session/name")
123
+ async def set_current_session_name(request: SessionNameRequest, project_path: str = Depends(get_project_path)):
124
+ """
125
+ 设置当前会话名称
126
+ """
127
+ try:
128
+ # 创建存储会话信息的目录
129
+ session_dir = os.path.join(project_path, ".auto-coder", "auto-coder.web")
130
+ os.makedirs(session_dir, exist_ok=True)
131
+
132
+ # 会话信息文件路径
133
+ session_file = os.path.join(session_dir, "current-session.json")
134
+
135
+ # 保存当前会话信息
136
+ session_data = {"session_name": request.session_name}
137
+ async with aiofiles.open(session_file, 'w') as f:
138
+ await f.write(json.dumps(session_data, indent=2, ensure_ascii=False))
139
+
140
+ return {"status": "success", "message": "Current session name updated"}
141
+
142
+ except Exception as e:
143
+ raise HTTPException(status_code=500, detail=f"Failed to set current session name: {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)
@@ -64,12 +64,20 @@ def _get_groups(project_path: str):
64
64
  ]
65
65
  return v
66
66
 
67
- def _switch_groups(group_names: List[str]):
67
+ def _switch_groups(group_names: List[str], file_paths: List[str] = None):
68
68
  memory = get_memory()
69
69
  new_files = []
70
70
  for group_name in group_names:
71
71
  files = memory["current_files"]["groups"][group_name]
72
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
+
73
81
  memory["current_files"]["files"] = new_files
74
82
  memory["current_files"]["current_groups"] = group_names
75
83
  save_memory()
@@ -139,12 +147,21 @@ async def auto_create_groups(
139
147
 
140
148
  @router.post("/api/file-groups/switch")
141
149
  async def switch_file_groups(
142
- request: Request
150
+ request: Request,
151
+ project_path: str = Depends(get_project_path)
143
152
  ):
144
153
  data = await request.json()
145
154
  group_names = data.get("group_names", [])
146
- await asyncio.to_thread(_switch_groups, group_names)
147
- return {"status": "success", "message": f"Switched to groups: {group_names}"}
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"}
148
165
 
149
166
 
150
167
  @router.delete("/api/file-groups/{name}")
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)