kbzhishiku

This commit is contained in:
runny committed 2026-05-12 08:16:42 +08:00
1 parent 7f542f54a5
commit 08a19ebe96
68 files changed
+16602 -229

No files matched your search

@@ -0,0 +1,88 @@
"""
backend/app/services/knowledge_cleaning/edkb_adapter.py
文件用途:edkb_system 轻量适配层
"""
from __future__ import annotations
import shutil
import tempfile
from pathlib import Path
TEXT_EXTS = {'.txt', '.md'}
def _find_generated_markdown(output_root: Path) -> Path:
# Look for md files directly inside output_root or inside any of its subdirectories
# Note that edkb_system puts output inside a directory matching the parent dir name, which might be tmp_dir
candidates = [path for path in output_root.rglob('*.md') if not path.name.startswith('_')]
if not candidates:
raise RuntimeError('No markdown output produced by edkb_system.')
candidates.sort(key=lambda item: len(item.parts))
return candidates[0]
def _write_text_result(source_path: Path, target_dir: Path) -> dict:
target_dir.mkdir(parents=True, exist_ok=True)
cleaned_path = target_dir / 'document.md'
content = source_path.read_text(encoding='utf-8', errors='ignore')
cleaned_path.write_text(content, encoding='utf-8')
return {
'mode': 'plain_text',
'cleaned_file_path': str(cleaned_path),
'cleaned_preview_path': str(cleaned_path),
'word_count': len(content.split()),
}
def build_clean_result(source_path: Path, target_dir: Path) -> dict:
"""
优先产出真实文本结果:
- txt/md:直接抽取文本;
- 其它格式:尝试走 edkb_system;
- 失败时由调用方决定是否回退。
"""
suffix = source_path.suffix.lower()
if suffix in TEXT_EXTS:
return _write_text_result(source_path, target_dir)
from edkb_system.config import SystemConfig
from edkb_system.controller import build_knowledge
target_dir.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(prefix='edkb_', dir=str(target_dir.parent)) as tmp_dir:
tmp_root = Path(tmp_dir)
output_dir = tmp_root / 'output'
cache_dir = tmp_root / 'cache'
output_dir.mkdir(parents=True, exist_ok=True)
cache_dir.mkdir(parents=True, exist_ok=True)
# IMPORTANT: copy the source file into tmp_root so that its parent directory is predictable
# edkb_system seems to output to output_dir / <parent_dir_name> / <filename>.md
# ALSO IMPORTANT: ensure the file has its original suffix, otherwise edkb_system might fail or output nothing
original_name = getattr(source_path, "original_filename", None)
if not original_name:
original_name = source_path.name
if "." not in original_name and hasattr(source_path, "suffix_hint"):
original_name += source_path.suffix_hint
tmp_source_path = tmp_root / original_name
shutil.copy2(source_path, tmp_source_path)
config = SystemConfig.load(None)
result = build_knowledge(tmp_source_path, output_dir, config, cache_dir)
failures = [item for item in result if item.get('status') == 'failed']
if failures:
raise RuntimeError(failures[0].get('error') or 'edkb_system failed.')
markdown_source = _find_generated_markdown(output_dir)
cleaned_path = target_dir / 'document.md'
shutil.copy2(markdown_source, cleaned_path)
content = cleaned_path.read_text(encoding='utf-8', errors='ignore')
return {
'mode': 'edkb_system',
'cleaned_file_path': str(cleaned_path),
'cleaned_preview_path': str(cleaned_path),
'word_count': len(content.split()),
}