From 11107ea4551414b2aa0f7a6268f10604e5de9b3e Mon Sep 17 00:00:00 2001 From: lintsinghua Date: Tue, 9 Dec 2025 23:13:06 +0800 Subject: [PATCH] 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 --- backend/app/api/v1/endpoints/scan.py | 15 +++++++-------- backend/app/services/llm/service.py | 22 +++++++++++++++++++--- frontend/src/pages/InstantAnalysis.tsx | 18 +++++++++++++----- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/backend/app/api/v1/endpoints/scan.py b/backend/app/api/v1/endpoints/scan.py index 4640502..4c31d87 100644 --- a/backend/app/api/v1/endpoints/scan.py +++ b/backend/app/api/v1/endpoints/scan.py @@ -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) diff --git a/backend/app/services/llm/service.py b/backend/app/services/llm/service.py index f6f33a8..610eaef 100644 --- a/backend/app/services/llm/service.py +++ b/backend/app/services/llm/service.py @@ -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) diff --git a/frontend/src/pages/InstantAnalysis.tsx b/frontend/src/pages/InstantAnalysis.tsx index 9837a5f..6f71171 100644 --- a/frontend/src/pages/InstantAnalysis.tsx +++ b/frontend/src/pages/InstantAnalysis.tsx @@ -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 {
{/* 工具栏 */}
-
+
+
-
+
+