jarvis-ai-assistant 0.1.131__py3-none-any.whl → 0.1.134__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 jarvis-ai-assistant might be problematic. Click here for more details.
- jarvis/__init__.py +1 -1
- jarvis/jarvis_agent/__init__.py +165 -285
- jarvis/jarvis_agent/jarvis.py +143 -0
- jarvis/jarvis_agent/main.py +0 -2
- jarvis/jarvis_agent/patch.py +70 -48
- jarvis/jarvis_agent/shell_input_handler.py +1 -1
- jarvis/jarvis_code_agent/code_agent.py +169 -117
- jarvis/jarvis_dev/main.py +327 -626
- jarvis/jarvis_git_squash/main.py +10 -31
- jarvis/jarvis_lsp/base.py +0 -42
- jarvis/jarvis_lsp/cpp.py +0 -15
- jarvis/jarvis_lsp/go.py +0 -15
- jarvis/jarvis_lsp/python.py +0 -19
- jarvis/jarvis_lsp/registry.py +0 -62
- jarvis/jarvis_lsp/rust.py +0 -15
- jarvis/jarvis_multi_agent/__init__.py +19 -69
- jarvis/jarvis_multi_agent/main.py +43 -0
- jarvis/jarvis_platform/ai8.py +7 -32
- jarvis/jarvis_platform/base.py +2 -7
- jarvis/jarvis_platform/kimi.py +3 -144
- jarvis/jarvis_platform/ollama.py +54 -68
- jarvis/jarvis_platform/openai.py +0 -4
- jarvis/jarvis_platform/oyi.py +0 -75
- jarvis/jarvis_platform/registry.py +2 -16
- jarvis/jarvis_platform/yuanbao.py +264 -0
- jarvis/jarvis_rag/file_processors.py +138 -0
- jarvis/jarvis_rag/main.py +1305 -425
- jarvis/jarvis_tools/ask_codebase.py +216 -43
- jarvis/jarvis_tools/code_review.py +158 -113
- jarvis/jarvis_tools/create_sub_agent.py +0 -1
- jarvis/jarvis_tools/execute_python_script.py +58 -0
- jarvis/jarvis_tools/execute_shell.py +13 -26
- jarvis/jarvis_tools/execute_shell_script.py +1 -1
- jarvis/jarvis_tools/file_analyzer.py +282 -0
- jarvis/jarvis_tools/file_operation.py +1 -1
- jarvis/jarvis_tools/find_caller.py +278 -0
- jarvis/jarvis_tools/find_symbol.py +295 -0
- jarvis/jarvis_tools/function_analyzer.py +331 -0
- jarvis/jarvis_tools/git_commiter.py +5 -5
- jarvis/jarvis_tools/methodology.py +88 -53
- jarvis/jarvis_tools/project_analyzer.py +308 -0
- jarvis/jarvis_tools/rag.py +0 -5
- jarvis/jarvis_tools/read_code.py +24 -3
- jarvis/jarvis_tools/read_webpage.py +195 -81
- jarvis/jarvis_tools/registry.py +132 -11
- jarvis/jarvis_tools/search_web.py +22 -307
- jarvis/jarvis_tools/tool_generator.py +8 -10
- jarvis/jarvis_utils/__init__.py +1 -0
- jarvis/jarvis_utils/config.py +80 -76
- jarvis/jarvis_utils/embedding.py +344 -45
- jarvis/jarvis_utils/git_utils.py +9 -1
- jarvis/jarvis_utils/input.py +7 -6
- jarvis/jarvis_utils/methodology.py +384 -15
- jarvis/jarvis_utils/output.py +5 -3
- jarvis/jarvis_utils/utils.py +60 -8
- {jarvis_ai_assistant-0.1.131.dist-info → jarvis_ai_assistant-0.1.134.dist-info}/METADATA +8 -16
- jarvis_ai_assistant-0.1.134.dist-info/RECORD +82 -0
- {jarvis_ai_assistant-0.1.131.dist-info → jarvis_ai_assistant-0.1.134.dist-info}/entry_points.txt +4 -3
- jarvis/jarvis_codebase/__init__.py +0 -0
- jarvis/jarvis_codebase/main.py +0 -1011
- jarvis/jarvis_tools/lsp_find_definition.py +0 -150
- jarvis/jarvis_tools/lsp_find_references.py +0 -127
- jarvis/jarvis_tools/treesitter_analyzer.py +0 -331
- jarvis/jarvis_treesitter/README.md +0 -104
- jarvis/jarvis_treesitter/__init__.py +0 -20
- jarvis/jarvis_treesitter/database.py +0 -258
- jarvis/jarvis_treesitter/example.py +0 -115
- jarvis/jarvis_treesitter/grammar_builder.py +0 -182
- jarvis/jarvis_treesitter/language.py +0 -117
- jarvis/jarvis_treesitter/symbol.py +0 -31
- jarvis/jarvis_treesitter/tools_usage.md +0 -121
- jarvis_ai_assistant-0.1.131.dist-info/RECORD +0 -85
- {jarvis_ai_assistant-0.1.131.dist-info → jarvis_ai_assistant-0.1.134.dist-info}/LICENSE +0 -0
- {jarvis_ai_assistant-0.1.131.dist-info → jarvis_ai_assistant-0.1.134.dist-info}/WHEEL +0 -0
- {jarvis_ai_assistant-0.1.131.dist-info → jarvis_ai_assistant-0.1.134.dist-info}/top_level.txt +0 -0
|
@@ -1,28 +1,35 @@
|
|
|
1
1
|
from typing import Dict, Any
|
|
2
2
|
import os
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
from jarvis.
|
|
6
|
-
from jarvis.
|
|
7
|
-
from jarvis.jarvis_utils.git_utils import find_git_root
|
|
4
|
+
|
|
5
|
+
from jarvis.jarvis_agent import Agent
|
|
6
|
+
from jarvis.jarvis_platform.registry import PlatformRegistry
|
|
8
7
|
from jarvis.jarvis_utils.output import OutputType, PrettyOutput
|
|
8
|
+
from jarvis.jarvis_utils.git_utils import find_git_root
|
|
9
|
+
from jarvis.jarvis_utils.utils import init_env
|
|
9
10
|
|
|
10
11
|
class AskCodebaseTool:
|
|
11
|
-
"""用于智能代码库查询和分析的工具
|
|
12
|
+
"""用于智能代码库查询和分析的工具
|
|
13
|
+
|
|
14
|
+
适用场景:
|
|
15
|
+
- 查询特定功能所在的文件位置(支持所有文件类型)
|
|
16
|
+
- 了解单个功能点的实现原理(支持所有文件类型)
|
|
17
|
+
- 查找特定API或接口的用法(支持所有文件类型)
|
|
18
|
+
|
|
19
|
+
不适用场景:
|
|
20
|
+
- 跨越多文件的大范围分析
|
|
21
|
+
- 复杂系统架构的全面评估
|
|
22
|
+
- 需要深入上下文理解的代码重构
|
|
23
|
+
"""
|
|
12
24
|
|
|
13
25
|
name = "ask_codebase"
|
|
14
|
-
description = "
|
|
26
|
+
description = "查询代码库中特定功能的位置和实现原理,适合定位功能所在文件和理解单点实现,不适合跨文件大范围分析"
|
|
15
27
|
parameters = {
|
|
16
28
|
"type": "object",
|
|
17
29
|
"properties": {
|
|
18
30
|
"question": {
|
|
19
31
|
"type": "string",
|
|
20
|
-
"description": "
|
|
21
|
-
},
|
|
22
|
-
"top_k": {
|
|
23
|
-
"type": "integer",
|
|
24
|
-
"description": "要分析的最相关文件数量(可选)",
|
|
25
|
-
"default": 20
|
|
32
|
+
"description": "关于代码库的问题,例如'登录功能在哪个文件实现?'或'如何实现了JWT验证?'"
|
|
26
33
|
},
|
|
27
34
|
"root_dir": {
|
|
28
35
|
"type": "string",
|
|
@@ -33,27 +40,31 @@ class AskCodebaseTool:
|
|
|
33
40
|
"required": ["question"]
|
|
34
41
|
}
|
|
35
42
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return not dont_use_local_model()
|
|
43
|
+
def __init__(self, auto_complete: bool = True):
|
|
44
|
+
self.auto_complete = auto_complete
|
|
39
45
|
|
|
40
46
|
def execute(self, args: Dict[str, Any]) -> Dict[str, Any]:
|
|
41
|
-
"""Execute codebase analysis using
|
|
47
|
+
"""Execute codebase analysis using an Agent with execute_shell and rag tools
|
|
42
48
|
|
|
43
49
|
Args:
|
|
44
50
|
args: Dictionary containing:
|
|
45
|
-
- question: The question to answer
|
|
51
|
+
- question: The question to answer, preferably about locating functionality
|
|
52
|
+
or understanding implementation details of a specific feature
|
|
46
53
|
- top_k: Optional number of files to analyze
|
|
54
|
+
- root_dir: Optional root directory of the codebase
|
|
47
55
|
|
|
48
56
|
Returns:
|
|
49
57
|
Dict containing:
|
|
50
58
|
- success: Boolean indicating success
|
|
51
59
|
- stdout: Analysis result
|
|
52
60
|
- stderr: Error message if any
|
|
61
|
+
|
|
62
|
+
Note:
|
|
63
|
+
This tool works best for focused questions about specific features or implementations.
|
|
64
|
+
It is not designed for comprehensive multi-file analysis or complex architectural questions.
|
|
53
65
|
"""
|
|
54
66
|
try:
|
|
55
67
|
question = args["question"]
|
|
56
|
-
top_k = args.get("top_k", 20)
|
|
57
68
|
root_dir = args.get("root_dir", ".")
|
|
58
69
|
|
|
59
70
|
# Store current directory
|
|
@@ -63,25 +74,39 @@ class AskCodebaseTool:
|
|
|
63
74
|
# Change to root_dir
|
|
64
75
|
os.chdir(root_dir)
|
|
65
76
|
|
|
66
|
-
#
|
|
67
|
-
git_root = find_git_root()
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
77
|
+
# Get git root directory
|
|
78
|
+
git_root = find_git_root() or os.getcwd()
|
|
79
|
+
|
|
80
|
+
# Create system prompt for the Agent
|
|
81
|
+
system_prompt = self._create_system_prompt(question, git_root)
|
|
82
|
+
|
|
83
|
+
# Create summary prompt for the Agent
|
|
84
|
+
summary_prompt = self._create_summary_prompt(question)
|
|
85
|
+
|
|
86
|
+
# Create tools registry
|
|
87
|
+
from jarvis.jarvis_tools.registry import ToolRegistry
|
|
88
|
+
tool_registry = ToolRegistry()
|
|
89
|
+
tool_registry.use_tools(["execute_shell", "read_code", "rag"])
|
|
72
90
|
|
|
73
|
-
#
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
91
|
+
# Create and run Agent
|
|
92
|
+
analyzer_agent = Agent(
|
|
93
|
+
system_prompt=system_prompt,
|
|
94
|
+
name=f"CodebaseAnalyzer",
|
|
95
|
+
description=f"分析代码库中的功能实现和定位",
|
|
96
|
+
summary_prompt=summary_prompt,
|
|
97
|
+
platform=PlatformRegistry().get_normal_platform(),
|
|
98
|
+
output_handler=[tool_registry],
|
|
99
|
+
execute_tool_confirm=False,
|
|
100
|
+
auto_complete=self.auto_complete
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
# Run agent and get result
|
|
104
|
+
task_input = f"回答关于代码库的问题: {question}"
|
|
105
|
+
result = analyzer_agent.run(task_input)
|
|
81
106
|
|
|
82
107
|
return {
|
|
83
108
|
"success": True,
|
|
84
|
-
"stdout":
|
|
109
|
+
"stdout": result,
|
|
85
110
|
"stderr": ""
|
|
86
111
|
}
|
|
87
112
|
finally:
|
|
@@ -95,28 +120,176 @@ class AskCodebaseTool:
|
|
|
95
120
|
"stdout": "",
|
|
96
121
|
"stderr": error_msg
|
|
97
122
|
}
|
|
123
|
+
|
|
124
|
+
def _create_system_prompt(self, question: str, git_root: str) -> str:
|
|
125
|
+
"""创建Agent的system prompt
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
question: 用户问题
|
|
129
|
+
git_root: Git仓库根目录
|
|
130
|
+
|
|
131
|
+
Returns:
|
|
132
|
+
系统提示文本
|
|
133
|
+
"""
|
|
134
|
+
return f"""# 代码库分析专家
|
|
135
|
+
|
|
136
|
+
## 任务描述
|
|
137
|
+
分析代码库,找出与用户问题最相关的信息,提供准确、具体的回答。
|
|
138
|
+
|
|
139
|
+
## 问题信息
|
|
140
|
+
- 问题: {question}
|
|
141
|
+
- 代码库根目录: {git_root}
|
|
142
|
+
|
|
143
|
+
## 工具使用优先级
|
|
144
|
+
1. **绝对优先使用 execute_shell**:
|
|
145
|
+
- 使用 fd 查找文件: `fd -t f -e py` 查找Python文件等
|
|
146
|
+
- 使用 rg 搜索代码: `rg "pattern" --type py` 在Python文件中搜索等
|
|
147
|
+
- 使用 loc 统计代码: `loc --include="*.py"` 统计Python代码量等
|
|
148
|
+
|
|
149
|
+
2. **优先使用 read_code**:
|
|
150
|
+
- 找到相关文件后优先使用read_code读取文件内容
|
|
151
|
+
- 对大文件使用行范围参数读取指定区域
|
|
152
|
+
|
|
153
|
+
3. **避免使用 rag**:
|
|
154
|
+
- 仅在fd、rg和read_code无法解决问题时作为最后手段
|
|
155
|
+
- 使用rag前必须先尝试使用shell命令解决问题
|
|
156
|
+
|
|
157
|
+
## 分析策略
|
|
158
|
+
1. 首先理解问题,确定需要查找的关键信息和代码组件
|
|
159
|
+
2. 使用fd命令查找可能相关的文件
|
|
160
|
+
3. 使用rg命令搜索关键词和代码模式
|
|
161
|
+
4. 使用read_code工具直接读取和分析相关文件内容
|
|
162
|
+
5. 只有在fd、rg和read_code都无法解决问题时才考虑使用RAG工具
|
|
163
|
+
6. 根据文件内容提供具体、准确的回答
|
|
164
|
+
7. 确保分析的完整性,收集充分的信息后再得出结论
|
|
165
|
+
8. 优先查阅README文件、文档目录和项目文档
|
|
166
|
+
|
|
167
|
+
## 分析步骤
|
|
168
|
+
1. **确定项目的编程语言**:
|
|
169
|
+
- 使用loc命令: `loc` 统计各类文件数量
|
|
170
|
+
- 查看构建文件和配置文件
|
|
171
|
+
- 了解项目使用的框架和主要依赖
|
|
172
|
+
|
|
173
|
+
2. **探索代码库结构**:
|
|
174
|
+
- 使用 `fd -t d` 了解目录结构
|
|
175
|
+
- 使用 `fd README.md` 查找README文件
|
|
176
|
+
- 使用 `fd -e md -g "doc*"` 查找文档文件
|
|
177
|
+
|
|
178
|
+
3. **定位相关文件**:
|
|
179
|
+
- 使用fd和rg命令查找关键词
|
|
180
|
+
- 示例: `rg -w "登录|login" --type py` 查找登录相关代码
|
|
181
|
+
- 示例: `fd -e py -g "*auth*"` 查找认证相关文件
|
|
182
|
+
|
|
183
|
+
4. **深入分析代码**:
|
|
184
|
+
- 使用read_code工具直接读取文件内容
|
|
185
|
+
- 分析关键文件的实现细节
|
|
186
|
+
- 使用rg识别功能的实现方式和关键逻辑
|
|
187
|
+
|
|
188
|
+
5. **回答问题**:
|
|
189
|
+
- 提供基于直接分析代码的具体回答
|
|
190
|
+
- 引用具体文件和代码片段作为依据
|
|
191
|
+
|
|
192
|
+
## 关于RAG工具使用
|
|
193
|
+
- RAG工具应作为最后选择,仅在fd、rg和read_code都无法解决问题时使用
|
|
194
|
+
- 必须通过查看实际代码文件验证RAG返回的每条重要信息
|
|
195
|
+
- 对于关键发现,始终使用`read_code`工具查看原始文件内容进行求证
|
|
196
|
+
- 如发现RAG结果与实际代码不符,以实际代码为准
|
|
197
|
+
|
|
198
|
+
## 输出要求
|
|
199
|
+
- 提供准确、具体的回答,避免模糊不清的描述
|
|
200
|
+
- 引用具体文件路径和代码片段作为依据
|
|
201
|
+
- 如果无法找到答案,明确说明并提供原因
|
|
202
|
+
- 组织信息的逻辑清晰,便于理解
|
|
203
|
+
- 对复杂概念提供简明解释
|
|
204
|
+
- 确保全面收集相关信息后再形成结论
|
|
205
|
+
- 明确区分已验证的信息和待验证的信息"""
|
|
206
|
+
|
|
207
|
+
def _create_summary_prompt(self, question: str) -> str:
|
|
208
|
+
"""创建Agent的summary prompt
|
|
209
|
+
|
|
210
|
+
Args:
|
|
211
|
+
question: 用户问题
|
|
212
|
+
|
|
213
|
+
Returns:
|
|
214
|
+
总结提示文本
|
|
215
|
+
"""
|
|
216
|
+
return f"""# 代码库分析报告
|
|
217
|
+
|
|
218
|
+
## 报告要求
|
|
219
|
+
生成关于以下问题的清晰、准确的分析报告:
|
|
220
|
+
|
|
221
|
+
**问题**: {question}
|
|
222
|
+
|
|
223
|
+
报告应包含:
|
|
224
|
+
|
|
225
|
+
1. **项目基本信息**:
|
|
226
|
+
- 项目的主要编程语言和技术栈
|
|
227
|
+
- 项目的主要框架和依赖
|
|
228
|
+
- 项目的基本结构
|
|
229
|
+
|
|
230
|
+
2. **问题回答**:
|
|
231
|
+
- 直接、具体地回答问题
|
|
232
|
+
- 基于代码库中的实际代码
|
|
233
|
+
- 避免模糊或推测性的回答
|
|
234
|
+
|
|
235
|
+
3. **核心发现**:
|
|
236
|
+
- 相关文件和代码位置
|
|
237
|
+
- 关键实现细节
|
|
238
|
+
- 功能运作机制
|
|
239
|
+
|
|
240
|
+
4. **证据引用**:
|
|
241
|
+
- 引用具体文件路径
|
|
242
|
+
- 包含关键代码片段
|
|
243
|
+
- 解释代码如何支持你的回答
|
|
244
|
+
|
|
245
|
+
5. **局限性**(如有):
|
|
246
|
+
- 指出分析的任何局限
|
|
247
|
+
- 说明任何无法确定的信息
|
|
248
|
+
|
|
249
|
+
6. **分析完整性**:
|
|
250
|
+
- 确保在得出结论前已全面收集和分析相关信息
|
|
251
|
+
- 避免基于部分信息形成不完整或偏颇的判断
|
|
252
|
+
- 明确标识哪些是已确认的结论,哪些可能需要进一步验证
|
|
253
|
+
|
|
254
|
+
使用清晰的Markdown格式,重点突出对问题的回答和支持证据。"""
|
|
255
|
+
|
|
256
|
+
|
|
98
257
|
def main():
|
|
99
|
-
"""
|
|
258
|
+
"""
|
|
259
|
+
命令行入口点,允许将ask_codebase作为独立脚本运行
|
|
260
|
+
|
|
261
|
+
用法示例:
|
|
262
|
+
```
|
|
263
|
+
python -m jarvis.jarvis_tools.ask_codebase "登录功能在哪个文件实现?" --root_dir /path/to/codebase
|
|
264
|
+
```
|
|
265
|
+
"""
|
|
100
266
|
import argparse
|
|
267
|
+
import sys
|
|
268
|
+
|
|
269
|
+
init_env()
|
|
101
270
|
|
|
102
|
-
|
|
103
|
-
parser.
|
|
104
|
-
parser.add_argument(
|
|
105
|
-
parser.add_argument(
|
|
271
|
+
# 创建命令行参数解析器
|
|
272
|
+
parser = argparse.ArgumentParser(description="智能代码库查询工具")
|
|
273
|
+
parser.add_argument("question", help="关于代码库的问题")
|
|
274
|
+
parser.add_argument("--root_dir", "-d", default=".", help="代码库根目录路径")
|
|
106
275
|
|
|
276
|
+
# 解析命令行参数
|
|
107
277
|
args = parser.parse_args()
|
|
108
|
-
|
|
278
|
+
|
|
279
|
+
# 创建并执行工具
|
|
280
|
+
tool = AskCodebaseTool(auto_complete=False)
|
|
109
281
|
result = tool.execute({
|
|
110
282
|
"question": args.question,
|
|
111
|
-
"top_k": args.top_k,
|
|
112
283
|
"root_dir": args.root_dir
|
|
113
284
|
})
|
|
114
285
|
|
|
286
|
+
# 输出结果
|
|
115
287
|
if result["success"]:
|
|
116
|
-
PrettyOutput.print(result["stdout"], OutputType.
|
|
288
|
+
PrettyOutput.print(result["stdout"], OutputType.SUCCESS)
|
|
117
289
|
else:
|
|
118
290
|
PrettyOutput.print(result["stderr"], OutputType.WARNING)
|
|
119
|
-
|
|
291
|
+
sys.exit(1)
|
|
292
|
+
|
|
120
293
|
|
|
121
294
|
if __name__ == "__main__":
|
|
122
|
-
main()
|
|
295
|
+
main()
|
|
@@ -4,12 +4,13 @@ import os
|
|
|
4
4
|
|
|
5
5
|
from yaspin import yaspin
|
|
6
6
|
from jarvis.jarvis_platform.registry import PlatformRegistry
|
|
7
|
+
from jarvis.jarvis_tools.read_code import ReadCodeTool
|
|
7
8
|
from jarvis.jarvis_tools.registry import ToolRegistry
|
|
8
9
|
from jarvis.jarvis_agent import Agent
|
|
9
10
|
import re
|
|
10
11
|
|
|
11
12
|
from jarvis.jarvis_utils.output import OutputType, PrettyOutput
|
|
12
|
-
from jarvis.jarvis_utils.utils import init_env
|
|
13
|
+
from jarvis.jarvis_utils.utils import ct, ot, init_env
|
|
13
14
|
|
|
14
15
|
class CodeReviewTool:
|
|
15
16
|
name = "code_review"
|
|
@@ -89,7 +90,7 @@ class CodeReviewTool:
|
|
|
89
90
|
"stderr": "file_path is required for file review type"
|
|
90
91
|
}
|
|
91
92
|
file_path = args["file_path"].strip()
|
|
92
|
-
diff_cmd =
|
|
93
|
+
diff_cmd = ReadCodeTool().execute({"files": [{"path": file_path}]})["stdout"]
|
|
93
94
|
else: # current changes
|
|
94
95
|
diff_cmd = "git diff HEAD | cat -"
|
|
95
96
|
|
|
@@ -112,102 +113,139 @@ class CodeReviewTool:
|
|
|
112
113
|
spinner.text = "代码变更获取完成"
|
|
113
114
|
spinner.ok("✅")
|
|
114
115
|
|
|
115
|
-
system_prompt = """
|
|
116
|
-
|
|
117
|
-
#
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
-
|
|
131
|
-
-
|
|
132
|
-
-
|
|
133
|
-
-
|
|
134
|
-
-
|
|
135
|
-
|
|
136
|
-
#
|
|
137
|
-
1.
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
-
|
|
177
|
-
-
|
|
178
|
-
-
|
|
179
|
-
|
|
180
|
-
-
|
|
181
|
-
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
116
|
+
system_prompt = """你是一位精益求精的首席代码审查专家,拥有多年企业级代码审计经验。你需要对所有代码变更进行极其全面、严谨且深入的审查,确保代码质量达到最高标准。
|
|
117
|
+
|
|
118
|
+
# 代码审查工具选择
|
|
119
|
+
优先使用执行shell命令进行静态分析,而非依赖内置代码审查功能:
|
|
120
|
+
|
|
121
|
+
| 分析需求 | 首选工具 | 备选工具 |
|
|
122
|
+
|---------|---------|----------|
|
|
123
|
+
| 代码质量检查 | execute_shell | - |
|
|
124
|
+
| 语法检查 | 语言特定lint工具 | - |
|
|
125
|
+
| 安全分析 | 安全扫描工具 | - |
|
|
126
|
+
| 代码统计 | loc | - |
|
|
127
|
+
|
|
128
|
+
# 推荐命令
|
|
129
|
+
- Python: `pylint <file_path>`, `flake8 <file_path>`, `mypy <file_path>`
|
|
130
|
+
- JavaScript/TypeScript: `eslint <file_path>`, `tsc --noEmit <file_path>`
|
|
131
|
+
- Java: `checkstyle <file_path>`, `pmd -d <file_path>`
|
|
132
|
+
- C/C++: `cppcheck <file_path>`, `clang-tidy <file_path>`
|
|
133
|
+
- Go: `golint <file_path>`, `go vet <file_path>`
|
|
134
|
+
- Rust: `cargo clippy`, `rustfmt --check <file_path>`
|
|
135
|
+
- 通用搜索:`rg "pattern" <files>` 查找特定代码模式
|
|
136
|
+
|
|
137
|
+
# 专家审查标准
|
|
138
|
+
1. 必须逐行分析每个修改文件,细致审查每一处变更,不遗漏任何细节
|
|
139
|
+
2. 基于坚实的证据识别问题,不做主观臆测,给出明确的问题定位和详细分析
|
|
140
|
+
3. 对每个问题提供完整可执行的解决方案,包括精确的改进代码
|
|
141
|
+
4. 确保报告条理清晰、层次分明,便于工程师快速采取行动
|
|
142
|
+
|
|
143
|
+
# 全面审查框架 (SCRIPPPS)
|
|
144
|
+
## S - 安全与风险 (Security & Risk)
|
|
145
|
+
- 发现所有潜在安全漏洞:注入攻击、授权缺陷、数据泄露风险
|
|
146
|
+
- 检查加密实现、密钥管理、敏感数据处理
|
|
147
|
+
- 审核权限验证逻辑、身份认证机制
|
|
148
|
+
- 检测OWASP Top 10安全风险和针对特定语言/框架的漏洞
|
|
149
|
+
|
|
150
|
+
## C - 正确性与完整性 (Correctness & Completeness)
|
|
151
|
+
- 验证业务逻辑和算法实现的准确性
|
|
152
|
+
- 全面检查条件边界、空值处理和异常情况
|
|
153
|
+
- 审核所有输入验证、参数校验和返回值处理
|
|
154
|
+
- 确保循环和递归的正确终止条件
|
|
155
|
+
- 严格检查线程安全和并发控制机制
|
|
156
|
+
|
|
157
|
+
## R - 可靠性与鲁棒性 (Reliability & Robustness)
|
|
158
|
+
- 评估代码在异常情况下的行为和恢复能力
|
|
159
|
+
- 审查错误处理、异常捕获和恢复策略
|
|
160
|
+
- 检查资源管理:内存、文件句柄、连接池、线程
|
|
161
|
+
- 评估容错设计和失败优雅降级机制
|
|
162
|
+
|
|
163
|
+
## I - 接口与集成 (Interface & Integration)
|
|
164
|
+
- 检查API合约遵守情况和向后兼容性
|
|
165
|
+
- 审核与外部系统的集成点和交互逻辑
|
|
166
|
+
- 验证数据格式、序列化和协议实现
|
|
167
|
+
- 评估系统边界处理和跨服务通信安全性
|
|
168
|
+
|
|
169
|
+
## P - 性能与效率 (Performance & Efficiency)
|
|
170
|
+
- 识别潜在性能瓶颈:CPU、内存、I/O、网络
|
|
171
|
+
- 审查数据结构选择和算法复杂度
|
|
172
|
+
- 检查资源密集型操作、数据库查询优化
|
|
173
|
+
- 评估缓存策略、批处理优化和并行处理机会
|
|
174
|
+
|
|
175
|
+
## P - 可移植性与平台适配 (Portability & Platform Compatibility)
|
|
176
|
+
- 检查跨平台兼容性问题和依赖项管理
|
|
177
|
+
- 评估配置管理和环境适配设计
|
|
178
|
+
- 审核国际化和本地化支持
|
|
179
|
+
- 验证部署和运行时环境需求
|
|
180
|
+
|
|
181
|
+
## S - 结构与可维护性 (Structure & Maintainability)
|
|
182
|
+
- 评估代码组织、模块划分和架构符合性
|
|
183
|
+
- 审查代码重复、设计模式应用和抽象水平
|
|
184
|
+
- 检查命名规范、代码风格和项目约定
|
|
185
|
+
- 评估文档完整性、注释质量和代码可读性
|
|
186
|
+
|
|
187
|
+
# 问题严重程度分级
|
|
188
|
+
- 严重 (P0): 安全漏洞、数据丢失风险、系统崩溃、功能严重缺陷
|
|
189
|
+
- 高危 (P1): 显著性能问题、可能导致部分功能失效、系统不稳定
|
|
190
|
+
- 中等 (P2): 功能局部缺陷、次优设计、明显的技术债务
|
|
191
|
+
- 低危 (P3): 代码风格问题、轻微优化机会、文档改进建议
|
|
192
|
+
|
|
193
|
+
# 输出规范
|
|
194
|
+
针对每个文件的问题必须包含:
|
|
195
|
+
- 精确文件路径和问题影响范围
|
|
196
|
+
- 问题位置(起始行号-结束行号)
|
|
197
|
+
- 详尽问题描述,包括具体影响和潜在风险
|
|
198
|
+
- 严重程度分级(P0-P3)并说明理由
|
|
199
|
+
- 具体改进建议,提供完整、可执行的代码示例
|
|
200
|
+
|
|
201
|
+
所有审查发现必须:
|
|
202
|
+
1. 基于确凿的代码证据
|
|
203
|
+
2. 说明具体问题而非笼统评论
|
|
204
|
+
3. 提供清晰的技术原理分析
|
|
205
|
+
4. 给出完整的改进实施步骤"""
|
|
206
|
+
|
|
197
207
|
tool_registry = ToolRegistry()
|
|
198
208
|
tool_registry.dont_use_tools(["code_review"])
|
|
199
209
|
agent = Agent(
|
|
200
210
|
system_prompt=system_prompt,
|
|
201
211
|
name="Code Review Agent",
|
|
202
|
-
summary_prompt="""
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
212
|
+
summary_prompt=f"""请生成一份专业级别的代码审查报告,对每处变更进行全面深入分析。将完整报告放在REPORT标签内,格式如下:
|
|
213
|
+
|
|
214
|
+
{ot("REPORT")}
|
|
215
|
+
# 整体评估
|
|
216
|
+
[提供对整体代码质量、架构和主要关注点的简明概述,总结主要发现]
|
|
217
|
+
|
|
218
|
+
# 详细问题清单
|
|
219
|
+
|
|
220
|
+
## 文件: [文件路径]
|
|
221
|
+
[如果该文件没有发现问题,则明确说明"未发现问题"]
|
|
222
|
+
|
|
223
|
+
### 问题 1
|
|
224
|
+
- **位置**: [起始行号-结束行号]
|
|
225
|
+
- **分类**: [使用SCRIPPPS框架中相关类别]
|
|
226
|
+
- **严重程度**: [P0/P1/P2/P3] - [简要说明判定理由]
|
|
227
|
+
- **问题描述**:
|
|
228
|
+
[详细描述问题,包括技术原理和潜在影响]
|
|
229
|
+
- **改进建议**:
|
|
230
|
+
```
|
|
231
|
+
[提供完整、可执行的代码示例,而非概念性建议]
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### 问题 2
|
|
235
|
+
...
|
|
236
|
+
|
|
237
|
+
## 文件: [文件路径2]
|
|
238
|
+
...
|
|
239
|
+
|
|
240
|
+
# 最佳实践建议
|
|
241
|
+
[提供适用于整个代码库的改进建议和最佳实践]
|
|
242
|
+
|
|
243
|
+
# 总结
|
|
244
|
+
[总结主要问题和优先处理建议]
|
|
245
|
+
{ct("REPORT")}
|
|
246
|
+
|
|
247
|
+
如果没有发现任何问题,请在REPORT标签内进行全面分析后明确说明"经过全面审查,未发现问题"并解释原因。
|
|
248
|
+
必须确保对所有修改的文件都进行了审查,并在报告中明确提及每个文件,即使某些文件没有发现问题。""",
|
|
211
249
|
output_handler=[tool_registry],
|
|
212
250
|
platform=PlatformRegistry().get_thinking_platform(),
|
|
213
251
|
auto_complete=True
|
|
@@ -231,7 +269,7 @@ class CodeReviewTool:
|
|
|
231
269
|
|
|
232
270
|
|
|
233
271
|
def extract_code_report(result: str) -> str:
|
|
234
|
-
sm = re.search(
|
|
272
|
+
sm = re.search(ot("REPORT")+r'\n(.*?)\n'+ct("REPORT"), result, re.DOTALL)
|
|
235
273
|
if sm:
|
|
236
274
|
return sm.group(1)
|
|
237
275
|
return ""
|
|
@@ -243,35 +281,42 @@ def main():
|
|
|
243
281
|
init_env()
|
|
244
282
|
|
|
245
283
|
parser = argparse.ArgumentParser(description='Autonomous code review tool')
|
|
246
|
-
parser.
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
284
|
+
subparsers = parser.add_subparsers(dest='type')
|
|
285
|
+
|
|
286
|
+
# Commit subcommand
|
|
287
|
+
commit_parser = subparsers.add_parser('commit', help='Review specific commit')
|
|
288
|
+
commit_parser.add_argument('commit', help='Commit SHA to review')
|
|
289
|
+
|
|
290
|
+
# Current subcommand
|
|
291
|
+
current_parser = subparsers.add_parser('current', help='Review current changes')
|
|
292
|
+
|
|
293
|
+
# Range subcommand
|
|
294
|
+
range_parser = subparsers.add_parser('range', help='Review commit range')
|
|
295
|
+
range_parser.add_argument('start_commit', help='Start commit SHA')
|
|
296
|
+
range_parser.add_argument('end_commit', help='End commit SHA')
|
|
297
|
+
|
|
298
|
+
# File subcommand
|
|
299
|
+
file_parser = subparsers.add_parser('file', help='Review specific file')
|
|
300
|
+
file_parser.add_argument('file', help='File path to review')
|
|
301
|
+
|
|
302
|
+
# Common arguments
|
|
252
303
|
parser.add_argument('--root-dir', type=str, help='Root directory of the codebase', default=".")
|
|
253
|
-
args = parser.parse_args()
|
|
254
304
|
|
|
255
|
-
#
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
if args.type == 'range' and (not args.start_commit or not args.end_commit):
|
|
259
|
-
parser.error("--start-commit and --end-commit are required when type is 'range'")
|
|
260
|
-
if args.type == 'file' and not args.file:
|
|
261
|
-
parser.error("--file is required when type is 'file'")
|
|
305
|
+
# Set default subcommand to 'current'
|
|
306
|
+
parser.set_defaults(type='current')
|
|
307
|
+
args = parser.parse_args()
|
|
262
308
|
|
|
263
309
|
tool = CodeReviewTool()
|
|
264
310
|
tool_args = {
|
|
265
311
|
"review_type": args.type,
|
|
266
312
|
"root_dir": args.root_dir
|
|
267
313
|
}
|
|
268
|
-
if args.commit:
|
|
314
|
+
if args.type == 'commit':
|
|
269
315
|
tool_args["commit_sha"] = args.commit
|
|
270
|
-
|
|
316
|
+
elif args.type == 'range':
|
|
271
317
|
tool_args["start_commit"] = args.start_commit
|
|
272
|
-
if args.end_commit:
|
|
273
318
|
tool_args["end_commit"] = args.end_commit
|
|
274
|
-
|
|
319
|
+
elif args.type == 'file':
|
|
275
320
|
tool_args["file_path"] = args.file
|
|
276
321
|
|
|
277
322
|
result = tool.execute(tool_args)
|
|
@@ -279,7 +324,7 @@ def main():
|
|
|
279
324
|
if result["success"]:
|
|
280
325
|
PrettyOutput.section("自动代码审查结果:", OutputType.SUCCESS)
|
|
281
326
|
report = extract_code_report(result["stdout"])
|
|
282
|
-
PrettyOutput.print(report, OutputType.SUCCESS, lang="
|
|
327
|
+
PrettyOutput.print(report, OutputType.SUCCESS, lang="markdown")
|
|
283
328
|
|
|
284
329
|
else:
|
|
285
330
|
PrettyOutput.print(result["stderr"], OutputType.WARNING)
|