feat!: migrate to OpenAI native tool calls and fix circular deps (#fuck-gemini)
- Fix circular dependencies in agent/tools - Migrate from custom JSON to OpenAI tool calls format - Add async streaming (step_stream, complete_stream) - Simplify prompt system and remove token counting - Add 5 new API endpoints (/health, /v1/models, /api/memory/*) - Add 3 new tools (get_torrent_by_index, add_torrent_by_index, set_language) - Fix all 500 tests and add coverage config (80% threshold) - Add comprehensive docs (README, pytest guide) BREAKING: LLM interface changed, memory injection via get_memory()
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
"""JSON-based TV show repository implementation."""
|
||||
from typing import List, Optional, Dict, Any
|
||||
import logging
|
||||
|
||||
from domain.tv_shows.repositories import TVShowRepository
|
||||
from domain.tv_shows.entities import TVShow
|
||||
from domain.tv_shows.value_objects import ShowStatus
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from domain.shared.value_objects import ImdbId
|
||||
from ..memory import Memory
|
||||
from domain.tv_shows.entities import TVShow
|
||||
from domain.tv_shows.repositories import TVShowRepository
|
||||
from domain.tv_shows.value_objects import ShowStatus
|
||||
from infrastructure.persistence import get_memory
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -14,99 +16,121 @@ logger = logging.getLogger(__name__)
|
||||
class JsonTVShowRepository(TVShowRepository):
|
||||
"""
|
||||
JSON-based implementation of TVShowRepository.
|
||||
|
||||
Stores TV shows in the memory.json file (compatible with existing tv_shows structure).
|
||||
|
||||
Stores TV shows in the LTM library using the memory context.
|
||||
"""
|
||||
|
||||
def __init__(self, memory: Memory):
|
||||
"""
|
||||
Initialize repository.
|
||||
|
||||
Args:
|
||||
memory: Memory instance for persistence
|
||||
"""
|
||||
self.memory = memory
|
||||
|
||||
|
||||
def save(self, show: TVShow) -> None:
|
||||
"""Save a TV show to the repository."""
|
||||
shows = self._load_all()
|
||||
|
||||
"""
|
||||
Save a TV show to the repository.
|
||||
|
||||
Updates existing show if IMDb ID matches.
|
||||
|
||||
Args:
|
||||
show: TVShow entity to save.
|
||||
"""
|
||||
memory = get_memory()
|
||||
shows = memory.ltm.library.get("tv_shows", [])
|
||||
|
||||
# Remove existing show with same IMDb ID
|
||||
shows = [s for s in shows if s.get('imdb_id') != str(show.imdb_id)]
|
||||
|
||||
# Add new show
|
||||
shows = [s for s in shows if s.get("imdb_id") != str(show.imdb_id)]
|
||||
|
||||
shows.append(self._to_dict(show))
|
||||
|
||||
# Save to memory
|
||||
self.memory.set('tv_shows', shows)
|
||||
|
||||
memory.ltm.library["tv_shows"] = shows
|
||||
memory.save()
|
||||
logger.debug(f"Saved TV show: {show.imdb_id}")
|
||||
|
||||
def find_by_imdb_id(self, imdb_id: ImdbId) -> Optional[TVShow]:
|
||||
"""Find a TV show by its IMDb ID."""
|
||||
shows = self._load_all()
|
||||
|
||||
|
||||
def find_by_imdb_id(self, imdb_id: ImdbId) -> TVShow | None:
|
||||
"""
|
||||
Find a TV show by its IMDb ID.
|
||||
|
||||
Args:
|
||||
imdb_id: IMDb ID to search for.
|
||||
|
||||
Returns:
|
||||
TVShow if found, None otherwise.
|
||||
"""
|
||||
memory = get_memory()
|
||||
shows = memory.ltm.library.get("tv_shows", [])
|
||||
|
||||
for show_dict in shows:
|
||||
if show_dict.get('imdb_id') == str(imdb_id):
|
||||
if show_dict.get("imdb_id") == str(imdb_id):
|
||||
return self._from_dict(show_dict)
|
||||
|
||||
|
||||
return None
|
||||
|
||||
def find_all(self) -> List[TVShow]:
|
||||
"""Get all TV shows in the repository."""
|
||||
shows_dict = self._load_all()
|
||||
|
||||
def find_all(self) -> list[TVShow]:
|
||||
"""
|
||||
Get all TV shows in the repository.
|
||||
|
||||
Returns:
|
||||
List of all TVShow entities.
|
||||
"""
|
||||
memory = get_memory()
|
||||
shows_dict = memory.ltm.library.get("tv_shows", [])
|
||||
return [self._from_dict(s) for s in shows_dict]
|
||||
|
||||
|
||||
def delete(self, imdb_id: ImdbId) -> bool:
|
||||
"""Delete a TV show from the repository."""
|
||||
shows = self._load_all()
|
||||
"""
|
||||
Delete a TV show from the repository.
|
||||
|
||||
Args:
|
||||
imdb_id: IMDb ID of show to delete.
|
||||
|
||||
Returns:
|
||||
True if deleted, False if not found.
|
||||
"""
|
||||
memory = get_memory()
|
||||
shows = memory.ltm.library.get("tv_shows", [])
|
||||
initial_count = len(shows)
|
||||
|
||||
# Filter out the show
|
||||
shows = [s for s in shows if s.get('imdb_id') != str(imdb_id)]
|
||||
|
||||
|
||||
shows = [s for s in shows if s.get("imdb_id") != str(imdb_id)]
|
||||
|
||||
if len(shows) < initial_count:
|
||||
self.memory.set('tv_shows', shows)
|
||||
memory.ltm.library["tv_shows"] = shows
|
||||
memory.save()
|
||||
logger.debug(f"Deleted TV show: {imdb_id}")
|
||||
return True
|
||||
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def exists(self, imdb_id: ImdbId) -> bool:
|
||||
"""Check if a TV show exists in the repository."""
|
||||
"""
|
||||
Check if a TV show exists in the repository.
|
||||
|
||||
Args:
|
||||
imdb_id: IMDb ID to check.
|
||||
|
||||
Returns:
|
||||
True if exists, False otherwise.
|
||||
"""
|
||||
return self.find_by_imdb_id(imdb_id) is not None
|
||||
|
||||
def _load_all(self) -> List[Dict[str, Any]]:
|
||||
"""Load all TV shows from memory."""
|
||||
return self.memory.get('tv_shows', [])
|
||||
|
||||
def _to_dict(self, show: TVShow) -> Dict[str, Any]:
|
||||
|
||||
def _to_dict(self, show: TVShow) -> dict[str, Any]:
|
||||
"""Convert TVShow entity to dict for storage."""
|
||||
return {
|
||||
'imdb_id': str(show.imdb_id),
|
||||
'title': show.title,
|
||||
'seasons_count': show.seasons_count,
|
||||
'status': show.status.value,
|
||||
'tmdb_id': show.tmdb_id,
|
||||
'overview': show.overview,
|
||||
'poster_path': show.poster_path,
|
||||
'first_air_date': show.first_air_date,
|
||||
'vote_average': show.vote_average,
|
||||
'added_at': show.added_at.isoformat(),
|
||||
"imdb_id": str(show.imdb_id),
|
||||
"title": show.title,
|
||||
"seasons_count": show.seasons_count,
|
||||
"status": show.status.value,
|
||||
"tmdb_id": show.tmdb_id,
|
||||
"first_air_date": show.first_air_date,
|
||||
"added_at": show.added_at.isoformat(),
|
||||
}
|
||||
|
||||
def _from_dict(self, data: Dict[str, Any]) -> TVShow:
|
||||
|
||||
def _from_dict(self, data: dict[str, Any]) -> TVShow:
|
||||
"""Convert dict from storage to TVShow entity."""
|
||||
from datetime import datetime
|
||||
|
||||
return TVShow(
|
||||
imdb_id=ImdbId(data['imdb_id']),
|
||||
title=data['title'],
|
||||
seasons_count=data['seasons_count'],
|
||||
status=ShowStatus.from_string(data['status']),
|
||||
tmdb_id=data.get('tmdb_id'),
|
||||
overview=data.get('overview'),
|
||||
poster_path=data.get('poster_path'),
|
||||
first_air_date=data.get('first_air_date'),
|
||||
vote_average=data.get('vote_average'),
|
||||
added_at=datetime.fromisoformat(data['added_at']) if data.get('added_at') else datetime.now(),
|
||||
imdb_id=ImdbId(data["imdb_id"]),
|
||||
title=data["title"],
|
||||
seasons_count=data["seasons_count"],
|
||||
status=ShowStatus.from_string(data["status"]),
|
||||
tmdb_id=data.get("tmdb_id"),
|
||||
first_air_date=data.get("first_air_date"),
|
||||
added_at=(
|
||||
datetime.fromisoformat(data["added_at"])
|
||||
if data.get("added_at")
|
||||
else datetime.now()
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user