Coverage for src\genesis\services\rag_service.py: 91%

26 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-26 14:20 +0800

1"""RAG 检索服务(Phase 5)。本阶段以罐头桩先行;真实检索后置。""" 

2from __future__ import annotations 

3 

4from pathlib import Path 

5from typing import Protocol, runtime_checkable 

6 

7 

8@runtime_checkable 

9class RagService(Protocol): 

10 def retrieve_write_rules(self, chapter_id: str) -> list[str]: ... 

11 def retrieve_design_rules(self, chapter_id: str) -> list[str]: ... 

12 

13 

14class CannedRagService: 

15 """从 sample/ 读入记入规则文档(Markdown),整体作为规则文本返回。""" 

16 

17 def __init__(self, samples_dir: str = "sample") -> None: 

18 self._samples_dir = Path(samples_dir) 

19 

20 def _load_rules_text(self) -> list[str]: 

21 texts: list[str] = [] 

22 for name in ("rules_entry_ja.docx", "rules_design_ja.docx"): 

23 p = self._samples_dir / name 

24 if not p.exists(): 

25 continue 

26 try: 

27 from genesis.parsers.rule_doc_parser import RuleDocParser 

28 

29 rule_doc = RuleDocParser().parse(str(p)) 

30 if rule_doc.markdown_content: 30 ↛ 22line 30 didn't jump to line 22 because the condition on line 30 was always true

31 texts.append(rule_doc.markdown_content) 

32 except Exception: 

33 continue 

34 return texts 

35 

36 def retrieve_write_rules(self, chapter_id: str) -> list[str]: 

37 return self._load_rules_text() 

38 

39 def retrieve_design_rules(self, chapter_id: str) -> list[str]: 

40 return self._load_rules_text()