|
| 1 | +import json |
| 2 | +import time |
| 3 | +from pathlib import Path |
| 4 | +from threading import Lock |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +DEFAULT_CACHE_DIR = Path.home() / ".anthropic_bridge" / "cache" |
| 8 | +DEFAULT_TTL_DAYS = 30 |
| 9 | + |
| 10 | + |
| 11 | +class ReasoningCache: |
| 12 | + def __init__(self, cache_dir: Path | None = None, ttl_days: int = DEFAULT_TTL_DAYS): |
| 13 | + self._cache_dir = cache_dir or DEFAULT_CACHE_DIR |
| 14 | + self._cache_file = self._cache_dir / "reasoning_details.json" |
| 15 | + self._ttl_seconds = ttl_days * 24 * 60 * 60 |
| 16 | + self._lock = Lock() |
| 17 | + self._memory_cache: dict[str, dict[str, Any]] = {} |
| 18 | + self._loaded = False |
| 19 | + |
| 20 | + def _ensure_loaded(self) -> None: |
| 21 | + if self._loaded: |
| 22 | + return |
| 23 | + with self._lock: |
| 24 | + if self._loaded: # Double-checked locking for thread safety |
| 25 | + return # type: ignore[unreachable] |
| 26 | + self._cache_dir.mkdir(parents=True, exist_ok=True) |
| 27 | + if self._cache_file.exists(): |
| 28 | + try: |
| 29 | + data = json.loads(self._cache_file.read_text()) |
| 30 | + self._memory_cache = data if isinstance(data, dict) else {} |
| 31 | + except (json.JSONDecodeError, OSError): |
| 32 | + self._memory_cache = {} |
| 33 | + self._loaded = True |
| 34 | + |
| 35 | + def _save(self) -> None: |
| 36 | + try: |
| 37 | + self._cache_file.write_text(json.dumps(self._memory_cache, indent=2)) |
| 38 | + except OSError: |
| 39 | + pass |
| 40 | + |
| 41 | + def _cleanup_expired(self) -> None: |
| 42 | + now = time.time() |
| 43 | + expired = [ |
| 44 | + k |
| 45 | + for k, v in self._memory_cache.items() |
| 46 | + if now - v.get("timestamp", 0) > self._ttl_seconds |
| 47 | + ] |
| 48 | + for k in expired: |
| 49 | + del self._memory_cache[k] |
| 50 | + |
| 51 | + def get(self, tool_call_id: str) -> list[dict[str, Any]] | None: |
| 52 | + self._ensure_loaded() |
| 53 | + entry = self._memory_cache.get(tool_call_id) |
| 54 | + if not entry: |
| 55 | + return None |
| 56 | + if time.time() - entry.get("timestamp", 0) > self._ttl_seconds: |
| 57 | + with self._lock: |
| 58 | + self._memory_cache.pop(tool_call_id, None) |
| 59 | + self._save() |
| 60 | + return None |
| 61 | + return entry.get("data") |
| 62 | + |
| 63 | + def set(self, tool_call_id: str, reasoning_details: list[dict[str, Any]]) -> None: |
| 64 | + self._ensure_loaded() |
| 65 | + with self._lock: |
| 66 | + self._memory_cache[tool_call_id] = { |
| 67 | + "timestamp": time.time(), |
| 68 | + "data": reasoning_details, |
| 69 | + } |
| 70 | + self._cleanup_expired() |
| 71 | + self._save() |
| 72 | + |
| 73 | + def clear(self) -> None: |
| 74 | + with self._lock: |
| 75 | + self._memory_cache = {} |
| 76 | + if self._cache_file.exists(): |
| 77 | + self._cache_file.unlink() |
| 78 | + |
| 79 | + |
| 80 | +# global instance |
| 81 | +_cache: ReasoningCache | None = None |
| 82 | + |
| 83 | + |
| 84 | +def get_reasoning_cache() -> ReasoningCache: |
| 85 | + global _cache |
| 86 | + if _cache is None: |
| 87 | + _cache = ReasoningCache() |
| 88 | + return _cache |
0 commit comments