--- phase: 13 plan: 07 type: execute wave: 2 depends_on: - 13-01 - 13-02 - 13-03 - 13-04 - 13-05 - 13-06 files_modified: - kb_workflow/pipeline/processors/srt.py - tests/test_srt_processor_integration.py autonomous: true requirements: - CONFIG-03 must_haves: truths: - "SRTProcessor 支持启用/禁用 LLM 语义分段" - "启用时使用 SemanticSegmentationEngine,禁用时使用规则分段" - "知乎/文章平台继续使用 AnalyzeProcessor,不受影响" artifacts: - path: "kb_workflow/pipeline/processors/srt.py" provides: "SRTProcessor 集成 LLM 分段" min_lines: 350 exports: ["SRTProcessor.__init__", "SRTProcessor.process"] - path: "tests/test_srt_processor_integration.py" provides: "SRTProcessor 集成测试" min_lines: 100 key_links: - from: "SRTProcessor.__init__" to: "SemanticSegmentationEngine" via: "enable_semantic_segmentation 参数" pattern: "enable_semantic.*segmentation" - from: "SRTProcessor.process" to: "SemanticSegmentationEngine.segment" via: "条件分支选择分段方式" pattern: "if.*semantic.*else.*rule" - from: "Pipeline.run" to: "AnalyzeProcessor/SRTProcessor" via: "平台路由逻辑(保持不变)" pattern: "platform.*bilibili.*srt" --- 集成 SemanticSegmentationEngine 到 SRTProcessor 目的:让 SRTProcessor 支持 LLM 语义分段(可选),保持向后兼容 输出:更新的 SRTProcessor,集成测试 @/home/starmen1265/.claude/get-shit-done/workflows/execute-plan.md @/home/starmen1265/.claude/get-shit-done/templates/summary.md @.planning/phases/13-llm/13-CONTEXT.md @.planning/phases/13-llm/13-RESEARCH.md # User Decision (Locked) - **SRTProcessor 集成**: 配置开关控制 — 新增 `enable_semantic_segmentation` 参数,True 时用 LLM 分段,False 时保留规则分段作为后备选项 - **现有 SemanticSegmenter**: 完全替代 — 新代码完全替代旧代码,SemanticSegmenter 被 SemanticSegmentationEngine 取代 # Existing Code (from srt.py) ```python class SRTProcessor(BaseProcessor): def __init__( self, state_manager: Optional[WorkflowStateManager] = None, enable_correction: bool = True ): ... self.segmenter = SemanticSegmenter() # 规则分段 ``` Task 1: 集成 SemanticSegmentationEngine 到 SRTProcessor kb_workflow/pipeline/processors/srt.py, tests/test_srt_processor_integration.py 更新 SRTProcessor,集成 LLM 语义分段: 1. 在 __init__ 中添加 `enable_semantic_segmentation` 参数: ```python def __init__( self, state_manager: Optional[WorkflowStateManager] = None, enable_correction: bool = True, enable_semantic_segmentation: bool = False # 新增参数 ): super().__init__(state_manager) self.enable_correction = enable_correction self.enable_semantic_segmentation = enable_semantic_segmentation # 初始化分段器 if enable_semantic_segmentation: try: from kb_workflow.srt.semantic_engine import SemanticSegmentationEngine from kb_workflow.deep_analyzer import SimpleAIClient ai_client = SimpleAIClient() if ai_client.api_key: self.segmenter = SemanticSegmentationEngine(ai_client=ai_client) logger.info("Semantic segmentation enabled") else: logger.info("AI API key not configured, using rule-based segmenter") self.segmenter = SemanticSegmenter() except Exception as e: logger.warning(f"Failed to initialize semantic engine: {e}, using rule-based segmenter") self.segmenter = SemanticSegmenter() else: self.segmenter = SemanticSegmenter() # 初始化纠错器(保持不变) ... ``` 2. 更新 process() 方法中的分段调用: ```python # Step 2: 语义分段 logger.info("Segmenting subtitles into paragraphs") if self.enable_semantic_segmentation and isinstance(self.segmenter, SemanticSegmentationEngine): # LLM 语义分段 paragraphs = self.segmenter.segment(segments, metadata=metadata) else: # 规则分段 paragraphs = self.segmenter.segment(segments) logger.info(f"Generated {len(paragraphs)} paragraphs") ``` 3. 更新 _generate_markdown() 以支持带标题的分段: ```python def _generate_markdown( self, output_file: Path, metadata: Dict[str, Any], paragraphs: List[str], # 或 List[VideoSegment] tags: List[str] ) -> None: ... # 如果 paragraphs 包含 VideoSegment(LLM 分段结果) if paragraphs and hasattr(paragraphs[0], 'title'): for seg in paragraphs: if seg.title: content_parts.append(f"## {seg.title}") content_parts.append(seg.content) content_parts.append("") else: # 规则分段(纯文本) content_parts.append("## 内容") content_parts.append("") for para in paragraphs: content_parts.append(para) content_parts.append("") ... ``` 4. 更新 SRTInput/Output Schema(如果需要): - 考虑是否需要添加 semantic_segmentation_enabled 字段 5. 创建集成测试: ```python # tests/test_srt_processor_integration.py def test_srt_processor_semantic_mode(): """测试 SRTProcessor 语义分段模式""" # mock AI client # enable_semantic_segmentation=True # 验证调用 SemanticSegmentationEngine def test_srt_processor_rule_based_mode(): """测试 SRTProcessor 规则分段模式""" # enable_semantic_segmentation=False # 验证使用 SemanticSegmenter def test_srt_processor_semantic_fallback(): """测试 AI 不可用时降级到规则分段""" # AI client 无效 # 验证降级到 SemanticSegmenter ``` 6. 确保 Pipeline 平台路由逻辑不受影响(Phase 12 已实现): - B站视频使用 SRTProcessor - 知乎/文章继续使用 AnalyzeProcessor - 不修改 Pipeline.run() 中的路由逻辑 pytest tests/test_srt_processor_integration.py::test_srt_processor_semantic_mode -x - SRTProcessor 支持 enable_semantic_segmentation 参数 - 启用时使用 SemanticSegmentationEngine - 禁用时使用 SemanticSegmenter(向后兼容) - AI 不可用时自动降级到规则分段 - _generate_markdown() 支持带标题的分段 - 集成测试验证所有场景 - 平台路由逻辑不受影响(知乎/文章继续使用 AnalyzeProcessor) - pytest tests/test_srt_processor_integration.py -v 全部通过 - pytest tests/test_srt_processor.py -v 现有测试不回归 - 平台路由逻辑不受影响 - 向后兼容(enable_semantic_segmentation=False 时行为不变) 1. SRTProcessor 支持 enable_semantic_segmentation 参数 2. 启用时使用 LLM 分段,禁用时使用规则分段 3. AI 不可用时自动降级 4. 集成测试验证所有场景 5. 平台路由逻辑不受影响 After completion, create `.planning/phases/13-llm/13-07-SUMMARY.md`