2026-10-24 13:41:54 +08:00
|
|
|
"""异步业务字段存储接口;字段值保留 bool/int/str 的区别。"""
|
2026-10-22 20:50:10 +08:00
|
|
|
|
|
|
|
|
from typing import Protocol
|
|
|
|
|
|
|
|
|
|
|
2026-10-23 19:48:29 +08:00
|
|
|
class CheckpointNotFoundError(Exception):
|
2026-10-24 13:41:54 +08:00
|
|
|
"""存储适配器明确确认线程不存在。"""
|
2026-10-23 19:48:29 +08:00
|
|
|
|
|
|
|
|
|
2026-10-22 20:50:10 +08:00
|
|
|
class SessionRepository(Protocol):
|
2026-10-24 13:41:54 +08:00
|
|
|
async def load(self, session_id: str) -> dict:
|
|
|
|
|
"""读取业务字段;不存在返回空 dict。"""
|
2026-10-22 20:50:10 +08:00
|
|
|
...
|
|
|
|
|
|
2026-10-24 13:41:54 +08:00
|
|
|
async def apply_update(self, session_id: str, patch: dict) -> dict:
|
|
|
|
|
"""提交差量后返回完整业务字段。"""
|
2026-10-22 20:50:10 +08:00
|
|
|
...
|
|
|
|
|
|
2026-10-24 13:41:54 +08:00
|
|
|
async def delete(self, session_id: str) -> None:
|
|
|
|
|
"""幂等删除;真实存储错误必须传播。"""
|
2026-10-22 20:50:10 +08:00
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InMemorySessionRepository:
|
2026-10-24 13:41:54 +08:00
|
|
|
def __init__(self):
|
2026-10-22 20:50:10 +08:00
|
|
|
self._store: dict[str, dict] = {}
|
|
|
|
|
|
2026-10-24 13:41:54 +08:00
|
|
|
async def load(self, session_id: str) -> dict:
|
2026-10-22 20:50:10 +08:00
|
|
|
return dict(self._store.get(session_id) or {})
|
|
|
|
|
|
2026-10-24 13:41:54 +08:00
|
|
|
async def apply_update(self, session_id: str, patch: dict) -> dict:
|
2026-10-22 20:50:10 +08:00
|
|
|
state = self._store.setdefault(session_id, {})
|
|
|
|
|
state.update(patch)
|
|
|
|
|
return dict(state)
|
|
|
|
|
|
2026-10-24 13:41:54 +08:00
|
|
|
async def delete(self, session_id: str) -> None:
|
2026-10-22 20:50:10 +08:00
|
|
|
self._store.pop(session_id, None)
|
|
|
|
|
|
|
|
|
|
|
2026-10-23 19:48:29 +08:00
|
|
|
__all__ = ["CheckpointNotFoundError", "SessionRepository", "InMemorySessionRepository"]
|