chore: initialize project repository
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Infrastructure adapters."""
|
||||
@@ -0,0 +1,6 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
|
||||
from app.core.config import get_settings
|
||||
|
||||
engine = create_async_engine(get_settings().mysql_dsn, pool_pre_ping=True)
|
||||
SessionFactory = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
||||
@@ -0,0 +1,33 @@
|
||||
from typing import Any, Protocol
|
||||
|
||||
|
||||
class CacheClient(Protocol):
|
||||
async def get(self, key: str) -> Any: ...
|
||||
|
||||
async def set(self, key: str, value: str, ex: int | None = None) -> Any: ...
|
||||
|
||||
|
||||
class CacheReadResult:
|
||||
def __init__(self, value: Any, degraded: bool = False) -> None:
|
||||
self.value = value
|
||||
self.degraded = degraded
|
||||
|
||||
|
||||
class MemoryCacheAdapter:
|
||||
"""Cache is an optimization; failures must not block MySQL recall."""
|
||||
|
||||
def __init__(self, client: CacheClient) -> None:
|
||||
self.client = client
|
||||
|
||||
async def get(self, key: str) -> CacheReadResult:
|
||||
try:
|
||||
return CacheReadResult(await self.client.get(key))
|
||||
except Exception:
|
||||
return CacheReadResult(None, degraded=True)
|
||||
|
||||
async def set(self, key: str, value: str, ttl_seconds: int = 300) -> bool:
|
||||
try:
|
||||
await self.client.set(key, value, ex=ttl_seconds)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
@@ -0,0 +1,28 @@
|
||||
from typing import Any, Protocol
|
||||
|
||||
|
||||
class VectorClient(Protocol):
|
||||
def search(self, collection_name: str, data: list[list[float]], limit: int) -> Any: ...
|
||||
|
||||
|
||||
class VectorSearchResult:
|
||||
def __init__(self, hits: Any, degraded: bool = False) -> None:
|
||||
self.hits = hits
|
||||
self.degraded = degraded
|
||||
|
||||
|
||||
class VectorMemoryAdapter:
|
||||
def __init__(self, client: VectorClient, collection: str) -> None:
|
||||
self.client = client
|
||||
self.collection = collection
|
||||
|
||||
def search(self, embedding: list[float], limit: int = 10) -> VectorSearchResult:
|
||||
try:
|
||||
hits = self.client.search(
|
||||
collection_name=self.collection,
|
||||
data=[embedding],
|
||||
limit=max(1, min(limit, 100)),
|
||||
)
|
||||
return VectorSearchResult(hits)
|
||||
except Exception:
|
||||
return VectorSearchResult([], degraded=True)
|
||||
Reference in New Issue
Block a user