From 8b07ef9dcd30f3417bbbe844164b7f16a5486175 Mon Sep 17 00:00:00 2001 From: starmen1265 Date: Tue, 17 Mar 2026 00:34:06 +0800 Subject: [PATCH] refactor(06-03): move sys.path manipulation to lazy import in analyze.py - Remove top-level sys.path.insert for kb-deep-analyzer - Add module-level flag _deep_analyzer_path_added for single execution - Move sys.path manipulation inside _run_analysis method - External dependency import now happens lazily at runtime Co-Authored-By: Claude Opus 4.6 --- kb_workflow/pipeline/processors/analyze.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/kb_workflow/pipeline/processors/analyze.py b/kb_workflow/pipeline/processors/analyze.py index 8a49f7d..83439dc 100644 --- a/kb_workflow/pipeline/processors/analyze.py +++ b/kb_workflow/pipeline/processors/analyze.py @@ -13,11 +13,6 @@ import sys from pathlib import Path from typing import Optional, Dict, Any -# Add kb-deep-analyzer to path for import -_kb_deep_analyzer_path = Path(__file__).parent.parent.parent / "kb-deep-analyzer" -if str(_kb_deep_analyzer_path) not in sys.path: - sys.path.insert(0, str(_kb_deep_analyzer_path)) - from kb_workflow.pipeline.config import get_settings from kb_workflow.pipeline.schemas import AnalyzeInput, AnalyzeOutput from kb_workflow.pipeline.state import WorkflowStateManager @@ -25,6 +20,9 @@ from kb_workflow.pipeline.processors.base import BaseProcessor logger = logging.getLogger(__name__) +# Flag to ensure sys.path is only modified once for kb-deep-analyzer +_deep_analyzer_path_added = False + class AnalyzeProcessor(BaseProcessor): """Analyze 处理器 - AI 深度内容分析 @@ -201,7 +199,14 @@ class AnalyzeProcessor(BaseProcessor): Returns: AnalysisResult 对象 """ - # Lazy import to avoid dependency issues + # Lazy import with delayed sys.path modification for external kb-deep-analyzer + global _deep_analyzer_path_added + if not _deep_analyzer_path_added: + _kb_deep_analyzer_path = Path(__file__).parent.parent.parent / "kb-deep-analyzer" + if str(_kb_deep_analyzer_path) not in sys.path: + sys.path.insert(0, str(_kb_deep_analyzer_path)) + _deep_analyzer_path_added = True + from deep_analyzer import DeepAnalyzer, SimpleAIClient # 初始化 AI 客户端