ReMe/reme2/component/file_parser/default_file_parser.py
jinliyl 52f1a33b3a
Some checks are pending
Pre-commit / run (ubuntu-latest) (push) Waiting to run
feat: add file parser support and search filtering (#214)
- Add file_parser component with default implementation
- Introduce SearchFilter schema for path and tag filtering
- Implement filter functionality in BaseFileStore and LocalFileStore
- Update file watcher to use parser-based filtering instead of suffix filters
- Register new FILE_PARSER component enum
- Add test_data directory to gitignore

refactor: improve component imports and initialization

- Fix relative imports in application.py
- Add file_parser import to component init
- Initialize registry dict when component type doesn't exist
- Remove circular import in HttpService by using string annotation
- Update config yaml to use proper component names

refactor: enhance file watcher architecture

- Replace MdFileWatcher with more flexible FullFileWatcher and LightFileWatcher
- Remove suffix-based filtering in favor of parser-based approach
- Update BaseFileWatcher to resolve parsers from app context
- Remove unused watch_filter method

refactor: update ReMe core functionality

- Remove memory_path creation
- Simplify dream and proactive methods to return empty strings
- Update config defaults for HTTP service and component backends

docs: update component configuration in paw.yaml

- Change service backend from cmd to http
- Rename components to use correct singular forms
- Add default file parser and file watcher configurations
- Set up local file store with default settings
```

Co-authored-by: huangsen <huangsen.huang@alibaba-inc.com>
2026-04-21 16:44:46 +08:00

64 lines
1.8 KiB
Python

"""Default file parser for unknown file types."""
import asyncio
import hashlib
from pathlib import Path
from .base_file_parser import BaseFileParser
from ..component_registry import R
from ...schema import FileChunk, FileMetadata
from ...utils import hash_text, chunk_markdown
@R.register("default")
class DefaultFileParser(BaseFileParser):
"""Fallback parser for unknown file types.
Attempts to read as text and chunk. If the file is binary,
stores metadata only with no chunks.
"""
suffixes = []
def __init__(self, encoding: str = "utf-8", **kwargs):
super().__init__(**kwargs)
self.encoding = encoding
async def parse(self, path: str) -> tuple[FileMetadata, list[FileChunk]]:
file_path = Path(path)
def _read_file():
stat = file_path.stat()
raw = file_path.read_bytes()
try:
content = raw.decode(self.encoding)
content_hash = hash_text(content)
return stat, content_hash, content
except (UnicodeDecodeError, ValueError):
binary_hash = hashlib.sha256(raw).hexdigest()
return stat, binary_hash, None
stat, file_hash, content = await asyncio.to_thread(_read_file)
file_meta = FileMetadata(
hash=file_hash,
mtime_ms=stat.st_mtime * 1000,
size=stat.st_size,
path=str(file_path.absolute()),
content=content,
)
chunks: list[FileChunk] = []
if content:
chunks = (
chunk_markdown(
content,
file_meta.path,
self.chunk_tokens,
self.chunk_overlap,
)
or []
)
file_meta.chunk_count = len(chunks)
return file_meta, chunks