vcode-analysis 0.7.1__py3-none-any.whl → 0.9.0__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.
- analyzers/_tuning_context.py +2 -0
- analyzers/architecture.py +4 -22
- analyzers/code_review.py +137 -72
- analyzers/context_builder.py +79 -0
- analyzers/documentation.py +21 -100
- analyzers/security.py +302 -50
- analyzers/security_ast.py +370 -0
- cli.py +301 -10
- core/__init__.py +19 -2
- core/batch_analyzer.py +4 -17
- core/cache_manager.py +1 -1
- core/config.py +77 -9
- core/feedback_hub.py +85 -0
- core/metrics_store.py +250 -0
- core/prompt_manager.py +284 -0
- core/report_generator.py +60 -9
- parsers/c/ast_parser.py +81 -3
- parsers/kotlin/patterns.py +2 -2
- parsers/kotlin/regex_parser.py +60 -58
- parsers/sql_parser.py +437 -0
- prompts/__init__.py +1 -0
- prompts/architecture.md +27 -0
- prompts/batch_review.md +22 -0
- prompts/code_review.md +31 -0
- prompts/code_review_v2.md +37 -0
- prompts/commit_review.md +49 -0
- prompts/documentation/api.md +28 -0
- prompts/documentation/module.md +33 -0
- prompts/documentation/readme.md +42 -0
- prompts/mcp/code_review_checklist.md +16 -0
- prompts/mcp/mr_review_template.md +15 -0
- prompts/mcp/vuln_tuning_advisor.md +14 -0
- prompts/security_analysis.md +61 -0
- {vcode_analysis-0.7.1.dist-info → vcode_analysis-0.9.0.dist-info}/METADATA +142 -10
- vcode_analysis-0.9.0.dist-info/RECORD +69 -0
- {vcode_analysis-0.7.1.dist-info → vcode_analysis-0.9.0.dist-info}/WHEEL +1 -1
- {vcode_analysis-0.7.1.dist-info → vcode_analysis-0.9.0.dist-info}/entry_points.txt +1 -0
- {vcode_analysis-0.7.1.dist-info → vcode_analysis-0.9.0.dist-info}/top_level.txt +2 -0
- vcode_mcp/__init__.py +1 -0
- vcode_mcp/prompt_handler.py +21 -0
- vcode_mcp/resource_handler.py +44 -0
- vcode_mcp/server.py +184 -0
- vcode_mcp/tool_handler.py +234 -0
- vcode_mcp/transports/__init__.py +1 -0
- vcode_analysis-0.7.1.dist-info/RECORD +0 -44
- {vcode_analysis-0.7.1.dist-info → vcode_analysis-0.9.0.dist-info}/licenses/LICENSE +0 -0
analyzers/architecture.py
CHANGED
|
@@ -10,6 +10,7 @@ import re
|
|
|
10
10
|
from pathlib import Path
|
|
11
11
|
|
|
12
12
|
from core.analyzer import Analyzer, FileInfo, AnalysisResult
|
|
13
|
+
from core.prompt_manager import get_prompt_manager
|
|
13
14
|
from core.json_utils import remove_trailing_commas
|
|
14
15
|
|
|
15
16
|
# Python AST 解析器(可选依赖)
|
|
@@ -90,27 +91,7 @@ class ArchitectureResult:
|
|
|
90
91
|
suggestions: list[str]
|
|
91
92
|
|
|
92
93
|
|
|
93
|
-
#
|
|
94
|
-
ARCH_ANALYSIS_PROMPT = """分析以下项目的架构信息并提供改进建议:
|
|
95
|
-
|
|
96
|
-
项目统计:
|
|
97
|
-
- 文件数: {total_files}
|
|
98
|
-
- 代码行数: {total_lines}
|
|
99
|
-
- 语言分布: {languages}
|
|
100
|
-
- 模块数: {modules_count}
|
|
101
|
-
- 循环依赖: {circular_deps}
|
|
102
|
-
|
|
103
|
-
模块列表:
|
|
104
|
-
{modules_summary}
|
|
105
|
-
|
|
106
|
-
请分析项目的架构质量,包括:
|
|
107
|
-
1. 模块化程度评价
|
|
108
|
-
2. 依赖关系分析
|
|
109
|
-
3. 潜在的架构问题
|
|
110
|
-
4. 改进建议
|
|
111
|
-
|
|
112
|
-
以 JSON 格式输出:
|
|
113
|
-
{{"summary": "架构评价", "suggestions": ["建议1", "建议2"]}}"""
|
|
94
|
+
# 架构分析提示词已迁移到 prompts/architecture.md
|
|
114
95
|
|
|
115
96
|
|
|
116
97
|
# 导入语句正则表达式
|
|
@@ -481,7 +462,8 @@ class ArchitectureAnalyzer:
|
|
|
481
462
|
for m in self.modules[:20] # 限制数量
|
|
482
463
|
])
|
|
483
464
|
|
|
484
|
-
prompt =
|
|
465
|
+
prompt = get_prompt_manager().render(
|
|
466
|
+
"architecture",
|
|
485
467
|
total_files=len(self.modules),
|
|
486
468
|
total_lines=total_lines,
|
|
487
469
|
languages=dict(languages),
|
analyzers/code_review.py
CHANGED
|
@@ -9,6 +9,12 @@ if TYPE_CHECKING:
|
|
|
9
9
|
from .context_builder import AnalysisContext
|
|
10
10
|
|
|
11
11
|
|
|
12
|
+
def _get_pm():
|
|
13
|
+
"""延迟导入 PromptManager,避免触发 core/__init__ → batch_analyzer → analyzers.code_review 的循环导入"""
|
|
14
|
+
from core.prompt_manager import get_prompt_manager
|
|
15
|
+
return get_prompt_manager()
|
|
16
|
+
|
|
17
|
+
|
|
12
18
|
@dataclass
|
|
13
19
|
class CodeReviewResult:
|
|
14
20
|
"""代码审查结果"""
|
|
@@ -28,58 +34,13 @@ def _add_line_numbers(content: str) -> str:
|
|
|
28
34
|
return '\n'.join(numbered_lines)
|
|
29
35
|
|
|
30
36
|
|
|
31
|
-
# 代码审查提示词模板
|
|
32
|
-
CODE_REVIEW_PROMPT = """请审查以下 {language} 代码:
|
|
33
|
-
|
|
34
|
-
文件: {file_path}
|
|
35
|
-
|
|
36
|
-
```{language}
|
|
37
|
-
{content}
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
请进行代码审查,必须严格按照以下 JSON 格式输出,不要输出任何其他内容:
|
|
41
|
-
|
|
42
|
-
{{"score": 评分1-10, "issues": [{{"type": "问题类型", "severity": "high/medium/low", "line": 行号, "description": "问题描述"}}], "suggestions": ["改进建议"], "summary": "代码评价总结"}}"""
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
# 带上下文的代码审查提示词模板
|
|
46
|
-
CODE_REVIEW_PROMPT_V2 = """请审查以下 {language} 代码:
|
|
47
|
-
|
|
48
|
-
文件: {file_path}
|
|
49
|
-
|
|
50
|
-
{context_section}
|
|
51
|
-
|
|
52
|
-
## 当前文件代码
|
|
53
|
-
```{language}
|
|
54
|
-
{content}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
请进行代码审查,必须严格按照以下 JSON 格式输出,不要输出任何其他内容:
|
|
58
|
-
|
|
59
|
-
{{"score": 评分1-10, "issues": [{{"type": "问题类型", "severity": "high/medium/low", "line": 行号, "description": "问题描述", "context": "相关上下文说明(可选)"}}], "suggestions": ["改进建议"], "summary": "代码评价总结", "cross_file_issues": ["跨文件问题(可选)"]}}"""
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
COMMIT_REVIEW_PROMPT = """你是一位代码审查专家。请审查以下 Git 提交的变更。
|
|
63
|
-
|
|
64
|
-
提交信息: {commit_message}
|
|
65
|
-
作者: {author}
|
|
66
|
-
|
|
67
|
-
变更内容:
|
|
68
|
-
{diff}
|
|
69
|
-
|
|
70
|
-
请分析:
|
|
71
|
-
1. 变更的目的和影响
|
|
72
|
-
2. 潜在的问题或风险
|
|
73
|
-
3. 代码风格和最佳实践
|
|
74
|
-
4. 改进建议
|
|
75
|
-
|
|
76
|
-
以 Markdown 格式输出审查报告。"""
|
|
77
|
-
|
|
78
|
-
|
|
79
37
|
class CodeReviewAnalyzer:
|
|
80
38
|
"""代码审查分析器"""
|
|
81
39
|
|
|
82
|
-
def __init__(self, analyzer: Analyzer):
|
|
40
|
+
def __init__(self, analyzer: Analyzer, metrics_store=None):
|
|
41
|
+
self.analyzer = analyzer
|
|
42
|
+
self.metrics_store = metrics_store
|
|
43
|
+
self._current_run_id = None
|
|
83
44
|
self.analyzer = analyzer
|
|
84
45
|
|
|
85
46
|
def review_file(self, file_info: FileInfo) -> CodeReviewResult:
|
|
@@ -90,11 +51,13 @@ class CodeReviewAnalyzer:
|
|
|
90
51
|
numbered_content = _add_line_numbers(content)
|
|
91
52
|
|
|
92
53
|
# 构建提示词
|
|
93
|
-
prompt =
|
|
54
|
+
prompt = _get_pm().render(
|
|
55
|
+
"code_review",
|
|
94
56
|
file_path=file_info.relative_path,
|
|
95
57
|
language=file_info.language,
|
|
96
58
|
content=numbered_content,
|
|
97
59
|
)
|
|
60
|
+
prompt = _get_pm().append_tuning_section(prompt, "code_review")
|
|
98
61
|
|
|
99
62
|
# 调用 LLM
|
|
100
63
|
response = self.analyzer.llm.simple_chat(prompt)
|
|
@@ -135,6 +98,27 @@ class CodeReviewAnalyzer:
|
|
|
135
98
|
content = content.split("```")[1].split("```")[0]
|
|
136
99
|
|
|
137
100
|
data = json.loads(remove_trailing_commas(content.strip()))
|
|
101
|
+
|
|
102
|
+
# 落库到 MetricsStore(可选)
|
|
103
|
+
if self.metrics_store and self._current_run_id and data.get("issues"):
|
|
104
|
+
from core.metrics_store import IssueRecord
|
|
105
|
+
for issue in data["issues"]:
|
|
106
|
+
try:
|
|
107
|
+
self.metrics_store.record_issue(
|
|
108
|
+
run_id=self._current_run_id,
|
|
109
|
+
issue=IssueRecord(
|
|
110
|
+
rule_id=issue.get("type", "review:unknown"),
|
|
111
|
+
severity=issue.get("severity", "low"),
|
|
112
|
+
analyzer="code_review",
|
|
113
|
+
file_path=file_info.relative_path,
|
|
114
|
+
line_start=issue.get("line", 0),
|
|
115
|
+
line_end=issue.get("line", 0),
|
|
116
|
+
code_snippet=issue.get("description", "")[:500],
|
|
117
|
+
),
|
|
118
|
+
)
|
|
119
|
+
except Exception:
|
|
120
|
+
pass
|
|
121
|
+
|
|
138
122
|
return CodeReviewResult(
|
|
139
123
|
file_path=file_info.relative_path,
|
|
140
124
|
score=data.get("score", 0),
|
|
@@ -152,16 +136,18 @@ class CodeReviewAnalyzer:
|
|
|
152
136
|
summary=result.content,
|
|
153
137
|
)
|
|
154
138
|
|
|
155
|
-
def review_commit(self, commit_hash: str) ->
|
|
139
|
+
def review_commit(self, commit_hash: str) -> CodeReviewResult:
|
|
156
140
|
"""审查指定提交"""
|
|
141
|
+
import json
|
|
157
142
|
git = self.analyzer.git
|
|
158
143
|
if not git:
|
|
159
|
-
return
|
|
160
|
-
file_path=
|
|
161
|
-
|
|
144
|
+
return CodeReviewResult(
|
|
145
|
+
file_path=commit_hash,
|
|
146
|
+
score=0,
|
|
147
|
+
issues=[{"type": "error", "severity": "high", "description": "目标路径不是有效的 Git 仓库"}],
|
|
148
|
+
suggestions=[],
|
|
149
|
+
summary="分析失败",
|
|
162
150
|
success=False,
|
|
163
|
-
content="",
|
|
164
|
-
errors=["目标路径不是有效的 Git 仓库"],
|
|
165
151
|
)
|
|
166
152
|
|
|
167
153
|
commit_info = git.get_commit_info(commit_hash)
|
|
@@ -174,26 +160,104 @@ class CodeReviewAnalyzer:
|
|
|
174
160
|
diff_content += f"状态: {diff.status} (+{diff.additions}/-{diff.deletions})\n"
|
|
175
161
|
diff_content += f"```diff\n{diff.diff}\n```\n"
|
|
176
162
|
|
|
177
|
-
|
|
163
|
+
# 构建数据库 Schema 上下文:解析本次提交涉及的所有 SQL/DDL 文件
|
|
164
|
+
schema_context = self._build_commit_schema_context(git, commit_hash, diffs)
|
|
165
|
+
|
|
166
|
+
prompt = _get_pm().render(
|
|
167
|
+
"commit_review",
|
|
178
168
|
commit_message=commit_info.message,
|
|
179
169
|
author=commit_info.author,
|
|
180
170
|
diff=diff_content,
|
|
171
|
+
schema_context=schema_context,
|
|
181
172
|
)
|
|
173
|
+
prompt = _get_pm().append_tuning_section(prompt, "commit_review")
|
|
182
174
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
"
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
175
|
+
try:
|
|
176
|
+
response = self.analyzer.llm.simple_chat(prompt)
|
|
177
|
+
except Exception as e:
|
|
178
|
+
return CodeReviewResult(
|
|
179
|
+
file_path=commit_hash,
|
|
180
|
+
score=0,
|
|
181
|
+
issues=[{"type": "error", "severity": "high", "description": str(e)}],
|
|
182
|
+
suggestions=[],
|
|
183
|
+
summary="LLM 调用失败",
|
|
184
|
+
success=False,
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
# 解析 JSON 结果
|
|
188
|
+
try:
|
|
189
|
+
content = response
|
|
190
|
+
if "```json" in content:
|
|
191
|
+
content = content.split("```json")[1].split("```")[0]
|
|
192
|
+
elif "```" in content:
|
|
193
|
+
content = content.split("```")[1].split("```")[0]
|
|
194
|
+
|
|
195
|
+
data = json.loads(remove_trailing_commas(content.strip()))
|
|
196
|
+
|
|
197
|
+
# 落库到 MetricsStore(可选)
|
|
198
|
+
if self.metrics_store and self._current_run_id and data.get("issues"):
|
|
199
|
+
from core.metrics_store import IssueRecord
|
|
200
|
+
for issue in data["issues"]:
|
|
201
|
+
try:
|
|
202
|
+
self.metrics_store.record_issue(
|
|
203
|
+
run_id=self._current_run_id,
|
|
204
|
+
issue=IssueRecord(
|
|
205
|
+
rule_id=issue.get("type", "review:unknown"),
|
|
206
|
+
severity=issue.get("severity", "low"),
|
|
207
|
+
analyzer="code_review",
|
|
208
|
+
file_path=issue.get("file", commit_hash),
|
|
209
|
+
line_start=issue.get("line", 0),
|
|
210
|
+
line_end=issue.get("line", 0),
|
|
211
|
+
code_snippet=issue.get("description", "")[:500],
|
|
212
|
+
),
|
|
213
|
+
)
|
|
214
|
+
except Exception:
|
|
215
|
+
pass
|
|
216
|
+
|
|
217
|
+
return CodeReviewResult(
|
|
218
|
+
file_path=commit_hash,
|
|
219
|
+
score=data.get("score", 0),
|
|
220
|
+
issues=data.get("issues", []),
|
|
221
|
+
suggestions=data.get("suggestions", []),
|
|
222
|
+
summary=data.get("summary", ""),
|
|
223
|
+
success=True,
|
|
224
|
+
)
|
|
225
|
+
except json.JSONDecodeError:
|
|
226
|
+
return CodeReviewResult(
|
|
227
|
+
file_path=commit_hash,
|
|
228
|
+
score=0,
|
|
229
|
+
issues=[],
|
|
230
|
+
suggestions=[],
|
|
231
|
+
summary=response,
|
|
232
|
+
success=True,
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
def _build_commit_schema_context(self, git, commit_hash: str, diffs: list) -> str:
|
|
236
|
+
"""解析本次提交涉及的所有 SQL/DDL 文件,生成 schema 摘要文本。
|
|
237
|
+
|
|
238
|
+
仅在提交包含 SQL 文件时返回非空摘要;否则返回空字符串,
|
|
239
|
+
由 commit_review.md 中的 {% if schema_context %} 控制是否展示。
|
|
240
|
+
"""
|
|
241
|
+
sql_files = [d.file_path for d in diffs
|
|
242
|
+
if d.file_path.lower().endswith((".sql", ".ddl"))
|
|
243
|
+
and d.status != "D"] # 跳过被删除的文件
|
|
244
|
+
if not sql_files:
|
|
245
|
+
return ""
|
|
246
|
+
|
|
247
|
+
from parsers import sql_parser as sp
|
|
248
|
+
schemas: dict[str, sp.Schema] = {}
|
|
249
|
+
for path in sql_files:
|
|
250
|
+
try:
|
|
251
|
+
content = git.get_file_content(path, commit_hash)
|
|
252
|
+
except Exception:
|
|
253
|
+
content = ""
|
|
254
|
+
if content:
|
|
255
|
+
schemas[path] = sp.parse_sql(content, source=path)
|
|
256
|
+
|
|
257
|
+
if not schemas:
|
|
258
|
+
return ""
|
|
259
|
+
|
|
260
|
+
return sp.schema_summary(schemas)
|
|
197
261
|
|
|
198
262
|
def review_files(self, file_infos: list[FileInfo]) -> list[CodeReviewResult]:
|
|
199
263
|
"""审查多个文件"""
|
|
@@ -232,7 +296,8 @@ class CodeReviewAnalyzer:
|
|
|
232
296
|
)
|
|
233
297
|
|
|
234
298
|
# 构建提示词
|
|
235
|
-
prompt =
|
|
299
|
+
prompt = _get_pm().render(
|
|
300
|
+
"code_review_v2",
|
|
236
301
|
file_path=file_info.relative_path,
|
|
237
302
|
language=file_info.language,
|
|
238
303
|
context_section=context_section,
|
analyzers/context_builder.py
CHANGED
|
@@ -11,6 +11,7 @@ from collections import defaultdict
|
|
|
11
11
|
import re
|
|
12
12
|
|
|
13
13
|
from core.analyzer import Analyzer, FileInfo
|
|
14
|
+
from parsers import sql_parser as _sql_parser
|
|
14
15
|
|
|
15
16
|
|
|
16
17
|
@dataclass
|
|
@@ -21,6 +22,8 @@ class AnalysisContext:
|
|
|
21
22
|
reverse_dependencies: dict[str, list[str]] # 反向依赖(被谁引用)
|
|
22
23
|
language_stats: dict[str, int] # 语言分布统计
|
|
23
24
|
module_info: dict[str, dict] # 模块信息摘要
|
|
25
|
+
sql_schema: dict = field(default_factory=dict) # {filename: parsers.sql_parser.Schema}
|
|
26
|
+
sql_related_files: dict = field(default_factory=dict) # {sql_file: [引用其表的代码文件]}
|
|
24
27
|
|
|
25
28
|
|
|
26
29
|
# 导入语句正则表达式(复用自 architecture.py)
|
|
@@ -109,14 +112,67 @@ class ContextBuilder:
|
|
|
109
112
|
# 5. 收集模块信息
|
|
110
113
|
module_info = self._collect_module_info(file_infos, file_deps)
|
|
111
114
|
|
|
115
|
+
# 6. 解析 SQL 文件并建立 SQL <-> 代码关联
|
|
116
|
+
sql_schema, sql_related_files = self._build_sql_relations(file_infos)
|
|
117
|
+
|
|
112
118
|
return AnalysisContext(
|
|
113
119
|
project_structure=project_structure,
|
|
114
120
|
file_dependencies=file_deps,
|
|
115
121
|
reverse_dependencies=reverse_deps,
|
|
116
122
|
language_stats=language_stats,
|
|
117
123
|
module_info=module_info,
|
|
124
|
+
sql_schema=sql_schema,
|
|
125
|
+
sql_related_files=sql_related_files,
|
|
118
126
|
)
|
|
119
127
|
|
|
128
|
+
def _build_sql_relations(
|
|
129
|
+
self, file_infos: list
|
|
130
|
+
) -> tuple[dict, dict]:
|
|
131
|
+
"""解析所有 .sql/.ddl 文件,建立表名 <-> 引用该表的代码文件 的关联。
|
|
132
|
+
|
|
133
|
+
Returns:
|
|
134
|
+
(sql_schema, sql_related_files)
|
|
135
|
+
- sql_schema: {filename: Schema}
|
|
136
|
+
- sql_related_files: {sql_file: [代码文件路径...]}
|
|
137
|
+
"""
|
|
138
|
+
sql_schema: dict = {}
|
|
139
|
+
table_to_sql: dict[str, str] = {} # 小写表名 -> sql 文件
|
|
140
|
+
code_files: list = []
|
|
141
|
+
|
|
142
|
+
for fi in file_infos:
|
|
143
|
+
if fi.relative_path.lower().endswith((".sql", ".ddl")):
|
|
144
|
+
try:
|
|
145
|
+
content = self.analyzer.read_file_content(fi)
|
|
146
|
+
except Exception:
|
|
147
|
+
content = ""
|
|
148
|
+
if content:
|
|
149
|
+
schema = _sql_parser.parse_sql(content, source=fi.relative_path)
|
|
150
|
+
if schema.tables:
|
|
151
|
+
sql_schema[fi.relative_path] = schema
|
|
152
|
+
for tname in schema.tables:
|
|
153
|
+
table_to_sql.setdefault(tname.lower(), fi.relative_path)
|
|
154
|
+
else:
|
|
155
|
+
code_files.append(fi)
|
|
156
|
+
|
|
157
|
+
if not table_to_sql:
|
|
158
|
+
return sql_schema, {}
|
|
159
|
+
|
|
160
|
+
sql_related_files: dict = defaultdict(list)
|
|
161
|
+
for fi in code_files:
|
|
162
|
+
try:
|
|
163
|
+
content = self.analyzer.read_file_content(fi)
|
|
164
|
+
except Exception:
|
|
165
|
+
continue
|
|
166
|
+
if not content:
|
|
167
|
+
continue
|
|
168
|
+
hits = _sql_parser.find_sql_table_references(content, set(table_to_sql.keys()))
|
|
169
|
+
for tbl in hits:
|
|
170
|
+
sql_file = table_to_sql[tbl]
|
|
171
|
+
if fi.relative_path not in sql_related_files[sql_file]:
|
|
172
|
+
sql_related_files[sql_file].append(fi.relative_path)
|
|
173
|
+
|
|
174
|
+
return sql_schema, dict(sql_related_files)
|
|
175
|
+
|
|
120
176
|
def _build_project_structure(self, root: Path, max_depth: int = 3) -> str:
|
|
121
177
|
"""构建项目目录树字符串"""
|
|
122
178
|
lines = []
|
|
@@ -398,6 +454,12 @@ class ContextBuilder:
|
|
|
398
454
|
# 移除自身
|
|
399
455
|
related.discard(file_path)
|
|
400
456
|
|
|
457
|
+
# SQL <-> 代码关联:引用该 SQL 表定义的代码文件互为相关
|
|
458
|
+
related.update(context.sql_related_files.get(file_path, []))
|
|
459
|
+
for sql_file, code_files in context.sql_related_files.items():
|
|
460
|
+
if file_path in code_files:
|
|
461
|
+
related.add(sql_file)
|
|
462
|
+
|
|
401
463
|
return list(related)[:10] # 限制数量
|
|
402
464
|
|
|
403
465
|
def format_context_for_prompt(
|
|
@@ -450,4 +512,21 @@ class ContextBuilder:
|
|
|
450
512
|
lines.append(", ".join(deps[:10]))
|
|
451
513
|
lines.append("")
|
|
452
514
|
|
|
515
|
+
# 数据库 Schema 上下文:收集与当前文件相关的 SQL 文件
|
|
516
|
+
sql_files = set(context.sql_related_files.get(file_path, []))
|
|
517
|
+
for sql_file, code_files in context.sql_related_files.items():
|
|
518
|
+
if file_path in code_files:
|
|
519
|
+
sql_files.add(sql_file)
|
|
520
|
+
# 若当前文件本身就是 SQL 文件
|
|
521
|
+
if file_path in context.sql_schema:
|
|
522
|
+
sql_files.add(file_path)
|
|
523
|
+
if sql_files:
|
|
524
|
+
schemas = {f: context.sql_schema[f] for f in sql_files if f in context.sql_schema}
|
|
525
|
+
summary = _sql_parser.schema_summary(schemas)
|
|
526
|
+
lines.append("## 数据库 Schema 上下文")
|
|
527
|
+
lines.append("```")
|
|
528
|
+
lines.append(summary)
|
|
529
|
+
lines.append("```")
|
|
530
|
+
lines.append("")
|
|
531
|
+
|
|
453
532
|
return "\n".join(lines)
|
analyzers/documentation.py
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
from dataclasses import dataclass
|
|
4
4
|
from typing import Optional
|
|
5
5
|
from core.analyzer import Analyzer, FileInfo, AnalysisResult
|
|
6
|
+
from core.prompt_manager import get_prompt_manager
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
@dataclass
|
|
@@ -14,95 +15,6 @@ class DocumentationResult:
|
|
|
14
15
|
success: bool
|
|
15
16
|
|
|
16
17
|
|
|
17
|
-
# 文档生成提示词模板
|
|
18
|
-
MODULE_DOC_PROMPT = """你是一位技术文档专家。请为以下 {language} 模块生成详细的文档。
|
|
19
|
-
|
|
20
|
-
文件: {file_path}
|
|
21
|
-
|
|
22
|
-
```{language}
|
|
23
|
-
{content}
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
请生成以下内容:
|
|
27
|
-
|
|
28
|
-
## 模块概述
|
|
29
|
-
简要描述模块的用途和功能。
|
|
30
|
-
|
|
31
|
-
## 主要组件
|
|
32
|
-
列出主要的类、函数、常量及其用途。
|
|
33
|
-
|
|
34
|
-
## 使用示例
|
|
35
|
-
提供典型的使用示例代码。
|
|
36
|
-
|
|
37
|
-
## 依赖关系
|
|
38
|
-
列出该模块依赖的其他模块或库。
|
|
39
|
-
|
|
40
|
-
## 注意事项
|
|
41
|
-
使用时需要注意的事项或限制。
|
|
42
|
-
|
|
43
|
-
请以 Markdown 格式输出。"""
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
API_DOC_PROMPT = """你是一位 API 文档专家。请为以下代码生成 API 文档。
|
|
47
|
-
|
|
48
|
-
文件: {file_path}
|
|
49
|
-
语言: {language}
|
|
50
|
-
|
|
51
|
-
```{language}
|
|
52
|
-
{content}
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
请生成 OpenAPI/Swagger 风格的 API 文档,包括:
|
|
56
|
-
|
|
57
|
-
## 接口列表
|
|
58
|
-
对于每个公开的接口/函数/方法:
|
|
59
|
-
|
|
60
|
-
### {函数名}
|
|
61
|
-
- **描述**: 功能说明
|
|
62
|
-
- **参数**: 参数名、类型、描述、是否必填
|
|
63
|
-
- **返回值**: 类型、描述
|
|
64
|
-
- **示例**: 调用示例
|
|
65
|
-
|
|
66
|
-
请以 Markdown 格式输出。"""
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
README_PROMPT = """你是一位技术写作专家。请根据项目结构生成 README.md 文档。
|
|
70
|
-
|
|
71
|
-
项目信息:
|
|
72
|
-
- 名称: {project_name}
|
|
73
|
-
- 描述: {project_description}
|
|
74
|
-
|
|
75
|
-
主要文件:
|
|
76
|
-
{file_summaries}
|
|
77
|
-
|
|
78
|
-
请生成完整的 README.md,包括:
|
|
79
|
-
|
|
80
|
-
# {项目名称}
|
|
81
|
-
|
|
82
|
-
## 简介
|
|
83
|
-
项目简介和主要功能。
|
|
84
|
-
|
|
85
|
-
## 特性
|
|
86
|
-
主要特性和亮点。
|
|
87
|
-
|
|
88
|
-
## 安装
|
|
89
|
-
安装步骤和依赖要求。
|
|
90
|
-
|
|
91
|
-
## 快速开始
|
|
92
|
-
基本使用示例。
|
|
93
|
-
|
|
94
|
-
## 项目结构
|
|
95
|
-
目录结构说明。
|
|
96
|
-
|
|
97
|
-
## 贡献指南
|
|
98
|
-
如何参与项目开发。
|
|
99
|
-
|
|
100
|
-
## 许可证
|
|
101
|
-
开源许可证信息。
|
|
102
|
-
|
|
103
|
-
请以 Markdown 格式输出。"""
|
|
104
|
-
|
|
105
|
-
|
|
106
18
|
class DocumentationAnalyzer:
|
|
107
19
|
"""文档生成分析器"""
|
|
108
20
|
|
|
@@ -116,16 +28,20 @@ class DocumentationAnalyzer:
|
|
|
116
28
|
) -> DocumentationResult:
|
|
117
29
|
"""生成模块文档"""
|
|
118
30
|
try:
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
31
|
+
content = self.analyzer.read_file_content(file_info)
|
|
32
|
+
prompt = get_prompt_manager().render(
|
|
33
|
+
"documentation/module",
|
|
34
|
+
language=file_info.language,
|
|
35
|
+
file_path=file_info.relative_path,
|
|
36
|
+
content=content,
|
|
122
37
|
)
|
|
38
|
+
response = self.analyzer.llm.simple_chat(prompt)
|
|
123
39
|
|
|
124
40
|
return DocumentationResult(
|
|
125
41
|
file_path=file_info.relative_path,
|
|
126
42
|
doc_type=doc_type,
|
|
127
|
-
content=
|
|
128
|
-
success=
|
|
43
|
+
content=response,
|
|
44
|
+
success=True,
|
|
129
45
|
)
|
|
130
46
|
|
|
131
47
|
except Exception as e:
|
|
@@ -139,16 +55,20 @@ class DocumentationAnalyzer:
|
|
|
139
55
|
def generate_api_doc(self, file_info: FileInfo) -> DocumentationResult:
|
|
140
56
|
"""生成 API 文档"""
|
|
141
57
|
try:
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
58
|
+
content = self.analyzer.read_file_content(file_info)
|
|
59
|
+
prompt = get_prompt_manager().render(
|
|
60
|
+
"documentation/api",
|
|
61
|
+
language=file_info.language,
|
|
62
|
+
file_path=file_info.relative_path,
|
|
63
|
+
content=content,
|
|
145
64
|
)
|
|
65
|
+
response = self.analyzer.llm.simple_chat(prompt)
|
|
146
66
|
|
|
147
67
|
return DocumentationResult(
|
|
148
68
|
file_path=file_info.relative_path,
|
|
149
69
|
doc_type="api",
|
|
150
|
-
content=
|
|
151
|
-
success=
|
|
70
|
+
content=response,
|
|
71
|
+
success=True,
|
|
152
72
|
)
|
|
153
73
|
|
|
154
74
|
except Exception as e:
|
|
@@ -171,7 +91,8 @@ class DocumentationAnalyzer:
|
|
|
171
91
|
for summary in file_summaries:
|
|
172
92
|
summaries_text += f"- **{summary['path']}**: {summary['description']}\n"
|
|
173
93
|
|
|
174
|
-
prompt =
|
|
94
|
+
prompt = get_prompt_manager().render(
|
|
95
|
+
"documentation/readme",
|
|
175
96
|
project_name=project_name,
|
|
176
97
|
project_description=project_description,
|
|
177
98
|
file_summaries=summaries_text,
|