Przeglądaj źródła

feat(teacher.plan.review): SC-T-28 AI方案内容安全复核闭环(S-cfc-228)

- HealthPlanServiceImpl.approveAndPublish 发布前接入内容安全复核(命中即拦截,未通过不置 published,fail-closed)
- ContentFilterController 新增 POST /api/content/plan-safety 方案内容复核端点
- API_REFERENCE §4.57 SC-T-28 端点行同步
Sisyphus Agent 3 dni temu
rodzic
commit
da981a2d85

+ 27 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/ContentFilterController.java

@@ -10,6 +10,7 @@ import com.etotem.cfc.mapper.ArticleMapper;
 import com.etotem.cfc.mapper.FoodMapper;
 import com.etotem.cfc.mapper.HealthKnowledgeBaseMapper;
 import com.etotem.cfc.mapper.ProblemDomainMapper;
+import com.etotem.cfc.service.api.WechatServiceInterface;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import javax.annotation.Resource;
@@ -47,6 +48,10 @@ public class ContentFilterController {
     @Resource
     private ProblemDomainMapper problemDomainMapper;
 
+    /** SC-T-28:方案内容安全复核(复用微信内容安全原语) */
+    @Resource
+    private WechatServiceInterface contentSafetyService;
+
     @Operation(summary = "按 resultTags + 问题域 code 过滤文章/知识库/饮食")
     @PostMapping("/by-tags")
     public Result<Map<String, Object>> byTags(@RequestBody Map<String, Object> params) {
@@ -83,6 +88,28 @@ public class ContentFilterController {
         return Result.success(data);
     }
 
+    /**
+     * SC-T-28:方案内容安全复核端点。
+     *
+     * <p>对 AI 生成的方案正文做敏感词/合规检查,命中即拦截(safe=false,调用方不得发布)。</p>
+     *
+     * @param params 请求体,支持 {@code content}(方案正文,必填非空才复核)与可选的
+     *               {@code planId}(仅透传回显,便于审核日志关联)
+     */
+    @Operation(summary = "方案内容安全复核(SC-T-28)")
+    @PostMapping("/plan-safety")
+    public Result<Map<String, Object>> planSafety(@RequestBody(required = false) Map<String, Object> params) {
+        String content = params == null ? null : (params.get("content") == null ? null : params.get("content").toString());
+        boolean safe = content == null || content.trim().isEmpty() || contentSafetyService.checkText(content);
+        Map<String, Object> data = new HashMap<>();
+        data.put("safe", safe);
+        data.put("message", safe ? "内容安全" : "内容命中敏感词,未通过复核");
+        if (params != null) {
+            data.put("planId", params.get("planId"));
+        }
+        return Result.success(data);
+    }
+
     /** 文章:tags 字段 LIKE 任一标签 */
     private List<Article> filterArticles(Set<String> tags, int size) {
         if (tags.isEmpty()) {

+ 32 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/impl/HealthPlanServiceImpl.java

@@ -28,6 +28,7 @@ import com.etotem.cfc.service.AttributionAnalysisService;
 import com.etotem.cfc.service.CartService;
 import com.etotem.cfc.service.HealthPlanService;
 import com.etotem.cfc.service.ProductRecommendationService;
+import com.etotem.cfc.service.api.WechatServiceInterface;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.ResponseEntity;
@@ -101,6 +102,10 @@ public class HealthPlanServiceImpl implements HealthPlanService {
     @Resource
     private ProductMapper productMapper;
 
+    /** SC-T-28:AI 方案内容安全复核(命中即拦截,发布于复核通过后) */
+    @Resource
+    private WechatServiceInterface contentSafetyService;
+
     @Value("${langgraph.base-url:}")
     private String langgraphBaseUrl;
 
@@ -1928,6 +1933,8 @@ public class HealthPlanServiceImpl implements HealthPlanService {
         if (!"pending_review".equals(plan.getStatus()) && !"draft".equals(plan.getStatus())) {
             throw new RuntimeException("方案状态不允许审核");
         }
+        // SC-T-28:AI 方案内容安全复核 —— 命中即拦截,未通过前不得置为 published
+        assertPlanContentSafe(plan);
         plan.setStatus("published");
         plan.setReviewedBy(reviewedBy);
         plan.setReviewedAt(new Date());
@@ -1960,6 +1967,31 @@ public class HealthPlanServiceImpl implements HealthPlanService {
         return dto;
     }
 
+    /**
+     * SC-T-28:AI 方案内容安全复核(命中即拦截)。
+     *
+     * <p>方案正文({@code planContent})非空时须经内容安全原语 {@code checkText(String)} 复核;
+     * 命中敏感内容(返回 false)或内容安全依赖未配置(fail-closed)时,抛业务异常阻止发布,
+     * 保证状态不落库、不置为 published。</p>
+     */
+    private void assertPlanContentSafe(HealthPlan plan) {
+        String content = plan.getPlanContent();
+        if (content == null || content.trim().isEmpty()) {
+            return;
+        }
+        boolean safe;
+        if (contentSafetyService == null) {
+            // fail-closed:审核链路未配置内容安全依赖时不允许发布,避免绕过复核
+            safe = false;
+            log.warn("方案{}内容安全复核依赖未配置,按不通过处理(fail-closed)", plan.getId());
+        } else {
+            safe = contentSafetyService.checkText(content);
+        }
+        if (!safe) {
+            throw new RuntimeException("方案内容未通过内容安全复核,请修改后重新送审");
+        }
+    }
+
 @Override
     public HealthPlan rejectPlan(Long planId, Long reviewedBy, String comment) {
         HealthPlan plan = healthPlanMapper.selectById(planId);

+ 1 - 1
docs/superpowers/api/API_REFERENCE.md

@@ -2366,7 +2366,7 @@ Web 管理端定义活动海报模板(迁移381 `activity_poster_templates` 
 | SC-T-25 | 舌诊/情绪测评录入(规划师)⚠ | 舌诊:`HealthReportController /report/parse-preview`(type=tongue)+ `/report/confirm`(type=tongue)+ `/report/discard`;情绪:`EmotionCheckinController /api/mind/checkin/*`(create/list/latest/trend/stats/weekly-report/emotion/recognize) |
 | SC-T-26 | 精准营养方案制定 | `DietRecommendationController /api/diet/recommendation/*`(today/generate/update/regenerate/complete);`HealthReportController /api/health/nutrition/*`;`NutritionProductController /api/nutrition/product/*` |
 | SC-T-27 | 家庭天盘解读(规划师)⚠ | `TianpanController /api/tianpan/*`(dashboard/member/{memberId}/compatibility/annual-energy/daily-fortune/related-items/recalibrate);`TraditionalMirrorController /api/mind/traditional/*`;`MindFortuneController /api/mind/fortune/export`;`AdminFamilyOverviewController /api/admin/families/{id}/overview` |
-| SC-T-28 | AI 方案内容安全复核 | `PlanReviewController /review`;`GuidePlanReviewController approve/reject`;`ReviewCenterController /summary`;`ContentFilterController /api/content/*` |
+| SC-T-28 | AI 方案内容安全复核 | `PlanReviewController /review`;`GuidePlanReviewController approve/reject`;`ReviewCenterController /summary`;`ContentFilterController POST /api/content/plan-safety` |
 | SC-T-29 | 客户档案数据导出(合规)⚠ | `UserController POST /api/user/data-export`(个保法可携带权,`AccountCleanupService.exportUserData`);**`AdminOrgMemberController /api/admin/org/member/*` 无 export → 缺口 G-3** |
 | SC-T-30 | 测评录入触发五维维度变化 | `DanAssessmentController /result/record`;`EnergyController /api/energy/*`(overview/logs/grant/score-history/family-score-history) |
 | SC-T-31 | 手动维度打分覆盖/调整 | `ManualDimensionScoreController /api/family/manual-score/*`(submit/latest/history) |