ReMe/reme/components/file_graph/base_file_graph.py
jinliyl c85917a812
feat(core): support live component replacement (#505)
* feat(core): support live component replacement

* fix(core): serialize replacement with app lifecycle

* fix(core): preserve state during component replacement

* fix(core): roll back partial component startup

* chore(deps): add 'web' extra to reme-ai and remove reme_studio from core dependencies

- Updated reme-ai dependency to include 'as' and 'web' extras
- Removed reme_studio from core dependency list to avoid duplication or unnecessary install

* fix(core): preserve persisted state on startup failure
2026-08-28 16:04:02 +08:00

69 lines
2.2 KiB
Python

"""Abstract base for file-graph backends."""
from abc import abstractmethod
from ..base_component import BaseComponent
from ...enumeration import ComponentEnum, LinkScopeEnum
from ...schema import FileLink, FileNode
class BaseFileGraph(BaseComponent):
"""Abstract base for file-graph backends.
Link scope (``get_outlinks`` / ``get_inlinks``):
REAL edges touching an indexed node
VIRTUAL edges touching a dangling placeholder
(referenced but never upserted, or already deleted)
ALL both
"""
component_type = ComponentEnum.FILE_GRAPH
# -- Lifecycle ---------------------------------------------------------
async def _start(self) -> None:
await super()._start()
await self.load()
async def _close(self) -> None:
if self.is_started:
await self.dump()
await super()._close()
async def load(self) -> None:
"""Restore persisted state. No-op for backends without local files."""
async def dump(self) -> None:
"""Persist state. No-op for backends without local files."""
# -- Node CRUD ---------------------------------------------------------
@abstractmethod
async def upsert_nodes(self, nodes: list[FileNode]) -> None:
"""Insert or update nodes."""
@abstractmethod
async def delete_nodes(self, paths: list[str]) -> None:
"""Remove nodes by path."""
@abstractmethod
async def get_nodes(self, paths: list[str] | None = None) -> list[FileNode]:
"""Return nodes by paths; ``None`` = all real nodes."""
@abstractmethod
async def rebuild_links(self) -> None:
"""Rebuild all edges from each node's link payload."""
@abstractmethod
async def clear(self):
"""Remove all nodes and edges."""
# -- Link access -------------------------------------------------------
@abstractmethod
async def get_outlinks(self, path: str, scope: LinkScopeEnum = LinkScopeEnum.REAL) -> list[FileLink]:
"""Outgoing links from *path*."""
@abstractmethod
async def get_inlinks(self, path: str, scope: LinkScopeEnum = LinkScopeEnum.REAL) -> list[FileLink]:
"""Inbound links to *path*."""