jarvis-ai-assistant 0.1.130__py3-none-any.whl → 0.1.131__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.

Files changed (60) hide show
  1. jarvis/__init__.py +1 -1
  2. jarvis/jarvis_agent/__init__.py +23 -9
  3. jarvis/jarvis_agent/builtin_input_handler.py +73 -0
  4. jarvis/{jarvis_code_agent → jarvis_agent}/file_input_handler.py +1 -1
  5. jarvis/jarvis_agent/main.py +1 -1
  6. jarvis/{jarvis_code_agent → jarvis_agent}/patch.py +23 -19
  7. jarvis/{jarvis_code_agent → jarvis_agent}/shell_input_handler.py +0 -1
  8. jarvis/jarvis_code_agent/code_agent.py +20 -16
  9. jarvis/jarvis_codebase/main.py +5 -5
  10. jarvis/jarvis_dev/main.py +1 -1
  11. jarvis/jarvis_git_squash/main.py +1 -1
  12. jarvis/jarvis_lsp/base.py +2 -26
  13. jarvis/jarvis_lsp/cpp.py +2 -14
  14. jarvis/jarvis_lsp/go.py +0 -13
  15. jarvis/jarvis_lsp/python.py +1 -30
  16. jarvis/jarvis_lsp/registry.py +10 -14
  17. jarvis/jarvis_lsp/rust.py +0 -12
  18. jarvis/jarvis_multi_agent/__init__.py +1 -1
  19. jarvis/jarvis_platform/registry.py +1 -1
  20. jarvis/jarvis_platform_manager/main.py +3 -3
  21. jarvis/jarvis_rag/main.py +1 -1
  22. jarvis/jarvis_tools/ask_codebase.py +40 -20
  23. jarvis/jarvis_tools/code_review.py +180 -143
  24. jarvis/jarvis_tools/create_code_agent.py +76 -72
  25. jarvis/jarvis_tools/create_sub_agent.py +32 -15
  26. jarvis/jarvis_tools/execute_shell.py +2 -2
  27. jarvis/jarvis_tools/execute_shell_script.py +1 -1
  28. jarvis/jarvis_tools/file_operation.py +2 -2
  29. jarvis/jarvis_tools/git_commiter.py +87 -68
  30. jarvis/jarvis_tools/lsp_find_definition.py +83 -67
  31. jarvis/jarvis_tools/lsp_find_references.py +62 -46
  32. jarvis/jarvis_tools/lsp_get_diagnostics.py +90 -74
  33. jarvis/jarvis_tools/methodology.py +3 -3
  34. jarvis/jarvis_tools/read_code.py +1 -1
  35. jarvis/jarvis_tools/search_web.py +18 -20
  36. jarvis/jarvis_tools/tool_generator.py +1 -1
  37. jarvis/jarvis_tools/treesitter_analyzer.py +331 -0
  38. jarvis/jarvis_treesitter/README.md +104 -0
  39. jarvis/jarvis_treesitter/__init__.py +20 -0
  40. jarvis/jarvis_treesitter/database.py +258 -0
  41. jarvis/jarvis_treesitter/example.py +115 -0
  42. jarvis/jarvis_treesitter/grammar_builder.py +182 -0
  43. jarvis/jarvis_treesitter/language.py +117 -0
  44. jarvis/jarvis_treesitter/symbol.py +31 -0
  45. jarvis/jarvis_treesitter/tools_usage.md +121 -0
  46. jarvis/jarvis_utils/git_utils.py +10 -2
  47. jarvis/jarvis_utils/input.py +3 -1
  48. jarvis/jarvis_utils/methodology.py +1 -1
  49. jarvis/jarvis_utils/utils.py +3 -3
  50. {jarvis_ai_assistant-0.1.130.dist-info → jarvis_ai_assistant-0.1.131.dist-info}/METADATA +2 -4
  51. jarvis_ai_assistant-0.1.131.dist-info/RECORD +85 -0
  52. jarvis/jarvis_c2rust/c2rust.yaml +0 -734
  53. jarvis/jarvis_code_agent/builtin_input_handler.py +0 -43
  54. jarvis/jarvis_tools/lsp_get_document_symbols.py +0 -87
  55. jarvis/jarvis_tools/lsp_prepare_rename.py +0 -130
  56. jarvis_ai_assistant-0.1.130.dist-info/RECORD +0 -79
  57. {jarvis_ai_assistant-0.1.130.dist-info → jarvis_ai_assistant-0.1.131.dist-info}/LICENSE +0 -0
  58. {jarvis_ai_assistant-0.1.130.dist-info → jarvis_ai_assistant-0.1.131.dist-info}/WHEEL +0 -0
  59. {jarvis_ai_assistant-0.1.130.dist-info → jarvis_ai_assistant-0.1.131.dist-info}/entry_points.txt +0 -0
  60. {jarvis_ai_assistant-0.1.130.dist-info → jarvis_ai_assistant-0.1.131.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,331 @@
1
+ from typing import Dict, Any, List, Optional
2
+ import os
3
+ import logging
4
+ from yaspin import yaspin
5
+
6
+ from jarvis.jarvis_utils.output import OutputType, PrettyOutput
7
+ from jarvis.jarvis_treesitter import (
8
+ CodeDatabase,
9
+ SymbolType,
10
+ setup_default_grammars,
11
+ DEFAULT_GRAMMAR_DIR
12
+ )
13
+
14
+ # 配置日志
15
+ logger = logging.getLogger(__name__)
16
+
17
+ class TreesitterAnalyzer:
18
+ """Tree-sitter 代码分析工具,用于快速查找代码中的符号定义、引用和调用关系"""
19
+
20
+ name = "treesitter_analyzer"
21
+ description = "使用 Tree-sitter 分析代码,查找符号定义、引用和调用关系"
22
+ parameters = {
23
+ "type": "object",
24
+ "properties": {
25
+ "action": {
26
+ "type": "string",
27
+ "enum": ["find_symbol", "find_references", "find_callers"],
28
+ "description": "分析操作类型: find_symbol(查找符号), find_references(查找引用), find_callers(查找调用者)"
29
+ },
30
+ "symbol_name": {
31
+ "type": "string",
32
+ "description": "要查找的符号名称,如函数名、类名、变量名等"
33
+ },
34
+ "directory": {
35
+ "type": "string",
36
+ "description": "要索引的代码目录,默认为当前目录",
37
+ "default": "."
38
+ },
39
+ "extensions": {
40
+ "type": "array",
41
+ "items": {"type": "string"},
42
+ "description": "要索引的文件扩展名列表,如 [\".py\", \".c\"],不指定则索引所有支持的文件类型"
43
+ },
44
+ "max_results": {
45
+ "type": "integer",
46
+ "description": "最大返回结果数量",
47
+ "default": 20
48
+ }
49
+ },
50
+ "required": ["action", "symbol_name", "directory"]
51
+ }
52
+
53
+ def __init__(self):
54
+ """初始化 Tree-sitter 分析器工具"""
55
+ # 确保语法文件目录存在
56
+ os.makedirs(DEFAULT_GRAMMAR_DIR, exist_ok=True)
57
+
58
+ # 创建代码数据库实例
59
+ self.db = None
60
+
61
+ def _get_database(self) -> CodeDatabase:
62
+ """获取 Tree-sitter 代码数据库实例,如果不存在则创建"""
63
+ if self.db is None:
64
+ self.db = CodeDatabase()
65
+ return self.db
66
+
67
+ def _index_directory(self, directory: str, extensions: Optional[List[str]] = None) -> Dict[str, Any]:
68
+ """索引指定目录下的代码文件"""
69
+ try:
70
+ db = self._get_database()
71
+ indexed_files = []
72
+ skipped_files = []
73
+
74
+ with yaspin(text=f"正在索引目录: {directory}...", color="cyan") as spinner:
75
+ for root, _, files in os.walk(directory):
76
+ for file in files:
77
+ # 检查文件扩展名
78
+ if extensions and not any(file.endswith(ext) for ext in extensions):
79
+ continue
80
+
81
+ file_path = os.path.join(root, file)
82
+ try:
83
+ db.index_file(file_path)
84
+ indexed_files.append(file_path)
85
+ except Exception as e:
86
+ skipped_files.append((file_path, str(e)))
87
+
88
+ spinner.text = f"索引完成: {len(indexed_files)} 个文件"
89
+ spinner.ok("✅")
90
+
91
+ return {
92
+ "success": True,
93
+ "indexed_files": indexed_files,
94
+ "skipped_files": skipped_files,
95
+ "index_summary": f"已成功索引 {len(indexed_files)} 个文件,跳过 {len(skipped_files)} 个文件"
96
+ }
97
+
98
+ except Exception as e:
99
+ logger.error(f"索引目录失败: {str(e)}")
100
+ return {
101
+ "success": False,
102
+ "stderr": f"索引目录失败: {str(e)}"
103
+ }
104
+
105
+ def _find_symbol(self, symbol_name: str, directory: str, max_results: int = 20) -> Dict[str, Any]:
106
+ """查找代码中的符号定义"""
107
+ try:
108
+ db = self._get_database()
109
+ symbols = db.find_symbol(symbol_name)
110
+
111
+ if not symbols:
112
+ return {
113
+ "success": True,
114
+ "stdout": f"未找到名为 '{symbol_name}' 的符号",
115
+ "symbols": []
116
+ }
117
+
118
+ # 限制结果数量
119
+ symbols = symbols[:max_results]
120
+
121
+ # 构建结果
122
+ result_list = []
123
+ for symbol in symbols:
124
+ result_list.append({
125
+ "name": symbol.name,
126
+ "type": symbol.type.value,
127
+ "file": symbol.location.file_path,
128
+ "line": symbol.location.start_line,
129
+ "column": symbol.location.start_column
130
+ })
131
+
132
+ # 构建输出文本
133
+ output_text = f"找到 {len(symbols)} 个名为 '{symbol_name}' 的符号:\n\n"
134
+ for i, symbol in enumerate(symbols, 1):
135
+ output_text += (f"{i}. {symbol.type.value}: {symbol.name}\n"
136
+ f" 位置: {symbol.location.file_path}:{symbol.location.start_line}:{symbol.location.start_column}\n\n")
137
+
138
+ return {
139
+ "success": True,
140
+ "stdout": output_text,
141
+ "symbols": result_list
142
+ }
143
+
144
+ except Exception as e:
145
+ logger.error(f"查找符号失败: {str(e)}")
146
+ return {
147
+ "success": False,
148
+ "stderr": f"查找符号失败: {str(e)}"
149
+ }
150
+
151
+ def _find_references(self, symbol_name: str, directory: str, max_results: int = 20) -> Dict[str, Any]:
152
+ """查找代码中符号的引用"""
153
+ try:
154
+ db = self._get_database()
155
+ symbols = db.find_symbol(symbol_name)
156
+
157
+ if not symbols:
158
+ return {
159
+ "success": True,
160
+ "stdout": f"未找到名为 '{symbol_name}' 的符号",
161
+ "references": []
162
+ }
163
+
164
+ # 获取第一个匹配符号的所有引用
165
+ references = db.find_references(symbols[0])
166
+
167
+ # 限制结果数量
168
+ references = references[:max_results]
169
+
170
+ # 构建结果
171
+ result_list = []
172
+ for ref in references:
173
+ result_list.append({
174
+ "file": ref.location.file_path,
175
+ "line": ref.location.start_line,
176
+ "column": ref.location.start_column
177
+ })
178
+
179
+ # 构建输出文本
180
+ output_text = f"找到 {len(references)} 处对 '{symbol_name}' 的引用:\n\n"
181
+ for i, ref in enumerate(references, 1):
182
+ output_text += f"{i}. {ref.location.file_path}:{ref.location.start_line}:{ref.location.start_column}\n"
183
+
184
+ return {
185
+ "success": True,
186
+ "stdout": output_text,
187
+ "symbol": {
188
+ "name": symbols[0].name,
189
+ "type": symbols[0].type.value,
190
+ "file": symbols[0].location.file_path,
191
+ "line": symbols[0].location.start_line,
192
+ "column": symbols[0].location.start_column
193
+ },
194
+ "references": result_list
195
+ }
196
+
197
+ except Exception as e:
198
+ logger.error(f"查找引用失败: {str(e)}")
199
+ return {
200
+ "success": False,
201
+ "stderr": f"查找引用失败: {str(e)}"
202
+ }
203
+
204
+ def _find_callers(self, symbol_name: str, directory: str, max_results: int = 20) -> Dict[str, Any]:
205
+ """查找代码中调用指定函数的位置"""
206
+ try:
207
+ db = self._get_database()
208
+ symbols = db.find_symbol(symbol_name)
209
+
210
+ if not symbols:
211
+ return {
212
+ "success": True,
213
+ "stdout": f"未找到名为 '{symbol_name}' 的函数",
214
+ "callers": []
215
+ }
216
+
217
+ # 筛选出函数类型的符号
218
+ function_symbols = [s for s in symbols if s.type == SymbolType.FUNCTION]
219
+ if not function_symbols:
220
+ return {
221
+ "success": True,
222
+ "stdout": f"'{symbol_name}' 不是一个函数",
223
+ "callers": []
224
+ }
225
+
226
+ # 获取第一个函数符号的所有调用者
227
+ callers = db.find_callers(function_symbols[0])
228
+
229
+ # 限制结果数量
230
+ callers = callers[:max_results]
231
+
232
+ # 构建结果
233
+ result_list = []
234
+ for caller in callers:
235
+ result_list.append({
236
+ "file": caller.location.file_path,
237
+ "line": caller.location.start_line,
238
+ "column": caller.location.start_column
239
+ })
240
+
241
+ # 构建输出文本
242
+ output_text = f"找到 {len(callers)} 处对函数 '{symbol_name}' 的调用:\n\n"
243
+ for i, caller in enumerate(callers, 1):
244
+ output_text += f"{i}. {caller.location.file_path}:{caller.location.start_line}:{caller.location.start_column}\n"
245
+
246
+ return {
247
+ "success": True,
248
+ "stdout": output_text,
249
+ "function": {
250
+ "name": function_symbols[0].name,
251
+ "file": function_symbols[0].location.file_path,
252
+ "line": function_symbols[0].location.start_line,
253
+ "column": function_symbols[0].location.start_column
254
+ },
255
+ "callers": result_list
256
+ }
257
+
258
+ except Exception as e:
259
+ logger.error(f"查找调用者失败: {str(e)}")
260
+ return {
261
+ "success": False,
262
+ "stderr": f"查找调用者失败: {str(e)}"
263
+ }
264
+
265
+ def execute(self, args: Dict) -> Dict[str, Any]:
266
+ """执行 Tree-sitter 代码分析
267
+
268
+ 参数:
269
+ args: 包含操作参数的字典
270
+
271
+ 返回:
272
+ Dict[str, Any]: 操作结果
273
+ """
274
+ try:
275
+ action = args.get("action")
276
+ symbol_name = args.get("symbol_name")
277
+ directory = args.get("directory", ".")
278
+ extensions = args.get("extensions", None)
279
+ max_results = args.get("max_results", 20)
280
+
281
+ # 确保 symbol_name 参数存在
282
+ if not symbol_name:
283
+ return {
284
+ "success": False,
285
+ "stdout": "",
286
+ "stderr": "缺少必要参数: symbol_name"
287
+ }
288
+
289
+ # 确保语法目录存在
290
+ os.makedirs(DEFAULT_GRAMMAR_DIR, exist_ok=True)
291
+
292
+ # 先自动索引目录
293
+ with yaspin(text="正在索引目录...") as spinner:
294
+ index_result = self._index_directory(directory, extensions)
295
+ if not index_result.get("success", False):
296
+ spinner.fail("✗")
297
+ return index_result
298
+ spinner.ok("✓")
299
+
300
+ # 根据不同的操作执行相应的函数
301
+ result = None
302
+ if action == "find_symbol":
303
+ result = self._find_symbol(symbol_name, directory, max_results)
304
+ elif action == "find_references":
305
+ result = self._find_references(symbol_name, directory, max_results)
306
+ elif action == "find_callers":
307
+ result = self._find_callers(symbol_name, directory, max_results)
308
+ else:
309
+ return {
310
+ "success": False,
311
+ "stdout": "",
312
+ "stderr": f"不支持的操作: {action}"
313
+ }
314
+
315
+ # 将索引信息添加到结果中
316
+ if result:
317
+ if "stdout" in result:
318
+ result["stdout"] = f"{index_result.get('index_summary', '')}\n\n{result['stdout']}"
319
+ else:
320
+ result["stdout"] = index_result.get('index_summary', '')
321
+
322
+ return result
323
+
324
+ except Exception as e:
325
+ logger.error(f"Tree-sitter 分析失败: {str(e)}")
326
+ return {
327
+ "success": False,
328
+ "stdout": "",
329
+ "stderr": f"Tree-sitter 分析失败: {str(e)}"
330
+ }
331
+
@@ -0,0 +1,104 @@
1
+ # Tree-sitter 代码数据库
2
+
3
+ 基于 tree-sitter 的代码分析工具,支持快速查询符号的定义位置、声明位置、引用位置和调用关系。
4
+
5
+ ## 功能特点
6
+
7
+ - 支持多种编程语言:Python、C、C++、Go、Rust
8
+ - 自动下载和编译语言语法文件
9
+ - 查找符号定义
10
+ - 查找符号引用
11
+ - 查找函数调用者
12
+
13
+ ## 安装
14
+
15
+ ```bash
16
+ pip install -r requirements.txt
17
+ ```
18
+
19
+ ## 使用方法
20
+
21
+ ### 基本用法
22
+
23
+ ```python
24
+ from jarvis.jarvis_treesitter import CodeDatabase
25
+
26
+ # 初始化代码数据库(自动下载所需的语法文件)
27
+ db = CodeDatabase() # 语法文件将保存到 ~/.jarvis/treesitter 目录
28
+
29
+ # 索引源文件
30
+ db.index_file("path/to/file.py")
31
+
32
+ # 查找符号
33
+ symbols = db.find_symbol("function_name")
34
+
35
+ # 查找符号引用
36
+ references = db.find_references(symbols[0])
37
+
38
+ # 查找函数调用者
39
+ callers = db.find_callers(symbols[0])
40
+ ```
41
+
42
+ ### 自定义语法文件位置
43
+
44
+ 虽然默认会使用 `~/.jarvis/treesitter` 目录,但您仍然可以指定自定义目录:
45
+
46
+ ```python
47
+ from jarvis.jarvis_treesitter import CodeDatabase
48
+
49
+ # 使用自定义语法文件目录
50
+ db = CodeDatabase(grammar_dir="/path/to/grammars")
51
+
52
+ # 不自动下载缺失的语法文件
53
+ db = CodeDatabase(auto_download=False)
54
+ ```
55
+
56
+ ### 手动下载语法文件
57
+
58
+ ```python
59
+ from jarvis.jarvis_treesitter import setup_default_grammars, GrammarBuilder, LanguageType, DEFAULT_GRAMMAR_DIR
60
+
61
+ # 下载所有支持的语言的语法文件到默认目录 (~/.jarvis/treesitter)
62
+ setup_default_grammars()
63
+
64
+ # 或者使用自定义目录
65
+ grammar_dir = "/path/to/grammars"
66
+ builder = GrammarBuilder(grammar_dir)
67
+ builder.ensure_all_grammars() # 下载所有语言
68
+ builder.ensure_grammar(LanguageType.PYTHON) # 只下载特定语言
69
+
70
+ # 查看默认语法文件目录
71
+ print(DEFAULT_GRAMMAR_DIR) # 输出: ~/.jarvis/treesitter
72
+ ```
73
+
74
+ ## 命令行工具
75
+
76
+ 提供了一个示例脚本 `example.py` 演示基本用法:
77
+
78
+ ```bash
79
+ # 索引当前目录并查找名为 "main" 的符号
80
+ python -m jarvis.jarvis_treesitter.example --dir . --symbol main
81
+
82
+ # 只索引Python文件
83
+ python -m jarvis.jarvis_treesitter.example --dir . --ext .py --symbol main
84
+
85
+ # 使用自定义语法文件目录(默认是 ~/.jarvis/treesitter)
86
+ python -m jarvis.jarvis_treesitter.example --dir . --grammar-dir /path/to/grammars --symbol main
87
+
88
+ # 不自动下载语法文件
89
+ python -m jarvis.jarvis_treesitter.example --dir . --no-download --symbol main
90
+ ```
91
+
92
+ ## 语法文件位置
93
+
94
+ 默认情况下,所有tree-sitter语法文件将保存在 `~/.jarvis/treesitter` 目录中。这些文件只需要下载和编译一次,后续使用时会自动加载。
95
+
96
+ ## 支持的语言
97
+
98
+ | 语言 | 文件扩展名 | 支持的符号类型 |
99
+ |--------|--------------------------|--------------------------------------------------|
100
+ | Python | .py | 函数、类、变量、导入、方法 |
101
+ | C | .c, .h | 函数、结构体、枚举、类型定义、宏、变量 |
102
+ | C++ | .cpp, .hpp, .cc, .hh | 函数、类、结构体、枚举、命名空间、模板、变量 |
103
+ | Go | .go | 函数、结构体、接口、包、导入、变量 |
104
+ | Rust | .rs | 函数、结构体、枚举、特征、实现、模块、变量 |
@@ -0,0 +1,20 @@
1
+ """Tree-sitter based code database for fast symbol lookup."""
2
+
3
+ __version__ = "0.1.0"
4
+
5
+ from .database import CodeDatabase
6
+ from .symbol import Symbol, SymbolType, SymbolLocation
7
+ from .language import LanguageType, LanguageConfig
8
+ from .grammar_builder import GrammarBuilder, setup_default_grammars, DEFAULT_GRAMMAR_DIR
9
+
10
+ __all__ = [
11
+ "CodeDatabase",
12
+ "Symbol",
13
+ "SymbolType",
14
+ "SymbolLocation",
15
+ "LanguageType",
16
+ "LanguageConfig",
17
+ "GrammarBuilder",
18
+ "setup_default_grammars",
19
+ "DEFAULT_GRAMMAR_DIR",
20
+ ]