84 行
无行尾
3.1 KiB
Python
84 行
无行尾
3.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Offline-first World gateway contract.
|
|
|
|
The production gateway is intentionally not implemented in this phase. The
|
|
null gateway preserves local behavior and makes the future boundary testable.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from datetime import datetime
|
|
from typing import Any
|
|
|
|
|
|
@dataclass
|
|
class WorldResult:
|
|
ok: bool
|
|
data: Any = None
|
|
reason: str = ""
|
|
fresh_until: str = ""
|
|
request_id: str = ""
|
|
|
|
|
|
class NullWorldGateway:
|
|
enabled = False
|
|
|
|
def publish_activity(self, activity: dict[str, Any]) -> WorldResult:
|
|
return WorldResult(False, reason="world_gateway_disabled")
|
|
|
|
def get_world_context(self, subject_id: str, now: datetime) -> WorldResult:
|
|
return WorldResult(False, data=[], reason="world_gateway_disabled")
|
|
|
|
def get_schedule_projection(self, subject_id: str, date: str) -> WorldResult:
|
|
return WorldResult(False, data=[], reason="world_gateway_disabled")
|
|
|
|
def confirm_shared_activity(self, candidate: dict[str, Any]) -> WorldResult:
|
|
return WorldResult(False, reason="world_gateway_disabled")
|
|
|
|
def revoke_projection(self, ref: str) -> WorldResult:
|
|
return WorldResult(False, reason="world_gateway_disabled")
|
|
|
|
|
|
@dataclass
|
|
class WorldGatewayContract:
|
|
gateway: Any = field(default_factory=NullWorldGateway)
|
|
timeout_seconds: float = 2.0
|
|
failure_cooldown_seconds: float = 60.0
|
|
|
|
def _call(self, method: str, *args: Any, **kwargs: Any) -> WorldResult:
|
|
if self.gateway is None:
|
|
return WorldResult(False, reason="world_gateway_missing")
|
|
try:
|
|
result = getattr(self.gateway, method)(*args, **kwargs)
|
|
return result if isinstance(result, WorldResult) else WorldResult(True, data=result)
|
|
except Exception as exc:
|
|
return WorldResult(False, reason=f"world_gateway_error:{type(exc).__name__}")
|
|
|
|
def publish_activity(self, activity: dict[str, Any]) -> WorldResult:
|
|
return self._call("publish_activity", activity)
|
|
|
|
def get_world_context(self, subject_id: str, now: datetime) -> WorldResult:
|
|
return self._call("get_world_context", subject_id, now)
|
|
|
|
def get_schedule_projection(self, subject_id: str, date: str) -> WorldResult:
|
|
return self._call("get_schedule_projection", subject_id, date)
|
|
|
|
def confirm_shared_activity(self, candidate: dict[str, Any]) -> WorldResult:
|
|
return self._call("confirm_shared_activity", candidate)
|
|
|
|
def revoke_projection(self, ref: str) -> WorldResult:
|
|
return self._call("revoke_projection", ref)
|
|
def projection_is_fresh(projection: dict[str, Any], now: datetime) -> bool:
|
|
if not isinstance(projection, dict):
|
|
return False
|
|
fresh_until = str(projection.get("fresh_until") or "").strip()
|
|
if not fresh_until:
|
|
return False
|
|
try:
|
|
current = now if now.tzinfo is not None else now.astimezone()
|
|
expiry = datetime.fromisoformat(fresh_until.replace("Z", "+00:00"))
|
|
if expiry.tzinfo is None:
|
|
expiry = expiry.replace(tzinfo=current.tzinfo)
|
|
return current <= expiry
|
|
except (TypeError, ValueError):
|
|
return False |