jarvis-ai-assistant 0.1.138__py3-none-any.whl → 0.1.141__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 +62 -14
- jarvis/jarvis_agent/builtin_input_handler.py +4 -14
- jarvis/jarvis_agent/main.py +1 -1
- jarvis/jarvis_agent/patch.py +37 -40
- jarvis/jarvis_agent/shell_input_handler.py +2 -3
- jarvis/jarvis_code_agent/code_agent.py +23 -30
- jarvis/jarvis_code_analysis/checklists/__init__.py +3 -0
- jarvis/jarvis_code_analysis/checklists/c_cpp.py +50 -0
- jarvis/jarvis_code_analysis/checklists/csharp.py +75 -0
- jarvis/jarvis_code_analysis/checklists/data_format.py +82 -0
- jarvis/jarvis_code_analysis/checklists/devops.py +107 -0
- jarvis/jarvis_code_analysis/checklists/docs.py +87 -0
- jarvis/jarvis_code_analysis/checklists/go.py +52 -0
- jarvis/jarvis_code_analysis/checklists/infrastructure.py +98 -0
- jarvis/jarvis_code_analysis/checklists/java.py +66 -0
- jarvis/jarvis_code_analysis/checklists/javascript.py +73 -0
- jarvis/jarvis_code_analysis/checklists/kotlin.py +107 -0
- jarvis/jarvis_code_analysis/checklists/loader.py +76 -0
- jarvis/jarvis_code_analysis/checklists/php.py +77 -0
- jarvis/jarvis_code_analysis/checklists/python.py +56 -0
- jarvis/jarvis_code_analysis/checklists/ruby.py +107 -0
- jarvis/jarvis_code_analysis/checklists/rust.py +58 -0
- jarvis/jarvis_code_analysis/checklists/shell.py +75 -0
- jarvis/jarvis_code_analysis/checklists/sql.py +72 -0
- jarvis/jarvis_code_analysis/checklists/swift.py +77 -0
- jarvis/jarvis_code_analysis/checklists/web.py +97 -0
- jarvis/jarvis_code_analysis/code_review.py +660 -0
- jarvis/jarvis_dev/main.py +61 -88
- jarvis/jarvis_git_squash/main.py +3 -3
- jarvis/jarvis_git_utils/git_commiter.py +242 -0
- jarvis/jarvis_init/main.py +62 -0
- jarvis/jarvis_platform/base.py +4 -0
- jarvis/jarvis_platform/kimi.py +173 -5
- jarvis/jarvis_platform/openai.py +3 -0
- jarvis/jarvis_platform/registry.py +1 -0
- jarvis/jarvis_platform/yuanbao.py +275 -5
- jarvis/jarvis_tools/ask_codebase.py +6 -9
- jarvis/jarvis_tools/ask_user.py +17 -5
- jarvis/jarvis_tools/base.py +3 -1
- jarvis/jarvis_tools/chdir.py +1 -0
- jarvis/jarvis_tools/create_code_agent.py +4 -3
- jarvis/jarvis_tools/create_sub_agent.py +1 -0
- jarvis/jarvis_tools/execute_script.py +170 -0
- jarvis/jarvis_tools/file_analyzer.py +90 -239
- jarvis/jarvis_tools/file_operation.py +99 -31
- jarvis/jarvis_tools/{find_methodolopy.py → find_methodology.py} +2 -1
- jarvis/jarvis_tools/lsp_get_diagnostics.py +2 -0
- jarvis/jarvis_tools/methodology.py +11 -11
- jarvis/jarvis_tools/read_code.py +2 -0
- jarvis/jarvis_tools/read_webpage.py +33 -196
- jarvis/jarvis_tools/registry.py +68 -131
- jarvis/jarvis_tools/search_web.py +14 -6
- jarvis/jarvis_tools/virtual_tty.py +399 -0
- jarvis/jarvis_utils/config.py +29 -3
- jarvis/jarvis_utils/embedding.py +0 -317
- jarvis/jarvis_utils/file_processors.py +343 -0
- jarvis/jarvis_utils/input.py +0 -1
- jarvis/jarvis_utils/methodology.py +94 -435
- jarvis/jarvis_utils/utils.py +207 -9
- {jarvis_ai_assistant-0.1.138.dist-info → jarvis_ai_assistant-0.1.141.dist-info}/METADATA +4 -4
- jarvis_ai_assistant-0.1.141.dist-info/RECORD +94 -0
- {jarvis_ai_assistant-0.1.138.dist-info → jarvis_ai_assistant-0.1.141.dist-info}/entry_points.txt +4 -4
- jarvis/jarvis_code_agent/file_select.py +0 -202
- jarvis/jarvis_platform/ai8.py +0 -268
- jarvis/jarvis_platform/ollama.py +0 -137
- jarvis/jarvis_platform/oyi.py +0 -307
- jarvis/jarvis_rag/file_processors.py +0 -138
- jarvis/jarvis_rag/main.py +0 -1734
- jarvis/jarvis_tools/code_review.py +0 -333
- jarvis/jarvis_tools/execute_python_script.py +0 -58
- jarvis/jarvis_tools/execute_shell.py +0 -97
- jarvis/jarvis_tools/execute_shell_script.py +0 -58
- jarvis/jarvis_tools/find_caller.py +0 -278
- jarvis/jarvis_tools/find_symbol.py +0 -295
- jarvis/jarvis_tools/function_analyzer.py +0 -331
- jarvis/jarvis_tools/git_commiter.py +0 -167
- jarvis/jarvis_tools/project_analyzer.py +0 -304
- jarvis/jarvis_tools/rag.py +0 -143
- jarvis/jarvis_tools/tool_generator.py +0 -221
- jarvis_ai_assistant-0.1.138.dist-info/RECORD +0 -85
- /jarvis/{jarvis_rag → jarvis_init}/__init__.py +0 -0
- {jarvis_ai_assistant-0.1.138.dist-info → jarvis_ai_assistant-0.1.141.dist-info}/LICENSE +0 -0
- {jarvis_ai_assistant-0.1.138.dist-info → jarvis_ai_assistant-0.1.141.dist-info}/WHEEL +0 -0
- {jarvis_ai_assistant-0.1.138.dist-info → jarvis_ai_assistant-0.1.141.dist-info}/top_level.txt +0 -0
|
@@ -1,331 +0,0 @@
|
|
|
1
|
-
from typing import Dict, Any, List
|
|
2
|
-
import os
|
|
3
|
-
|
|
4
|
-
from jarvis.jarvis_agent import Agent
|
|
5
|
-
from jarvis.jarvis_platform.registry import PlatformRegistry
|
|
6
|
-
from jarvis.jarvis_utils.output import OutputType, PrettyOutput
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class FunctionAnalyzerTool:
|
|
10
|
-
"""
|
|
11
|
-
函数分析工具
|
|
12
|
-
使用agent深入分析函数内部实现,包括子函数调用、全局变量使用等
|
|
13
|
-
"""
|
|
14
|
-
|
|
15
|
-
name = "function_analyzer"
|
|
16
|
-
description = "深入分析函数内部实现,查找子函数调用、全局变量使用等详细信息"
|
|
17
|
-
parameters = {
|
|
18
|
-
"type": "object",
|
|
19
|
-
"properties": {
|
|
20
|
-
"function_name": {
|
|
21
|
-
"type": "string",
|
|
22
|
-
"description": "要分析的函数名称"
|
|
23
|
-
},
|
|
24
|
-
"file_path": {
|
|
25
|
-
"type": "string",
|
|
26
|
-
"description": "函数所在文件路径(如果已知)",
|
|
27
|
-
"default": ""
|
|
28
|
-
},
|
|
29
|
-
"root_dir": {
|
|
30
|
-
"type": "string",
|
|
31
|
-
"description": "代码库根目录路径(可选)",
|
|
32
|
-
"default": "."
|
|
33
|
-
},
|
|
34
|
-
"file_extensions": {
|
|
35
|
-
"type": "array",
|
|
36
|
-
"items": {
|
|
37
|
-
"type": "string"
|
|
38
|
-
},
|
|
39
|
-
"description": "要搜索的文件扩展名列表(如:['.py', '.js'])(可选)",
|
|
40
|
-
"default": []
|
|
41
|
-
},
|
|
42
|
-
"exclude_dirs": {
|
|
43
|
-
"type": "array",
|
|
44
|
-
"items": {
|
|
45
|
-
"type": "string"
|
|
46
|
-
},
|
|
47
|
-
"description": "要排除的目录列表(可选)",
|
|
48
|
-
"default": []
|
|
49
|
-
},
|
|
50
|
-
"analysis_depth": {
|
|
51
|
-
"type": "integer",
|
|
52
|
-
"description": "子函数分析深度(可选),0表示不分析子函数,1表示分析直接子函数,以此类推",
|
|
53
|
-
"default": 1
|
|
54
|
-
},
|
|
55
|
-
"objective": {
|
|
56
|
-
"type": "string",
|
|
57
|
-
"description": "描述本次函数分析的目标和用途,例如'理解函数实现以便重构'或'评估性能瓶颈'",
|
|
58
|
-
"default": ""
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
"required": ["function_name"]
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
def execute(self, args: Dict[str, Any]) -> Dict[str, Any]:
|
|
65
|
-
"""
|
|
66
|
-
执行函数分析工具
|
|
67
|
-
|
|
68
|
-
Args:
|
|
69
|
-
args: 包含参数的字典
|
|
70
|
-
|
|
71
|
-
Returns:
|
|
72
|
-
包含执行结果的字典
|
|
73
|
-
"""
|
|
74
|
-
# 存储原始目录
|
|
75
|
-
original_dir = os.getcwd()
|
|
76
|
-
|
|
77
|
-
try:
|
|
78
|
-
# 解析参数
|
|
79
|
-
function_name = args.get("function_name", "")
|
|
80
|
-
file_path = args.get("file_path", "")
|
|
81
|
-
root_dir = args.get("root_dir", ".")
|
|
82
|
-
file_extensions = args.get("file_extensions", [])
|
|
83
|
-
exclude_dirs = args.get("exclude_dirs", [])
|
|
84
|
-
analysis_depth = args.get("analysis_depth", 1)
|
|
85
|
-
objective = args.get("objective", "")
|
|
86
|
-
|
|
87
|
-
# 验证参数
|
|
88
|
-
if not function_name:
|
|
89
|
-
return {
|
|
90
|
-
"success": False,
|
|
91
|
-
"stdout": "",
|
|
92
|
-
"stderr": "必须提供函数名称"
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
# 创建agent的system prompt
|
|
96
|
-
system_prompt = self._create_system_prompt(
|
|
97
|
-
function_name, file_path, root_dir,
|
|
98
|
-
file_extensions, exclude_dirs, analysis_depth, objective
|
|
99
|
-
)
|
|
100
|
-
|
|
101
|
-
# 创建agent的summary prompt
|
|
102
|
-
summary_prompt = self._create_summary_prompt(function_name, analysis_depth)
|
|
103
|
-
|
|
104
|
-
# 切换到根目录
|
|
105
|
-
os.chdir(root_dir)
|
|
106
|
-
|
|
107
|
-
# 构建使用的工具
|
|
108
|
-
from jarvis.jarvis_tools.registry import ToolRegistry
|
|
109
|
-
tool_registry = ToolRegistry()
|
|
110
|
-
tool_registry.use_tools(["execute_shell", "read_code"])
|
|
111
|
-
|
|
112
|
-
# 创建并运行agent
|
|
113
|
-
analyzer_agent = Agent(
|
|
114
|
-
system_prompt=system_prompt,
|
|
115
|
-
name=f"FunctionAnalyzer-{function_name}",
|
|
116
|
-
description=f"分析 '{function_name}' 函数的内部实现",
|
|
117
|
-
summary_prompt=summary_prompt,
|
|
118
|
-
platform=PlatformRegistry().get_normal_platform(),
|
|
119
|
-
output_handler=[tool_registry],
|
|
120
|
-
execute_tool_confirm=False,
|
|
121
|
-
auto_complete=True
|
|
122
|
-
)
|
|
123
|
-
|
|
124
|
-
# 运行agent并获取结果
|
|
125
|
-
task_input = f"深入分析 '{function_name}' 函数的内部实现,包括子函数调用、全局变量使用等详细信息"
|
|
126
|
-
result = analyzer_agent.run(task_input)
|
|
127
|
-
|
|
128
|
-
return {
|
|
129
|
-
"success": True,
|
|
130
|
-
"stdout": result,
|
|
131
|
-
"stderr": ""
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
except Exception as e:
|
|
135
|
-
PrettyOutput.print(str(e), OutputType.ERROR)
|
|
136
|
-
return {
|
|
137
|
-
"success": False,
|
|
138
|
-
"stdout": "",
|
|
139
|
-
"stderr": f"函数分析失败: {str(e)}"
|
|
140
|
-
}
|
|
141
|
-
finally:
|
|
142
|
-
# 恢复原始目录
|
|
143
|
-
os.chdir(original_dir)
|
|
144
|
-
|
|
145
|
-
def _create_system_prompt(self, function_name: str, file_path: str, root_dir: str,
|
|
146
|
-
file_extensions: List[str], exclude_dirs: List[str],
|
|
147
|
-
analysis_depth: int, objective: str) -> str:
|
|
148
|
-
"""
|
|
149
|
-
创建Agent的system prompt
|
|
150
|
-
|
|
151
|
-
Args:
|
|
152
|
-
function_name: 函数名称
|
|
153
|
-
file_path: 函数所在文件路径
|
|
154
|
-
root_dir: 代码库根目录
|
|
155
|
-
file_extensions: 文件扩展名列表
|
|
156
|
-
exclude_dirs: 排除目录列表
|
|
157
|
-
analysis_depth: 子函数分析深度
|
|
158
|
-
objective: 分析目标
|
|
159
|
-
|
|
160
|
-
Returns:
|
|
161
|
-
系统提示文本
|
|
162
|
-
"""
|
|
163
|
-
file_ext_str = " ".join([f"*{ext}" for ext in file_extensions]) if file_extensions else ""
|
|
164
|
-
exclude_str = " ".join([f"--glob '!{excl}'" for excl in exclude_dirs]) if exclude_dirs else ""
|
|
165
|
-
|
|
166
|
-
depth_description = "不分析子函数" if analysis_depth == 0 else f"分析 {analysis_depth} 层子函数"
|
|
167
|
-
file_info = f"已知文件路径: {file_path}" if file_path else "需要首先查找函数定义位置"
|
|
168
|
-
objective_text = f"\n\n## 分析目标\n{objective}" if objective else ""
|
|
169
|
-
|
|
170
|
-
return f"""# 函数实现分析专家
|
|
171
|
-
|
|
172
|
-
## 任务描述
|
|
173
|
-
分析函数 `{function_name}` 的实现,专注于分析目标所需的信息,生成有针对性的函数分析报告。{objective_text}
|
|
174
|
-
|
|
175
|
-
## 工具使用优先级
|
|
176
|
-
1. **首先使用 execute_shell 执行 rg 命令查找函数定义**:
|
|
177
|
-
- `rg "def\\s+{function_name}\\s*\\(" --type py` 查找Python函数定义
|
|
178
|
-
- `rg "function\\s+{function_name}\\s*\\(" --type js` 查找JavaScript函数定义
|
|
179
|
-
- `rg "func\\s+{function_name}\\s*\\(" --type go` 查找Go函数定义
|
|
180
|
-
|
|
181
|
-
2. **优先使用 read_code 阅读函数实现**:
|
|
182
|
-
- 找到函数位置后使用read_code阅读完整实现
|
|
183
|
-
- 对于长函数可分段读取关键部分
|
|
184
|
-
|
|
185
|
-
3. **使用 rg 搜索子函数调用和使用模式**:
|
|
186
|
-
- `rg -w "子函数名" --type py` 查找子函数调用
|
|
187
|
-
- `rg "import|from" 函数所在文件` 查找导入模块
|
|
188
|
-
|
|
189
|
-
4. **避免使用专用分析工具**:
|
|
190
|
-
- 只有当rg命令和read_code工具无法满足需求时才考虑
|
|
191
|
-
|
|
192
|
-
## 函数信息
|
|
193
|
-
- 函数名称: `{function_name}`
|
|
194
|
-
- {file_info}
|
|
195
|
-
- 分析深度: {depth_description}
|
|
196
|
-
- 代码范围: {file_ext_str if file_ext_str else "所有文件"}
|
|
197
|
-
- 排除目录: {", ".join(exclude_dirs) if exclude_dirs else "无"}
|
|
198
|
-
|
|
199
|
-
## 分析策略
|
|
200
|
-
1. 首先确定项目的主要编程语言和技术栈,以便更准确地分析函数实现
|
|
201
|
-
2. 理解分析目标,明确需要查找的信息
|
|
202
|
-
3. {"在指定文件中定位函数定义" if file_path else "搜索代码库查找函数定义位置"}
|
|
203
|
-
4. 根据分析目标,确定重点分析的方面
|
|
204
|
-
5. 灵活调整分析深度,关注与目标相关的实现细节
|
|
205
|
-
6. 根据目标需要自行判断是否需要分析子函数
|
|
206
|
-
|
|
207
|
-
## 函数分析工具指南
|
|
208
|
-
|
|
209
|
-
### execute_shell 搜索命令
|
|
210
|
-
- **查找函数定义**:
|
|
211
|
-
- `rg "def\\s+{function_name}\\s*\\(" --type py` 查找Python函数定义
|
|
212
|
-
- `rg "function\\s+{function_name}\\s*\\(" --type js` 查找JavaScript函数定义
|
|
213
|
-
- `rg "func\\s+{function_name}\\s*\\(" --type go` 查找Go函数定义
|
|
214
|
-
|
|
215
|
-
- **查找函数调用**:
|
|
216
|
-
- `rg "\\b{function_name}\\s*\\(" --type py` 查找函数调用
|
|
217
|
-
- `rg -w "{function_name}" -A 2 -B 2` 查看函数调用上下文
|
|
218
|
-
|
|
219
|
-
- **分析函数依赖**:
|
|
220
|
-
- `rg "import|from" 函数所在文件` 查找Python导入语句
|
|
221
|
-
- `rg "require\\(" 函数所在文件` 查找JavaScript导入语句
|
|
222
|
-
- `rg "import " 函数所在文件` 查找Go导入语句
|
|
223
|
-
|
|
224
|
-
- **统计分析**:
|
|
225
|
-
- `loc 函数所在文件` 获取文件代码统计
|
|
226
|
-
- `rg -c "if|else|elif" 函数所在文件` 统计条件分支数量
|
|
227
|
-
- `rg -c "for|while" 函数所在文件` 统计循环数量
|
|
228
|
-
|
|
229
|
-
### read_code
|
|
230
|
-
- **用途**:读取函数实现和相关代码
|
|
231
|
-
- **典型用法**:
|
|
232
|
-
- 阅读完整函数实现,包括注释和文档
|
|
233
|
-
- 阅读关键子函数的实现(根据analysis_depth)
|
|
234
|
-
- 阅读使用到的关键变量和依赖组件定义
|
|
235
|
-
- **使用策略**:
|
|
236
|
-
- 首先阅读整个函数以获取完整视图
|
|
237
|
-
- 识别函数的主要逻辑分支和处理路径
|
|
238
|
-
- 重点关注错误处理和边界条件检查
|
|
239
|
-
- 对复杂子函数进行递归分析(不超过指定深度)
|
|
240
|
-
|
|
241
|
-
### 函数分析模式
|
|
242
|
-
|
|
243
|
-
1. **逻辑流程分析**:
|
|
244
|
-
- 识别函数的主要执行路径
|
|
245
|
-
- 分析条件分支和循环结构
|
|
246
|
-
- 构建完整的逻辑流程图
|
|
247
|
-
|
|
248
|
-
2. **参数分析**:
|
|
249
|
-
- 分析参数类型、默认值和约束条件
|
|
250
|
-
- 了解参数在函数中的使用方式
|
|
251
|
-
- 识别关键参数及其影响
|
|
252
|
-
|
|
253
|
-
3. **依赖与副作用分析**:
|
|
254
|
-
- 识别函数使用的外部变量和组件
|
|
255
|
-
- 分析函数对外部状态的修改
|
|
256
|
-
- 评估函数的纯度和可测试性
|
|
257
|
-
|
|
258
|
-
4. **异常处理分析**:
|
|
259
|
-
- 识别可能的异常和错误情况
|
|
260
|
-
- 分析错误处理和恢复机制
|
|
261
|
-
- 评估错误处理的完整性和合理性
|
|
262
|
-
|
|
263
|
-
5. **性能分析**:
|
|
264
|
-
- 识别潜在的性能瓶颈
|
|
265
|
-
- 分析循环和递归的效率
|
|
266
|
-
- 评估算法复杂度
|
|
267
|
-
|
|
268
|
-
6. **子函数调用分析**:
|
|
269
|
-
- 识别所有调用的子函数
|
|
270
|
-
- 分析子函数的作用和调用模式
|
|
271
|
-
- 在允许的深度内递归分析关键子函数
|
|
272
|
-
|
|
273
|
-
## 编程范式适应
|
|
274
|
-
|
|
275
|
-
针对不同的编程范式,函数分析应关注不同方面:
|
|
276
|
-
|
|
277
|
-
### 过程式/函数式
|
|
278
|
-
- 输入输出关系和纯函数特性
|
|
279
|
-
- 控制流程和数据转换
|
|
280
|
-
- 递归和迭代模式
|
|
281
|
-
|
|
282
|
-
### 面向对象
|
|
283
|
-
- 方法与类的交互
|
|
284
|
-
- 状态修改和实例变量访问
|
|
285
|
-
- 继承和多态特性
|
|
286
|
-
|
|
287
|
-
### 事件驱动/回调式
|
|
288
|
-
- 事件注册和触发机制
|
|
289
|
-
- 回调链和异步流程
|
|
290
|
-
- 状态管理和并发控制
|
|
291
|
-
|
|
292
|
-
### 声明式
|
|
293
|
-
- 规则和约束定义
|
|
294
|
-
- 表达式和模式匹配
|
|
295
|
-
- 执行上下文和环境
|
|
296
|
-
|
|
297
|
-
## 输出要求
|
|
298
|
-
- 直接回应分析目标的关键问题
|
|
299
|
-
- 提供与目标相关的函数实现信息
|
|
300
|
-
- 分析内容应直接服务于分析目标
|
|
301
|
-
- 避免与目标无关的冗余信息
|
|
302
|
-
- 使用具体代码片段和示例支持分析结论
|
|
303
|
-
- 提供针对分析目标的具体见解和建议"""
|
|
304
|
-
|
|
305
|
-
def _create_summary_prompt(self, function_name: str, analysis_depth: int) -> str:
|
|
306
|
-
"""
|
|
307
|
-
创建Agent的summary prompt
|
|
308
|
-
|
|
309
|
-
Args:
|
|
310
|
-
function_name: 函数名称
|
|
311
|
-
analysis_depth: 子函数分析深度
|
|
312
|
-
|
|
313
|
-
Returns:
|
|
314
|
-
总结提示文本
|
|
315
|
-
"""
|
|
316
|
-
depth_description = "不包含子函数分析" if analysis_depth == 0 else f"包含 {analysis_depth} 层子函数分析"
|
|
317
|
-
|
|
318
|
-
return f"""# 函数分析报告: `{function_name}`
|
|
319
|
-
|
|
320
|
-
## 报告要求
|
|
321
|
-
生成一份完全以分析目标为导向的函数分析报告,{depth_description}。不要遵循固定的报告模板,而是完全根据分析目标来组织内容:
|
|
322
|
-
|
|
323
|
-
- 首先简要说明项目的主要编程语言和技术栈
|
|
324
|
-
- 专注回答分析目标提出的问题
|
|
325
|
-
- 只包含与分析目标直接相关的实现发现和洞察
|
|
326
|
-
- 完全跳过与分析目标无关的内容,无需做全面分析
|
|
327
|
-
- 分析深度应与目标的具体需求匹配
|
|
328
|
-
- 使用具体的代码片段支持你的观点
|
|
329
|
-
- 以清晰的Markdown格式呈现,简洁明了
|
|
330
|
-
|
|
331
|
-
在分析中保持灵活性,避免固定思维模式。你的任务不是提供全面的函数概览,而是直接解决分析目标中提出的具体问题。"""
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import re
|
|
2
|
-
import shlex
|
|
3
|
-
import subprocess
|
|
4
|
-
from typing import Dict, Any
|
|
5
|
-
import tempfile
|
|
6
|
-
import yaml
|
|
7
|
-
from yaspin import yaspin
|
|
8
|
-
from jarvis.jarvis_platform.registry import PlatformRegistry
|
|
9
|
-
import sys
|
|
10
|
-
import argparse
|
|
11
|
-
import os
|
|
12
|
-
|
|
13
|
-
from jarvis.jarvis_utils.git_utils import find_git_root, has_uncommitted_changes
|
|
14
|
-
from jarvis.jarvis_utils.output import OutputType, PrettyOutput
|
|
15
|
-
from jarvis.jarvis_utils.utils import ct, ot, init_env
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
class GitCommitTool:
|
|
19
|
-
name = "git_commit_agent"
|
|
20
|
-
description = "根据代码变更自动生成并执行Git提交"
|
|
21
|
-
parameters = {
|
|
22
|
-
"type": "object",
|
|
23
|
-
"properties": {
|
|
24
|
-
"lang": {
|
|
25
|
-
"type": "string",
|
|
26
|
-
"description": "提交信息的语言",
|
|
27
|
-
"default": "Chinese"
|
|
28
|
-
},
|
|
29
|
-
"root_dir": {
|
|
30
|
-
"type": "string",
|
|
31
|
-
"description": "Git仓库的根目录路径(可选)",
|
|
32
|
-
"default": "."
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
"required": []
|
|
36
|
-
}
|
|
37
|
-
def _extract_commit_message(self, message):
|
|
38
|
-
"""Raw extraction preserving all characters"""
|
|
39
|
-
r = re.search(
|
|
40
|
-
r"(?i)" + ot("COMMIT_MESSAGE") + r"\s*([\s\S]*?)\s*" + ct("COMMIT_MESSAGE"),
|
|
41
|
-
message
|
|
42
|
-
)
|
|
43
|
-
if r:
|
|
44
|
-
# 直接返回原始内容,仅去除外围空白
|
|
45
|
-
return shlex.quote(r.group(1).strip())
|
|
46
|
-
return "<<FORMAT VIOLATION>> Invalid commit message structure"
|
|
47
|
-
|
|
48
|
-
def _get_last_commit_hash(self):
|
|
49
|
-
process = subprocess.Popen(
|
|
50
|
-
["git", "log", "-1", "--pretty=%H"],
|
|
51
|
-
stdout=subprocess.PIPE,
|
|
52
|
-
stderr=subprocess.PIPE
|
|
53
|
-
)
|
|
54
|
-
stdout, _ = process.communicate()
|
|
55
|
-
return stdout.decode().strip()
|
|
56
|
-
|
|
57
|
-
def execute(self, args: Dict) -> Dict[str, Any]:
|
|
58
|
-
"""Execute automatic commit process with support for multi-line messages and special characters"""
|
|
59
|
-
try:
|
|
60
|
-
root_dir = args.get("root_dir", ".")
|
|
61
|
-
|
|
62
|
-
# Store current directory
|
|
63
|
-
original_dir = os.getcwd()
|
|
64
|
-
|
|
65
|
-
try:
|
|
66
|
-
# Change to root_dir
|
|
67
|
-
os.chdir(root_dir)
|
|
68
|
-
|
|
69
|
-
find_git_root()
|
|
70
|
-
if not has_uncommitted_changes():
|
|
71
|
-
PrettyOutput.print("没有未提交的更改", OutputType.SUCCESS)
|
|
72
|
-
return {"success": True, "stdout": "No changes to commit", "stderr": ""}
|
|
73
|
-
|
|
74
|
-
with yaspin(text="正在初始化提交流程...", color="cyan") as spinner:
|
|
75
|
-
# 添加文件
|
|
76
|
-
spinner.text = "正在添加文件到提交..."
|
|
77
|
-
subprocess.Popen(
|
|
78
|
-
["git", "add", "."],
|
|
79
|
-
stdout=subprocess.DEVNULL,
|
|
80
|
-
stderr=subprocess.DEVNULL
|
|
81
|
-
).wait()
|
|
82
|
-
spinner.write("✅ 添加文件到提交")
|
|
83
|
-
|
|
84
|
-
# 获取差异
|
|
85
|
-
spinner.text = "正在获取代码差异..."
|
|
86
|
-
process = subprocess.Popen(
|
|
87
|
-
["git", "diff", "--cached", "--exit-code"],
|
|
88
|
-
stdout=subprocess.PIPE,
|
|
89
|
-
stderr=subprocess.PIPE
|
|
90
|
-
)
|
|
91
|
-
diff = process.communicate()[0].decode()
|
|
92
|
-
spinner.write("✅ 获取差异")
|
|
93
|
-
|
|
94
|
-
# 生成提交信息
|
|
95
|
-
spinner.text = "正在生成提交消息..."
|
|
96
|
-
prompt = f'''根据以下规则生成提交信息:
|
|
97
|
-
提交信息应使用{args.get('lang', '中文')}书写
|
|
98
|
-
# 必需结构
|
|
99
|
-
必须使用以下格式:
|
|
100
|
-
{ot("COMMIT_MESSAGE")}
|
|
101
|
-
<类型>(<范围>): <主题>
|
|
102
|
-
使用祈使语气描述变更内容
|
|
103
|
-
{ct("COMMIT_MESSAGE")}
|
|
104
|
-
# 格式规则
|
|
105
|
-
1. 类型: fix, feat, docs, style, refactor, test, chore
|
|
106
|
-
2. 范围表示模块 (例如: auth, database)
|
|
107
|
-
3. 主题行 <= 72个字符,不以句号结尾
|
|
108
|
-
4. 正文使用现在时态解释每个变更的内容和原因
|
|
109
|
-
5. 不要遗漏任何变更
|
|
110
|
-
# 分析材料
|
|
111
|
-
{diff}
|
|
112
|
-
'''
|
|
113
|
-
platform = PlatformRegistry().get_normal_platform()
|
|
114
|
-
commit_message = platform.chat_until_success(prompt)
|
|
115
|
-
commit_message = self._extract_commit_message(commit_message)
|
|
116
|
-
spinner.write("✅ 生成提交消息")
|
|
117
|
-
|
|
118
|
-
# 执行提交
|
|
119
|
-
spinner.text = "正在准备提交..."
|
|
120
|
-
with tempfile.NamedTemporaryFile(mode='w', delete=True) as tmp_file:
|
|
121
|
-
tmp_file.write(commit_message)
|
|
122
|
-
tmp_file.flush()
|
|
123
|
-
spinner.text = "正在执行提交..."
|
|
124
|
-
commit_cmd = ["git", "commit", "-F", tmp_file.name]
|
|
125
|
-
subprocess.Popen(
|
|
126
|
-
commit_cmd,
|
|
127
|
-
stdout=subprocess.DEVNULL,
|
|
128
|
-
stderr=subprocess.DEVNULL
|
|
129
|
-
).wait()
|
|
130
|
-
spinner.write("✅ 提交")
|
|
131
|
-
|
|
132
|
-
commit_hash = self._get_last_commit_hash()
|
|
133
|
-
spinner.text = "完成提交"
|
|
134
|
-
spinner.ok("✅")
|
|
135
|
-
|
|
136
|
-
PrettyOutput.print(f"提交哈希: {commit_hash}\n提交消息: {commit_message}", OutputType.SUCCESS)
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
"success": True,
|
|
140
|
-
"stdout": yaml.safe_dump({
|
|
141
|
-
"commit_hash": commit_hash,
|
|
142
|
-
"commit_message": commit_message
|
|
143
|
-
}),
|
|
144
|
-
"stderr": ""
|
|
145
|
-
}
|
|
146
|
-
finally:
|
|
147
|
-
# Always restore original directory
|
|
148
|
-
os.chdir(original_dir)
|
|
149
|
-
|
|
150
|
-
except Exception as e:
|
|
151
|
-
return {
|
|
152
|
-
"success": False,
|
|
153
|
-
"stdout": "",
|
|
154
|
-
"stderr": f"Commit failed: {str(e)}"
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
def main():
|
|
158
|
-
init_env()
|
|
159
|
-
parser = argparse.ArgumentParser(description='Git commit tool')
|
|
160
|
-
parser.add_argument('--lang', type=str, default='Chinese', help='Language for commit messages')
|
|
161
|
-
parser.add_argument('--root-dir', type=str, default='.', help='Root directory of the Git repository')
|
|
162
|
-
args = parser.parse_args()
|
|
163
|
-
tool = GitCommitTool()
|
|
164
|
-
tool.execute({"lang": args.lang if hasattr(args, 'lang') else 'Chinese', "root_dir": args.root_dir})
|
|
165
|
-
|
|
166
|
-
if __name__ == "__main__":
|
|
167
|
-
sys.exit(main())
|