feat(analysis): unify code analysis workflow with default template support

- Consolidate instant analysis to always use analyze_code_with_rules method
- Add use_default_template parameter to automatically select database default template when none specified
- Implement fallback logic to use hardcoded defaults only when no database template available
- Auto-select default prompt template on frontend page load
- Add visual labels for language and template selection dropdowns
- Improve template selector UX by removing empty default option and showing default indicator
- Simplifies analysis flow by removing conditional branching between analyze_code and analyze_code_with_rules
This commit is contained in:
lintsinghua 2025-12-09 23:13:06 +08:00
parent 4d71ed546a
commit 11107ea455
3 changed files with 39 additions and 16 deletions

View File

@ -437,14 +437,13 @@ async def instant_analysis(
try:
# 如果指定了提示词模板,使用自定义分析
if req.prompt_template_id:
result = await llm_service.analyze_code_with_rules(
req.code, req.language,
prompt_template_id=req.prompt_template_id,
db_session=db
)
else:
result = await llm_service.analyze_code(req.code, req.language)
# 统一使用 analyze_code_with_rules会自动使用默认模板
result = await llm_service.analyze_code_with_rules(
req.code, req.language,
prompt_template_id=req.prompt_template_id,
db_session=db,
use_default_template=True # 没有指定模板时使用数据库中的默认模板
)
except Exception as e:
# 分析失败,返回错误信息
error_msg = str(e)

View File

@ -779,7 +779,8 @@ Please analyze the following code:
language: str,
rule_set_id: Optional[str] = None,
prompt_template_id: Optional[str] = None,
db_session = None
db_session = None,
use_default_template: bool = True
) -> Dict[str, Any]:
"""
使用指定的规则集和提示词模板分析代码
@ -790,6 +791,7 @@ Please analyze the following code:
rule_set_id: 规则集ID可选
prompt_template_id: 提示词模板ID可选
db_session: 数据库会话
use_default_template: 当没有指定模板时是否使用数据库中的默认模板
"""
custom_prompt = None
rules = None
@ -797,10 +799,10 @@ Please analyze the following code:
if db_session:
from sqlalchemy.future import select
from sqlalchemy.orm import selectinload
from app.models.prompt_template import PromptTemplate
# 获取提示词模板
if prompt_template_id:
from app.models.prompt_template import PromptTemplate
result = await db_session.execute(
select(PromptTemplate).where(PromptTemplate.id == prompt_template_id)
)
@ -808,6 +810,20 @@ Please analyze the following code:
if template:
output_language = self._get_output_language()
custom_prompt = template.content_zh if output_language == 'zh-CN' else template.content_en
elif use_default_template:
# 没有指定模板时,使用数据库中的默认模板
result = await db_session.execute(
select(PromptTemplate).where(
PromptTemplate.is_default == True,
PromptTemplate.is_active == True,
PromptTemplate.template_type == 'system'
)
)
template = result.scalar_one_or_none()
if template:
output_language = self._get_output_language()
custom_prompt = template.content_zh if output_language == 'zh-CN' else template.content_en
logger.info(f"📋 使用默认提示词模板: {template.name}")
# 获取规则集
if rule_set_id:
@ -836,7 +852,7 @@ Please analyze the following code:
if custom_prompt:
return await self.analyze_code_with_custom_prompt(code, language, custom_prompt, rules)
# 否则使用默认分析
# 否则使用硬编码的默认分析(兜底)
return await self.analyze_code(code, language)

View File

@ -82,6 +82,13 @@ export default function InstantAnalysis() {
try {
const res = await getPromptTemplates({ is_active: true });
setPromptTemplates(res.items);
// 自动选中默认模板
const defaultTemplate = res.items.find(t => t.is_default);
if (defaultTemplate) {
setSelectedPromptTemplateId(defaultTemplate.id);
} else if (res.items.length > 0) {
setSelectedPromptTemplateId(res.items[0].id);
}
} catch (error) {
console.error("加载提示词模板失败:", error);
}
@ -712,7 +719,8 @@ class UserManager {
<div className="p-6 space-y-4">
{/* 工具栏 */}
<div className="flex flex-col sm:flex-row gap-3">
<div className="flex-1">
<div className="flex-1 space-y-1">
<label className="text-xs font-bold text-gray-600 uppercase font-mono"></label>
<Select value={language} onValueChange={setLanguage}>
<SelectTrigger className="h-10 retro-input rounded-none border-2 border-black shadow-[2px_2px_0px_0px_rgba(0,0,0,1)] focus:ring-0">
<SelectValue placeholder="选择编程语言" />
@ -726,19 +734,19 @@ class UserManager {
</SelectContent>
</Select>
</div>
<div className="flex-1">
<div className="flex-1 space-y-1">
<label className="text-xs font-bold text-gray-600 uppercase font-mono"></label>
<Select value={selectedPromptTemplateId} onValueChange={setSelectedPromptTemplateId}>
<SelectTrigger className="h-10 retro-input rounded-none border-2 border-black shadow-[2px_2px_0px_0px_rgba(0,0,0,1)] focus:ring-0">
<div className="flex items-center gap-2">
<MessageSquare className="w-4 h-4 text-purple-600" />
<SelectValue placeholder="默认提示词" />
<SelectValue placeholder="选择提示词模板" />
</div>
</SelectTrigger>
<SelectContent className="rounded-none border-2 border-black shadow-[4px_4px_0px_0px_rgba(0,0,0,1)]">
<SelectItem value=""></SelectItem>
{promptTemplates.map((pt) => (
<SelectItem key={pt.id} value={pt.id}>
{pt.name}
{pt.name} {pt.is_default && '(默认)'}
</SelectItem>
))}
</SelectContent>