New archi: domain driven development

Working but need to check out code
This commit is contained in:
2025-12-01 07:10:03 +01:00
parent 2b815502f6
commit 2c8cdd3ab1
73 changed files with 4084 additions and 853 deletions
+1
View File
@@ -0,0 +1 @@
"""Infrastructure layer - External services, persistence, and technical concerns."""
+1
View File
@@ -0,0 +1 @@
"""API clients for external services."""
+22
View File
@@ -0,0 +1,22 @@
"""Knaben API client."""
from .client import KnabenClient
from .dto import TorrentResult
from .exceptions import (
KnabenError,
KnabenConfigurationError,
KnabenAPIError,
KnabenNotFoundError,
)
# Global Knaben client instance (singleton)
knaben_client = KnabenClient()
__all__ = [
"KnabenClient",
"TorrentResult",
"KnabenError",
"KnabenConfigurationError",
"KnabenAPIError",
"KnabenNotFoundError",
"knaben_client",
]
+191
View File
@@ -0,0 +1,191 @@
"""Knaben torrent search API client."""
from typing import Dict, Any, Optional, List
import logging
import requests
from requests.exceptions import RequestException, Timeout, HTTPError
from agent.config import Settings, settings
from .dto import TorrentResult
from .exceptions import KnabenError, KnabenAPIError, KnabenNotFoundError
logger = logging.getLogger(__name__)
class KnabenClient:
"""
Client for interacting with Knaben torrent search API.
Knaben is a torrent search engine that aggregates results from multiple trackers.
Example:
>>> client = KnabenClient()
>>> results = client.search("Inception 1080p")
>>> for torrent in results[:5]:
... print(f"{torrent.name} - Seeders: {torrent.seeders}")
"""
def __init__(
self,
base_url: Optional[str] = None,
timeout: Optional[int] = None,
config: Optional[Settings] = None
):
"""
Initialize Knaben client.
Args:
base_url: Knaben API base URL (defaults to https://api.knaben.org/v1)
timeout: Request timeout in seconds (defaults to settings)
config: Optional Settings instance (for testing)
Note:
Knaben API doesn't require an API key
"""
cfg = config or settings
self.base_url = base_url or "https://api.knaben.org/v1"
self.timeout = timeout or cfg.request_timeout
logger.info("Knaben client initialized")
def _make_request(
self,
params: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
"""
Make a request to Knaben API.
Args:
params: Query parameters
Returns:
JSON response as dict
Raises:
KnabenAPIError: If request fails
"""
try:
logger.debug(f"Knaben request with params: {params}")
response = requests.post(self.base_url, json=params, timeout=self.timeout)
response.raise_for_status()
return response.json()
except Timeout as e:
logger.error(f"Knaben API timeout: {e}")
raise KnabenAPIError(f"Request timeout after {self.timeout} seconds") from e
except HTTPError as e:
logger.error(f"Knaben API HTTP error: {e}")
if e.response is not None:
status_code = e.response.status_code
if status_code == 404:
raise KnabenNotFoundError("Resource not found") from e
elif status_code == 429:
raise KnabenAPIError("Rate limit exceeded") from e
else:
raise KnabenAPIError(f"HTTP {status_code}: {e}") from e
raise KnabenAPIError(f"HTTP error: {e}") from e
except RequestException as e:
logger.error(f"Knaben API request failed: {e}")
raise KnabenAPIError(f"Failed to connect to Knaben API: {e}") from e
def search(
self,
query: str,
limit: int = 10
) -> List[TorrentResult]:
"""
Search for torrents.
Args:
query: Search query (e.g., "Inception 1080p")
limit: Maximum number of results (default: 10)
Returns:
List of TorrentResult objects
Raises:
KnabenAPIError: If request fails
ValueError: If query is invalid
"""
if not query or not isinstance(query, str):
raise ValueError("Query must be a non-empty string")
if len(query) > 500:
raise ValueError("Query is too long (max 500 characters)")
# Build params
params = {
"query": query,
"search_field": "title",
"order_by": "peers",
"order_direction": "desc",
"from": 0,
"size": limit,
"hide_unsafe": True,
"hide_xxx": True,
}
try:
data = self._make_request(params)
except KnabenNotFoundError:
logger.info(f"No torrents found for '{query}'")
return []
except Exception as e:
logger.error(f"Unexpected error in search: {e}", exc_info=True)
raise
# Parse results
results = []
torrents = data.get('hits', [])
if not torrents:
logger.info(f"No torrents found for '{query}'")
return []
for torrent in torrents:
try:
result = self._parse_torrent(torrent)
results.append(result)
except Exception as e:
logger.warning(f"Failed to parse torrent: {e}")
continue
logger.info(f"Found {len(results)} torrents for '{query}'")
return results
def _parse_torrent(self, torrent: Dict[str, Any]) -> TorrentResult:
"""
Parse a torrent result into a TorrentResult object.
Args:
torrent: Raw torrent dict from API
Returns:
TorrentResult object
"""
# Extract required fields (API uses camelCase)
title = torrent.get('title', 'Unknown')
size = torrent.get('size', 'Unknown')
seeders = int(torrent.get('seeders', 0) or 0)
leechers = int(torrent.get('leechers', 0) or 0)
magnet = torrent.get('magnetUrl', '')
# Extract optional fields
info_hash = torrent.get('hash')
tracker = torrent.get('tracker')
upload_date = torrent.get('date')
category = torrent.get('category')
return TorrentResult(
title=title,
size=size,
seeders=seeders,
leechers=leechers,
magnet=magnet,
info_hash=info_hash,
tracker=tracker,
upload_date=upload_date,
category=category
)
+17
View File
@@ -0,0 +1,17 @@
"""Knaben Data Transfer Objects."""
from dataclasses import dataclass
from typing import Optional
@dataclass
class TorrentResult:
"""Represents a torrent search result from Knaben."""
title: str
size: str
seeders: int
leechers: int
magnet: str
info_hash: Optional[str] = None
tracker: Optional[str] = None
upload_date: Optional[str] = None
category: Optional[str] = None
+21
View File
@@ -0,0 +1,21 @@
"""Knaben API exceptions."""
class KnabenError(Exception):
"""Base exception for Knaben-related errors."""
pass
class KnabenConfigurationError(KnabenError):
"""Raised when Knaben API is not properly configured."""
pass
class KnabenAPIError(KnabenError):
"""Raised when Knaben API returns an error."""
pass
class KnabenNotFoundError(KnabenError):
"""Raised when no torrents are found."""
pass
@@ -0,0 +1,22 @@
"""qBittorrent API client."""
from .client import QBittorrentClient
from .dto import TorrentInfo
from .exceptions import (
QBittorrentError,
QBittorrentConfigurationError,
QBittorrentAPIError,
QBittorrentAuthError,
)
# Global qBittorrent client instance (singleton)
qbittorrent_client = QBittorrentClient()
__all__ = [
"QBittorrentClient",
"TorrentInfo",
"QBittorrentError",
"QBittorrentConfigurationError",
"QBittorrentAPIError",
"QBittorrentAuthError",
"qbittorrent_client",
]
+388
View File
@@ -0,0 +1,388 @@
"""qBittorrent Web API client."""
from typing import Dict, Any, Optional, List
import logging
import requests
from requests.exceptions import RequestException, Timeout, HTTPError
from agent.config import Settings, settings
from .dto import TorrentInfo
from .exceptions import QBittorrentError, QBittorrentAPIError, QBittorrentAuthError
logger = logging.getLogger(__name__)
class QBittorrentClient:
"""
Client for interacting with qBittorrent Web API.
This client provides methods to manage torrents in qBittorrent.
Example:
>>> client = QBittorrentClient()
>>> client.login()
>>> torrents = client.get_torrents()
>>> for torrent in torrents:
... print(f"{torrent.name} - {torrent.progress}%")
"""
def __init__(
self,
host: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None,
timeout: Optional[int] = None,
config: Optional[Settings] = None
):
"""
Initialize qBittorrent client.
Args:
host: qBittorrent host URL (e.g., "http://192.168.1.100:8080")
username: qBittorrent username
password: qBittorrent password
timeout: Request timeout in seconds (defaults to settings)
config: Optional Settings instance (for testing)
"""
cfg = config or settings
self.host = host or "http://192.168.178.47:30024"
self.username = username or "admin"
self.password = password or "adminadmin"
self.timeout = timeout or cfg.request_timeout
self.session = requests.Session()
self._authenticated = False
logger.info(f"qBittorrent client initialized for {self.host}")
def _make_request(
self,
method: str,
endpoint: str,
data: Optional[Dict[str, Any]] = None,
files: Optional[Dict[str, Any]] = None
) -> Any:
"""
Make a request to qBittorrent API.
Args:
method: HTTP method (GET, POST)
endpoint: API endpoint (e.g., '/api/v2/torrents/info')
data: Request data
files: Files to upload
Returns:
Response (JSON or text)
Raises:
QBittorrentAPIError: If request fails
"""
url = f"{self.host}{endpoint}"
try:
logger.debug(f"qBittorrent {method} request: {endpoint}")
if method.upper() == "GET":
response = self.session.get(url, params=data, timeout=self.timeout)
elif method.upper() == "POST":
response = self.session.post(url, data=data, files=files, timeout=self.timeout)
else:
raise ValueError(f"Unsupported HTTP method: {method}")
response.raise_for_status()
# Try to parse as JSON, otherwise return text
try:
return response.json()
except ValueError:
return response.text
except Timeout as e:
logger.error(f"qBittorrent API timeout: {e}")
raise QBittorrentAPIError(f"Request timeout after {self.timeout} seconds") from e
except HTTPError as e:
logger.error(f"qBittorrent API HTTP error: {e}")
if e.response is not None:
status_code = e.response.status_code
if status_code == 403:
raise QBittorrentAuthError("Authentication required or forbidden") from e
else:
raise QBittorrentAPIError(f"HTTP {status_code}: {e}") from e
raise QBittorrentAPIError(f"HTTP error: {e}") from e
except RequestException as e:
logger.error(f"qBittorrent API request failed: {e}")
raise QBittorrentAPIError(f"Failed to connect to qBittorrent: {e}") from e
def login(self) -> bool:
"""
Authenticate with qBittorrent.
Returns:
True if authentication successful
Raises:
QBittorrentAuthError: If authentication fails
"""
try:
data = {
"username": self.username,
"password": self.password
}
response = self._make_request("POST", "/api/v2/auth/login", data=data)
if response == "Ok.":
self._authenticated = True
logger.info("Successfully authenticated with qBittorrent")
return True
else:
raise QBittorrentAuthError("Authentication failed")
except QBittorrentAPIError as e:
logger.error(f"Login failed: {e}")
raise QBittorrentAuthError("Failed to authenticate") from e
def logout(self) -> bool:
"""
Logout from qBittorrent.
Returns:
True if logout successful
"""
try:
self._make_request("POST", "/api/v2/auth/logout")
self._authenticated = False
logger.info("Logged out from qBittorrent")
return True
except Exception as e:
logger.warning(f"Logout failed: {e}")
return False
def get_torrents(
self,
filter: Optional[str] = None,
category: Optional[str] = None
) -> List[TorrentInfo]:
"""
Get list of torrents.
Args:
filter: Filter torrents (all, downloading, completed, paused, active, inactive)
category: Filter by category
Returns:
List of TorrentInfo objects
Raises:
QBittorrentAPIError: If request fails
"""
if not self._authenticated:
self.login()
params = {}
if filter:
params["filter"] = filter
if category:
params["category"] = category
try:
data = self._make_request("GET", "/api/v2/torrents/info", data=params)
if not isinstance(data, list):
logger.warning("Unexpected response format")
return []
torrents = []
for torrent in data:
try:
torrents.append(self._parse_torrent(torrent))
except Exception as e:
logger.warning(f"Failed to parse torrent: {e}")
continue
logger.info(f"Retrieved {len(torrents)} torrents")
return torrents
except QBittorrentAPIError as e:
logger.error(f"Failed to get torrents: {e}")
raise
def add_torrent(
self,
magnet: str,
category: Optional[str] = None,
save_path: Optional[str] = None,
paused: bool = False
) -> bool:
"""
Add a torrent via magnet link.
Args:
magnet: Magnet link
category: Category to assign
save_path: Download path
paused: Start torrent paused
Returns:
True if torrent added successfully
Raises:
QBittorrentAPIError: If request fails
"""
if not self._authenticated:
self.login()
data = {
"urls": magnet,
"paused": "true" if paused else "false"
}
if category:
data["category"] = category
if save_path:
data["savepath"] = save_path
try:
response = self._make_request("POST", "/api/v2/torrents/add", data=data)
if response == "Ok.":
logger.info(f"Successfully added torrent")
return True
else:
logger.warning(f"Unexpected response: {response}")
return False
except QBittorrentAPIError as e:
logger.error(f"Failed to add torrent: {e}")
raise
def delete_torrent(
self,
torrent_hash: str,
delete_files: bool = False
) -> bool:
"""
Delete a torrent.
Args:
torrent_hash: Hash of the torrent
delete_files: Also delete downloaded files
Returns:
True if torrent deleted successfully
Raises:
QBittorrentAPIError: If request fails
"""
if not self._authenticated:
self.login()
data = {
"hashes": torrent_hash,
"deleteFiles": "true" if delete_files else "false"
}
try:
response = self._make_request("POST", "/api/v2/torrents/delete", data=data)
logger.info(f"Deleted torrent {torrent_hash}")
return True
except QBittorrentAPIError as e:
logger.error(f"Failed to delete torrent: {e}")
raise
def pause_torrent(self, torrent_hash: str) -> bool:
"""
Pause a torrent.
Args:
torrent_hash: Hash of the torrent
Returns:
True if torrent paused successfully
"""
if not self._authenticated:
self.login()
data = {"hashes": torrent_hash}
try:
self._make_request("POST", "/api/v2/torrents/pause", data=data)
logger.info(f"Paused torrent {torrent_hash}")
return True
except QBittorrentAPIError as e:
logger.error(f"Failed to pause torrent: {e}")
raise
def resume_torrent(self, torrent_hash: str) -> bool:
"""
Resume a torrent.
Args:
torrent_hash: Hash of the torrent
Returns:
True if torrent resumed successfully
"""
if not self._authenticated:
self.login()
data = {"hashes": torrent_hash}
try:
self._make_request("POST", "/api/v2/torrents/resume", data=data)
logger.info(f"Resumed torrent {torrent_hash}")
return True
except QBittorrentAPIError as e:
logger.error(f"Failed to resume torrent: {e}")
raise
def get_torrent_properties(self, torrent_hash: str) -> Dict[str, Any]:
"""
Get detailed properties of a torrent.
Args:
torrent_hash: Hash of the torrent
Returns:
Dict with torrent properties
"""
if not self._authenticated:
self.login()
params = {"hash": torrent_hash}
try:
data = self._make_request("GET", "/api/v2/torrents/properties", data=params)
return data
except QBittorrentAPIError as e:
logger.error(f"Failed to get torrent properties: {e}")
raise
def _parse_torrent(self, torrent: Dict[str, Any]) -> TorrentInfo:
"""
Parse a torrent dict into a TorrentInfo object.
Args:
torrent: Raw torrent dict from API
Returns:
TorrentInfo object
"""
return TorrentInfo(
hash=torrent.get("hash", ""),
name=torrent.get("name", "Unknown"),
size=torrent.get("size", 0),
progress=torrent.get("progress", 0.0) * 100, # Convert to percentage
state=torrent.get("state", "unknown"),
download_speed=torrent.get("dlspeed", 0),
upload_speed=torrent.get("upspeed", 0),
eta=torrent.get("eta", 0),
num_seeds=torrent.get("num_seeds", 0),
num_leechs=torrent.get("num_leechs", 0),
ratio=torrent.get("ratio", 0.0),
category=torrent.get("category"),
save_path=torrent.get("save_path")
)
+21
View File
@@ -0,0 +1,21 @@
"""qBittorrent Data Transfer Objects."""
from dataclasses import dataclass
from typing import Optional
@dataclass
class TorrentInfo:
"""Represents a torrent in qBittorrent."""
hash: str
name: str
size: int
progress: float
state: str
download_speed: int
upload_speed: int
eta: int
num_seeds: int
num_leechs: int
ratio: float
category: Optional[str] = None
save_path: Optional[str] = None
@@ -0,0 +1,21 @@
"""qBittorrent API exceptions."""
class QBittorrentError(Exception):
"""Base exception for qBittorrent-related errors."""
pass
class QBittorrentConfigurationError(QBittorrentError):
"""Raised when qBittorrent is not properly configured."""
pass
class QBittorrentAPIError(QBittorrentError):
"""Raised when qBittorrent API returns an error."""
pass
class QBittorrentAuthError(QBittorrentError):
"""Raised when authentication fails."""
pass
+23
View File
@@ -0,0 +1,23 @@
"""TMDB API client."""
from .client import TMDBClient
from .dto import MediaResult, ExternalIds
from .exceptions import (
TMDBError,
TMDBConfigurationError,
TMDBAPIError,
TMDBNotFoundError,
)
# Global TMDB client instance (singleton)
tmdb_client = TMDBClient()
__all__ = [
"TMDBClient",
"MediaResult",
"ExternalIds",
"TMDBError",
"TMDBConfigurationError",
"TMDBAPIError",
"TMDBNotFoundError",
"tmdb_client",
]
+281
View File
@@ -0,0 +1,281 @@
"""TMDB (The Movie Database) API client."""
from typing import Dict, Any, Optional, List
import logging
import requests
from requests.exceptions import RequestException, Timeout, HTTPError
from agent.config import Settings, settings
from .dto import MediaResult
from .exceptions import TMDBError, TMDBConfigurationError, TMDBAPIError, TMDBNotFoundError
logger = logging.getLogger(__name__)
class TMDBClient:
"""
Client for interacting with The Movie Database (TMDB) API.
This client provides methods to search for movies and TV shows,
retrieve their details, and get external IDs (like IMDb).
Example:
>>> client = TMDBClient()
>>> result = client.search_media("Inception")
>>> print(result.imdb_id)
'tt1375666'
"""
def __init__(
self,
api_key: Optional[str] = None,
base_url: Optional[str] = None,
timeout: Optional[int] = None,
config: Optional[Settings] = None
):
"""
Initialize TMDB client.
Args:
api_key: TMDB API key (defaults to settings)
base_url: TMDB API base URL (defaults to settings)
timeout: Request timeout in seconds (defaults to settings)
config: Optional Settings instance (for testing)
Raises:
TMDBConfigurationError: If API key is missing
"""
cfg = config or settings
self.api_key = api_key or cfg.tmdb_api_key
self.base_url = base_url or cfg.tmdb_base_url
self.timeout = timeout or cfg.request_timeout
if not self.api_key:
raise TMDBConfigurationError(
"TMDB API key is required. Set TMDB_API_KEY environment variable."
)
if not self.base_url:
raise TMDBConfigurationError(
"TMDB base URL is required. Set TMDB_BASE_URL environment variable."
)
logger.info("TMDB client initialized")
def _make_request(
self,
endpoint: str,
params: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
"""
Make a request to TMDB API.
Args:
endpoint: API endpoint (e.g., '/search/multi')
params: Query parameters
Returns:
JSON response as dict
Raises:
TMDBAPIError: If request fails
"""
url = f"{self.base_url}{endpoint}"
# Add API key to params
request_params = params or {}
request_params['api_key'] = self.api_key
try:
logger.debug(f"TMDB request: {endpoint}")
response = requests.get(url, params=request_params, timeout=self.timeout)
response.raise_for_status()
return response.json()
except Timeout as e:
logger.error(f"TMDB API timeout: {e}")
raise TMDBAPIError(f"Request timeout after {self.timeout} seconds") from e
except HTTPError as e:
logger.error(f"TMDB API HTTP error: {e}")
if e.response is not None:
status_code = e.response.status_code
if status_code == 401:
raise TMDBAPIError("Invalid TMDB API key") from e
elif status_code == 404:
raise TMDBNotFoundError("Resource not found") from e
else:
raise TMDBAPIError(f"HTTP {status_code}: {e}") from e
raise TMDBAPIError(f"HTTP error: {e}") from e
except RequestException as e:
logger.error(f"TMDB API request failed: {e}")
raise TMDBAPIError(f"Failed to connect to TMDB API: {e}") from e
def search_multi(self, query: str) -> List[Dict[str, Any]]:
"""
Search for movies and TV shows.
Args:
query: Search query (movie or TV show title)
Returns:
List of search results
Raises:
TMDBAPIError: If request fails
TMDBNotFoundError: If no results found
"""
if not query or not isinstance(query, str):
raise ValueError("Query must be a non-empty string")
if len(query) > 500:
raise ValueError("Query is too long (max 500 characters)")
data = self._make_request('/search/multi', {'query': query})
results = data.get('results', [])
if not results:
raise TMDBNotFoundError(f"No results found for '{query}'")
logger.info(f"Found {len(results)} results for '{query}'")
return results
def get_external_ids(self, media_type: str, tmdb_id: int) -> Dict[str, Any]:
"""
Get external IDs (IMDb, TVDB, etc.) for a media item.
Args:
media_type: Type of media ('movie' or 'tv')
tmdb_id: TMDB ID of the media
Returns:
Dict with external IDs
Raises:
TMDBAPIError: If request fails
"""
if media_type not in ('movie', 'tv'):
raise ValueError(f"Invalid media_type: {media_type}. Must be 'movie' or 'tv'")
endpoint = f"/{media_type}/{tmdb_id}/external_ids"
return self._make_request(endpoint)
def search_media(self, title: str) -> MediaResult:
"""
Search for a media item and return detailed information including IMDb ID.
This is a convenience method that combines search and external ID lookup.
Args:
title: Title of the movie or TV show
Returns:
MediaResult with all available information
Raises:
TMDBAPIError: If request fails
TMDBNotFoundError: If media not found
"""
# Search for media
results = self.search_multi(title)
# Get the first (most relevant) result
top_result = results[0]
# Validate result structure
if 'id' not in top_result or 'media_type' not in top_result:
raise TMDBAPIError("Invalid TMDB response structure")
tmdb_id = top_result['id']
media_type = top_result['media_type']
# Skip if not movie or TV show
if media_type not in ('movie', 'tv'):
logger.warning(f"Skipping result of type: {media_type}")
if len(results) > 1:
# Try next result
return self._parse_result(results[1])
raise TMDBNotFoundError(f"No movie or TV show found for '{title}'")
return self._parse_result(top_result)
def _parse_result(self, result: Dict[str, Any]) -> MediaResult:
"""
Parse a TMDB result into a MediaResult object.
Args:
result: Raw TMDB result dict
Returns:
MediaResult object
"""
tmdb_id = result['id']
media_type = result['media_type']
title = result.get('title') or result.get('name', 'Unknown')
# Get external IDs (including IMDb)
try:
external_ids = self.get_external_ids(media_type, tmdb_id)
imdb_id = external_ids.get('imdb_id')
except TMDBAPIError as e:
logger.warning(f"Failed to get external IDs: {e}")
imdb_id = None
# Extract other useful information
overview = result.get('overview')
release_date = result.get('release_date') or result.get('first_air_date')
poster_path = result.get('poster_path')
vote_average = result.get('vote_average')
logger.info(f"Found: {title} (Type: {media_type}, TMDB ID: {tmdb_id}, IMDb: {imdb_id})")
return MediaResult(
tmdb_id=tmdb_id,
title=title,
media_type=media_type,
imdb_id=imdb_id,
overview=overview,
release_date=release_date,
poster_path=poster_path,
vote_average=vote_average
)
def get_movie_details(self, movie_id: int) -> Dict[str, Any]:
"""
Get detailed information about a movie.
Args:
movie_id: TMDB movie ID
Returns:
Dict with movie details
Raises:
TMDBAPIError: If request fails
"""
return self._make_request(f'/movie/{movie_id}')
def get_tv_details(self, tv_id: int) -> Dict[str, Any]:
"""
Get detailed information about a TV show.
Args:
tv_id: TMDB TV show ID
Returns:
Dict with TV show details
Raises:
TMDBAPIError: If request fails
"""
return self._make_request(f'/tv/{tv_id}')
def is_configured(self) -> bool:
"""
Check if TMDB client is properly configured.
Returns:
True if configured, False otherwise
"""
return bool(self.api_key and self.base_url)
+26
View File
@@ -0,0 +1,26 @@
"""TMDB Data Transfer Objects."""
from dataclasses import dataclass
from typing import Optional
@dataclass
class MediaResult:
"""Represents a media search result from TMDB."""
tmdb_id: int
title: str
media_type: str # 'movie' or 'tv'
imdb_id: Optional[str] = None
overview: Optional[str] = None
release_date: Optional[str] = None
poster_path: Optional[str] = None
vote_average: Optional[float] = None
@dataclass
class ExternalIds:
"""External IDs for a media item."""
imdb_id: Optional[str] = None
tvdb_id: Optional[int] = None
facebook_id: Optional[str] = None
instagram_id: Optional[str] = None
twitter_id: Optional[str] = None
+21
View File
@@ -0,0 +1,21 @@
"""TMDB API exceptions."""
class TMDBError(Exception):
"""Base exception for TMDB-related errors."""
pass
class TMDBConfigurationError(TMDBError):
"""Raised when TMDB API is not properly configured."""
pass
class TMDBAPIError(TMDBError):
"""Raised when TMDB API returns an error."""
pass
class TMDBNotFoundError(TMDBError):
"""Raised when media is not found."""
pass
+11
View File
@@ -0,0 +1,11 @@
"""Filesystem operations."""
from .file_manager import FileManager
from .organizer import MediaOrganizer
from .exceptions import FilesystemError, PathTraversalError
__all__ = [
"FileManager",
"MediaOrganizer",
"FilesystemError",
"PathTraversalError",
]
+21
View File
@@ -0,0 +1,21 @@
"""Filesystem exceptions."""
class FilesystemError(Exception):
"""Base exception for filesystem operations."""
pass
class PathTraversalError(FilesystemError):
"""Raised when path traversal attack is detected."""
pass
class FileNotFoundError(FilesystemError):
"""Raised when a file is not found."""
pass
class PermissionDeniedError(FilesystemError):
"""Raised when permission is denied."""
pass
+309
View File
@@ -0,0 +1,309 @@
"""File manager - Migrated from agent/tools/filesystem.py with domain logic extracted."""
from typing import Dict, Any, List
from enum import Enum
from pathlib import Path
import logging
import os
import shutil
from .exceptions import FilesystemError, PathTraversalError
from infrastructure.persistence.memory import Memory
logger = logging.getLogger(__name__)
class FolderName(Enum):
"""Types of folders that can be managed."""
DOWNLOAD = "download"
TVSHOW = "tvshow"
MOVIE = "movie"
TORRENT = "torrent"
class FileManager:
"""
File manager for filesystem operations.
Handles folder configuration, listing, and file operations with security.
"""
def __init__(self, memory: Memory):
"""
Initialize file manager.
Args:
memory: Memory instance for folder configuration
"""
self.memory = memory
def set_folder_path(self, folder_name: str, path_value: str) -> Dict[str, Any]:
"""
Set a folder path in the configuration with validation.
Args:
folder_name: Name of folder to set (download, tvshow, movie, torrent)
path_value: Absolute path to the folder
Returns:
Dict with status or error information
"""
try:
# Validate folder name
self._validate_folder_name(folder_name)
# Convert to Path object for better handling
path_obj = Path(path_value).resolve()
# Validate path exists and is a directory
if not path_obj.exists():
logger.warning(f"Path does not exist: {path_value}")
return {
"error": "invalid_path",
"message": f"Path does not exist: {path_value}"
}
if not path_obj.is_dir():
logger.warning(f"Path is not a directory: {path_value}")
return {
"error": "invalid_path",
"message": f"Path is not a directory: {path_value}"
}
# Check if path is readable
if not os.access(path_obj, os.R_OK):
logger.warning(f"Path is not readable: {path_value}")
return {
"error": "permission_denied",
"message": f"Path is not readable: {path_value}"
}
# Store in memory
config = self.memory.get("config", {})
config[f"{folder_name}_folder"] = str(path_obj)
self.memory.set("config", config)
logger.info(f"Set {folder_name}_folder to: {path_obj}")
return {
"status": "ok",
"folder_name": folder_name,
"path": str(path_obj)
}
except ValueError as e:
logger.error(f"Validation error: {e}")
return {"error": "validation_failed", "message": str(e)}
except Exception as e:
logger.error(f"Unexpected error setting path: {e}", exc_info=True)
return {"error": "internal_error", "message": "Failed to set path"}
def list_folder(self, folder_type: str, path: str = ".") -> Dict[str, Any]:
"""
List contents of a folder with security checks.
Args:
folder_type: Type of folder to list (download, tvshow, movie, torrent)
path: Relative path within the folder (default: ".")
Returns:
Dict with folder contents or error information
"""
try:
# Validate folder type
self._validate_folder_name(folder_type)
# Sanitize the path
safe_path = self._sanitize_path(path)
# Get root folder from config
folder_key = f"{folder_type}_folder"
config = self.memory.get("config", {})
if folder_key not in config or not config[folder_key]:
logger.warning(f"Folder not configured: {folder_type}")
return {
"error": "folder_not_set",
"message": f"{folder_type.capitalize()} folder not set in config."
}
root = Path(config[folder_key])
target = root / safe_path
# Security check: ensure target is within root
if not self._is_safe_path(root, target):
logger.warning(f"Path traversal attempt detected: {path}")
return {
"error": "forbidden",
"message": "Access denied: path outside allowed directory"
}
# Check if target exists
if not target.exists():
logger.warning(f"Path does not exist: {target}")
return {
"error": "not_found",
"message": f"Path does not exist: {safe_path}"
}
# Check if target is a directory
if not target.is_dir():
logger.warning(f"Path is not a directory: {target}")
return {
"error": "not_a_directory",
"message": f"Path is not a directory: {safe_path}"
}
# List directory contents
try:
entries = [entry.name for entry in target.iterdir()]
logger.debug(f"Listed {len(entries)} entries in {target}")
return {
"status": "ok",
"folder_type": folder_type,
"path": safe_path,
"entries": sorted(entries),
"count": len(entries)
}
except PermissionError:
logger.warning(f"Permission denied accessing: {target}")
return {
"error": "permission_denied",
"message": f"Permission denied accessing: {safe_path}"
}
except PathTraversalError as e:
logger.warning(f"Path traversal attempt: {e}")
return {
"error": "forbidden",
"message": str(e)
}
except ValueError as e:
logger.error(f"Validation error: {e}")
return {"error": "validation_failed", "message": str(e)}
except Exception as e:
logger.error(f"Unexpected error listing folder: {e}", exc_info=True)
return {"error": "internal_error", "message": "Failed to list folder"}
def move_file(self, source: str, destination: str) -> Dict[str, Any]:
"""
Move a file from one location to another with safety checks.
Args:
source: Source file path
destination: Destination file path
Returns:
Dict with status or error information
"""
try:
# Convert to Path objects
source_path = Path(source).resolve()
dest_path = Path(destination).resolve()
logger.info(f"Moving file from {source_path} to {dest_path}")
# Validate source
if not source_path.exists():
return {
"error": "source_not_found",
"message": f"Source file does not exist: {source}"
}
if not source_path.is_file():
return {
"error": "source_not_file",
"message": f"Source is not a file: {source}"
}
# Get source file size for verification
source_size = source_path.stat().st_size
# Validate destination
dest_parent = dest_path.parent
if not dest_parent.exists():
return {
"error": "destination_dir_not_found",
"message": f"Destination directory does not exist: {dest_parent}"
}
if dest_path.exists():
return {
"error": "destination_exists",
"message": f"Destination file already exists: {destination}"
}
# Perform move
shutil.move(str(source_path), str(dest_path))
# Verify
if not dest_path.exists():
return {
"error": "move_verification_failed",
"message": "File was not moved successfully"
}
dest_size = dest_path.stat().st_size
if dest_size != source_size:
return {
"error": "size_mismatch",
"message": f"File size mismatch after move"
}
logger.info(f"File successfully moved: {dest_path.name}")
return {
"status": "ok",
"source": str(source_path),
"destination": str(dest_path),
"filename": dest_path.name,
"size": dest_size
}
except Exception as e:
logger.error(f"Error moving file: {e}", exc_info=True)
return {
"error": "move_failed",
"message": str(e)
}
def _validate_folder_name(self, folder_name: str) -> bool:
"""Validate folder name against allowed values."""
valid_names = [fn.value for fn in FolderName]
if folder_name not in valid_names:
raise ValueError(
f"Invalid folder_name '{folder_name}'. Must be one of: {', '.join(valid_names)}"
)
return True
def _sanitize_path(self, path: str) -> str:
"""Sanitize path to prevent path traversal attacks."""
# Normalize path
normalized = os.path.normpath(path)
# Check for absolute paths
if os.path.isabs(normalized):
raise PathTraversalError("Absolute paths are not allowed")
# Check for parent directory references
if normalized.startswith("..") or "/.." in normalized or "\\.." in normalized:
raise PathTraversalError("Parent directory references are not allowed")
# Check for null bytes
if "\x00" in normalized:
raise PathTraversalError("Null bytes in path are not allowed")
return normalized
def _is_safe_path(self, base_path: Path, target_path: Path) -> bool:
"""Check if target path is within base path (prevents path traversal)."""
try:
# Resolve both paths to absolute paths
base_resolved = base_path.resolve()
target_resolved = target_path.resolve()
# Check if target is relative to base
target_resolved.relative_to(base_resolved)
return True
except (ValueError, OSError):
return False
+150
View File
@@ -0,0 +1,150 @@
"""Media organizer - Organizes movies and TV shows into proper folder structures."""
from pathlib import Path
import logging
from typing import Optional
from domain.movies.entities import Movie
from domain.tv_shows.entities import TVShow, Episode
from domain.shared.value_objects import FilePath
logger = logging.getLogger(__name__)
class MediaOrganizer:
"""
Organizes media files into proper folder structures.
This service knows how to organize movies and TV shows according to
common media server conventions (Plex, Jellyfin, etc.).
"""
def __init__(self, movie_folder: Path, tvshow_folder: Path):
"""
Initialize media organizer.
Args:
movie_folder: Root folder for movies
tvshow_folder: Root folder for TV shows
"""
self.movie_folder = movie_folder
self.tvshow_folder = tvshow_folder
def get_movie_destination(self, movie: Movie, filename: str) -> Path:
"""
Get the destination path for a movie file.
Structure: /movies/Movie Title (Year)/Movie.Title.Year.Quality.ext
Args:
movie: Movie entity
filename: Original filename (to extract extension)
Returns:
Full destination path
"""
# Create movie folder
folder_name = movie.get_folder_name()
movie_dir = self.movie_folder / folder_name
# Get extension from original filename
extension = Path(filename).suffix
# Create new filename
new_filename = movie.get_filename() + extension
return movie_dir / new_filename
def get_episode_destination(
self,
show: TVShow,
episode: Episode,
filename: str
) -> Path:
"""
Get the destination path for a TV show episode file.
Structure: /tvshows/Show.Name/Season 01/S01E05.Episode.Title.ext
Args:
show: TVShow entity
episode: Episode entity
filename: Original filename (to extract extension)
Returns:
Full destination path
"""
# Create show folder
show_folder_name = show.get_folder_name()
show_dir = self.tvshow_folder / show_folder_name
# Create season folder
from domain.tv_shows.entities import Season
season = Season(
show_imdb_id=show.imdb_id,
season_number=episode.season_number,
episode_count=0 # Not needed for folder name
)
season_folder_name = season.get_folder_name()
season_dir = show_dir / season_folder_name
# Get extension from original filename
extension = Path(filename).suffix
# Create new filename
new_filename = episode.get_filename() + extension
return season_dir / new_filename
def create_movie_directory(self, movie: Movie) -> bool:
"""
Create the directory structure for a movie.
Args:
movie: Movie entity
Returns:
True if successful
"""
folder_name = movie.get_folder_name()
movie_dir = self.movie_folder / folder_name
try:
movie_dir.mkdir(parents=True, exist_ok=True)
logger.info(f"Created movie directory: {movie_dir}")
return True
except Exception as e:
logger.error(f"Failed to create movie directory: {e}")
return False
def create_episode_directory(self, show: TVShow, season_number: int) -> bool:
"""
Create the directory structure for a TV show season.
Args:
show: TVShow entity
season_number: Season number
Returns:
True if successful
"""
from domain.tv_shows.entities import Season
from domain.tv_shows.value_objects import SeasonNumber
show_folder_name = show.get_folder_name()
show_dir = self.tvshow_folder / show_folder_name
season = Season(
show_imdb_id=show.imdb_id,
season_number=SeasonNumber(season_number),
episode_count=0
)
season_folder_name = season.get_folder_name()
season_dir = show_dir / season_folder_name
try:
season_dir.mkdir(parents=True, exist_ok=True)
logger.info(f"Created season directory: {season_dir}")
return True
except Exception as e:
logger.error(f"Failed to create season directory: {e}")
return False
+1
View File
@@ -0,0 +1 @@
"""Persistence layer - Data storage implementations."""
@@ -0,0 +1,10 @@
"""JSON-based repository implementations."""
from .movie_repository import JsonMovieRepository
from .tvshow_repository import JsonTVShowRepository
from .subtitle_repository import JsonSubtitleRepository
__all__ = [
"JsonMovieRepository",
"JsonTVShowRepository",
"JsonSubtitleRepository",
]
@@ -0,0 +1,115 @@
"""JSON-based movie repository implementation."""
from typing import List, Optional, Dict, Any
import logging
from domain.movies.repositories import MovieRepository
from domain.movies.entities import Movie
from domain.shared.value_objects import ImdbId
from ..memory import Memory
logger = logging.getLogger(__name__)
class JsonMovieRepository(MovieRepository):
"""
JSON-based implementation of MovieRepository.
Stores movies in the memory.json file.
"""
def __init__(self, memory: Memory):
"""
Initialize repository.
Args:
memory: Memory instance for persistence
"""
self.memory = memory
def save(self, movie: Movie) -> None:
"""Save a movie to the repository."""
movies = self._load_all()
# Remove existing movie with same IMDb ID
movies = [m for m in movies if m.get('imdb_id') != str(movie.imdb_id)]
# Add new movie
movies.append(self._to_dict(movie))
# Save to memory
self.memory.set('movies', movies)
logger.debug(f"Saved movie: {movie.imdb_id}")
def find_by_imdb_id(self, imdb_id: ImdbId) -> Optional[Movie]:
"""Find a movie by its IMDb ID."""
movies = self._load_all()
for movie_dict in movies:
if movie_dict.get('imdb_id') == str(imdb_id):
return self._from_dict(movie_dict)
return None
def find_all(self) -> List[Movie]:
"""Get all movies in the repository."""
movies_dict = self._load_all()
return [self._from_dict(m) for m in movies_dict]
def delete(self, imdb_id: ImdbId) -> bool:
"""Delete a movie from the repository."""
movies = self._load_all()
initial_count = len(movies)
# Filter out the movie
movies = [m for m in movies if m.get('imdb_id') != str(imdb_id)]
if len(movies) < initial_count:
self.memory.set('movies', movies)
logger.debug(f"Deleted movie: {imdb_id}")
return True
return False
def exists(self, imdb_id: ImdbId) -> bool:
"""Check if a movie exists in the repository."""
return self.find_by_imdb_id(imdb_id) is not None
def _load_all(self) -> List[Dict[str, Any]]:
"""Load all movies from memory."""
return self.memory.get('movies', [])
def _to_dict(self, movie: Movie) -> Dict[str, Any]:
"""Convert Movie entity to dict for storage."""
return {
'imdb_id': str(movie.imdb_id),
'title': movie.title.value,
'release_year': movie.release_year.value if movie.release_year else None,
'quality': movie.quality.value,
'file_path': str(movie.file_path) if movie.file_path else None,
'file_size': movie.file_size.bytes if movie.file_size else None,
'tmdb_id': movie.tmdb_id,
'overview': movie.overview,
'poster_path': movie.poster_path,
'vote_average': movie.vote_average,
'added_at': movie.added_at.isoformat(),
}
def _from_dict(self, data: Dict[str, Any]) -> Movie:
"""Convert dict from storage to Movie entity."""
from domain.movies.value_objects import MovieTitle, ReleaseYear, Quality
from domain.shared.value_objects import FilePath, FileSize
from datetime import datetime
return Movie(
imdb_id=ImdbId(data['imdb_id']),
title=MovieTitle(data['title']),
release_year=ReleaseYear(data['release_year']) if data.get('release_year') else None,
quality=Quality(data.get('quality', 'unknown')),
file_path=FilePath(data['file_path']) if data.get('file_path') else None,
file_size=FileSize(data['file_size']) if data.get('file_size') else None,
tmdb_id=data.get('tmdb_id'),
overview=data.get('overview'),
poster_path=data.get('poster_path'),
vote_average=data.get('vote_average'),
added_at=datetime.fromisoformat(data['added_at']) if data.get('added_at') else datetime.now(),
)
@@ -0,0 +1,127 @@
"""JSON-based subtitle repository implementation."""
from typing import List, Optional, Dict, Any
import logging
from domain.subtitles.repositories import SubtitleRepository
from domain.subtitles.entities import Subtitle
from domain.subtitles.value_objects import Language, SubtitleFormat, TimingOffset
from domain.shared.value_objects import ImdbId, FilePath
from ..memory import Memory
logger = logging.getLogger(__name__)
class JsonSubtitleRepository(SubtitleRepository):
"""
JSON-based implementation of SubtitleRepository.
Stores subtitles in the memory.json file.
"""
def __init__(self, memory: Memory):
"""
Initialize repository.
Args:
memory: Memory instance for persistence
"""
self.memory = memory
def save(self, subtitle: Subtitle) -> None:
"""Save a subtitle to the repository."""
subtitles = self._load_all()
# Add new subtitle (we allow multiple subtitles for same media)
subtitles.append(self._to_dict(subtitle))
# Save to memory
self.memory.set('subtitles', subtitles)
logger.debug(f"Saved subtitle for: {subtitle.media_imdb_id}")
def find_by_media(
self,
media_imdb_id: ImdbId,
language: Optional[Language] = None,
season: Optional[int] = None,
episode: Optional[int] = None
) -> List[Subtitle]:
"""Find subtitles for a media item."""
subtitles = self._load_all()
results = []
for sub_dict in subtitles:
# Filter by IMDb ID
if sub_dict.get('media_imdb_id') != str(media_imdb_id):
continue
# Filter by language if specified
if language and sub_dict.get('language') != language.value:
continue
# Filter by season/episode if specified
if season is not None and sub_dict.get('season_number') != season:
continue
if episode is not None and sub_dict.get('episode_number') != episode:
continue
results.append(self._from_dict(sub_dict))
return results
def delete(self, subtitle: Subtitle) -> bool:
"""Delete a subtitle from the repository."""
subtitles = self._load_all()
initial_count = len(subtitles)
# Filter out the subtitle (match by file path)
subtitles = [
s for s in subtitles
if s.get('file_path') != str(subtitle.file_path)
]
if len(subtitles) < initial_count:
self.memory.set('subtitles', subtitles)
logger.debug(f"Deleted subtitle: {subtitle.file_path}")
return True
return False
def _load_all(self) -> List[Dict[str, Any]]:
"""Load all subtitles from memory."""
return self.memory.get('subtitles', [])
def _to_dict(self, subtitle: Subtitle) -> Dict[str, Any]:
"""Convert Subtitle entity to dict for storage."""
return {
'media_imdb_id': str(subtitle.media_imdb_id),
'language': subtitle.language.value,
'format': subtitle.format.value,
'file_path': str(subtitle.file_path),
'season_number': subtitle.season_number,
'episode_number': subtitle.episode_number,
'timing_offset': subtitle.timing_offset.milliseconds,
'hearing_impaired': subtitle.hearing_impaired,
'forced': subtitle.forced,
'source': subtitle.source,
'uploader': subtitle.uploader,
'download_count': subtitle.download_count,
'rating': subtitle.rating,
}
def _from_dict(self, data: Dict[str, Any]) -> Subtitle:
"""Convert dict from storage to Subtitle entity."""
return Subtitle(
media_imdb_id=ImdbId(data['media_imdb_id']),
language=Language.from_code(data['language']),
format=SubtitleFormat.from_extension(data['format']),
file_path=FilePath(data['file_path']),
season_number=data.get('season_number'),
episode_number=data.get('episode_number'),
timing_offset=TimingOffset(data.get('timing_offset', 0)),
hearing_impaired=data.get('hearing_impaired', False),
forced=data.get('forced', False),
source=data.get('source'),
uploader=data.get('uploader'),
download_count=data.get('download_count'),
rating=data.get('rating'),
)
@@ -0,0 +1,112 @@
"""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
from domain.shared.value_objects import ImdbId
from ..memory import Memory
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).
"""
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()
# 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.append(self._to_dict(show))
# Save to memory
self.memory.set('tv_shows', shows)
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()
for show_dict in shows:
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()
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()
initial_count = len(shows)
# Filter out the show
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)
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."""
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]:
"""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(),
}
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(),
)
+86
View File
@@ -0,0 +1,86 @@
"""Memory storage - Migrated from agent/memory.py"""
from pathlib import Path
from typing import Any, Dict
import json
from agent.config import settings
from agent.parameters import validate_parameter, get_parameter_schema
class Memory:
"""
Generic memory storage for agent state.
Provides a simple key-value store that persists to JSON.
"""
def __init__(self, path: str = "memory.json"):
self.file = Path(path)
self.data: Dict[str, Any] = {}
self.load()
def load(self) -> None:
"""Load memory from file or initialize with defaults."""
if self.file.exists():
try:
self.data = json.loads(self.file.read_text(encoding="utf-8"))
except (json.JSONDecodeError, IOError) as e:
print(f"Warning: Could not load memory file: {e}")
self.data = {
"config": {},
"tv_shows": [],
"history": [],
}
else:
self.data = {
"config": {},
"tv_shows": [],
"history": [],
}
def save(self) -> None:
self.file.write_text(
json.dumps(self.data, indent=2, ensure_ascii=False),
encoding="utf-8",
)
def get(self, key: str, default: Any = None) -> Any:
"""Get a value from memory by key."""
return self.data.get(key, default)
def set(self, key: str, value: Any) -> None:
"""
Set a value in memory and save.
Validates the value against the parameter schema if one exists.
"""
# Validate if schema exists
is_valid, error_msg = validate_parameter(key, value)
if not is_valid:
print(f'Validation failed for {key}: {error_msg}')
raise ValueError(f"Invalid value for {key}: {error_msg}")
print(f'Setting {key} in memory to: {value}')
self.data[key] = value
self.save()
def has(self, key: str) -> bool:
"""Check if a key exists and has a non-None value."""
return key in self.data and self.data[key] is not None
def append_history(self, role: str, content: str) -> None:
"""
Append a message to conversation history.
Args:
role: Message role ('user' or 'assistant')
content: Message content
"""
if "history" not in self.data:
self.data["history"] = []
self.data["history"].append({
"role": role,
"content": content
})
self.save()