"""Backend-specific ref type mapping for Memory Gateway v2.""" from __future__ import annotations from .schemas_v2 import BackendType, MemoryRefType OPENVIKING_REF_TYPE_MAP = { "session_archive": MemoryRefType.SESSION_ARCHIVE, "context_resource": MemoryRefType.CONTEXT_RESOURCE, "resource": MemoryRefType.CONTEXT_RESOURCE, "session_summary": MemoryRefType.SESSION_ARCHIVE, } EVERMEMOS_REF_TYPE_MAP = { "message_memory": MemoryRefType.MESSAGE_MEMORY, "episodic_memory": MemoryRefType.EPISODIC_MEMORY, "episode": MemoryRefType.EPISODIC_MEMORY, "profile": MemoryRefType.PROFILE, "long_term_memory": MemoryRefType.LONG_TERM_MEMORY, "memory": MemoryRefType.LONG_TERM_MEMORY, "preference": MemoryRefType.PROFILE, } OBSIDIAN_REF_TYPE_MAP = { "review_draft": MemoryRefType.DRAFT_REVIEW, "draft_review": MemoryRefType.DRAFT_REVIEW, } def map_backend_ref_type( backend_type: BackendType, backend_ref_type: str | None, ) -> tuple[MemoryRefType, dict[str, str]]: """Map backend-native ref type to a Gateway MemoryRefType. Unknown values fall back to a backend-appropriate default and preserve the original value in returned metadata for later inspection. """ raw_type = (backend_ref_type or "").strip() normalized = raw_type.lower() if backend_type == BackendType.OPENVIKING: mapped = OPENVIKING_REF_TYPE_MAP.get(normalized, MemoryRefType.SESSION_ARCHIVE) elif backend_type == BackendType.EVERMEMOS: mapped = EVERMEMOS_REF_TYPE_MAP.get(normalized, MemoryRefType.LONG_TERM_MEMORY) elif backend_type == BackendType.OBSIDIAN: mapped = OBSIDIAN_REF_TYPE_MAP.get(normalized, MemoryRefType.DRAFT_REVIEW) else: mapped = MemoryRefType.LONG_TERM_MEMORY metadata: dict[str, str] = {} if raw_type and raw_type not in {mapped.value, normalized}: metadata["original_ref_type"] = raw_type elif raw_type and normalized not in _known_backend_ref_types(backend_type): metadata["original_ref_type"] = raw_type return mapped, metadata def _known_backend_ref_types(backend_type: BackendType) -> set[str]: if backend_type == BackendType.OPENVIKING: return set(OPENVIKING_REF_TYPE_MAP) if backend_type == BackendType.EVERMEMOS: return set(EVERMEMOS_REF_TYPE_MAP) if backend_type == BackendType.OBSIDIAN: return set(OBSIDIAN_REF_TYPE_MAP) return set()