Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

Major refactor done with kimi-k2.5 in claude code

Alishahryar1fcbe204f4408
diff --git a/api/app.py b/api/app.py
index 028886dc..f4188fb0 100644
--- a/api/app.py
+++ b/api/app.py
@@ -16,7 +16,8 @@ from providers.exceptions import ProviderError
from config.settings import get_settings
# Configure logging (atomic - only on true fresh start)
-LOG_FILE = "server.log"
+_settings = get_settings()
+LOG_FILE = _settings.log_file
# Check if logging is already configured (e.g., hot reload)
# If handlers exist, skip setup to avoid clearing logs mid-session
@@ -49,11 +50,18 @@ async def lifespan(app: FastAPI):
cli_manager = None
try:
- if settings.telegram_bot_token:
- from messaging.telegram import TelegramPlatform
+ # Use the messaging factory to create the right platform
+ from messaging.factory import create_messaging_platform
+
+ messaging_platform = create_messaging_platform(
+ platform_type=settings.messaging_platform,
+ bot_token=settings.telegram_bot_token,
+ allowed_user_id=settings.allowed_telegram_user_id,
+ )
+
+ if messaging_platform:
from messaging.handler import ClaudeMessageHandler
from messaging.session import SessionStore
-
from cli.manager import CLISessionManager
# Setup workspace - CLI runs in allowed_dir if set (e.g. project root)
@@ -64,14 +72,15 @@ async def lifespan(app: FastAPI):
)
os.makedirs(workspace, exist_ok=True)
- # Session data (Telegram session, app sessions) stored in .agent_workspace
+ # Session data stored in agent_workspace
data_path = os.path.abspath(settings.claude_workspace)
os.makedirs(data_path, exist_ok=True)
+ api_url = f"http://{settings.host}:{settings.port}/v1"
allowed_dirs = [workspace] if settings.allowed_dir else []
cli_manager = CLISessionManager(
workspace_path=workspace,
- api_url="http://localhost:8082/v1",
+ api_url=api_url,
allowed_dirs=allowed_dirs,
max_sessions=settings.max_cli_sessions,
)
@@ -81,12 +90,6 @@ async def lifespan(app: FastAPI):
storage_path=os.path.join(data_path, "sessions.json")
)
- # Create Telegram platform
- messaging_platform = TelegramPlatform(
- bot_token=settings.telegram_bot_token,
- allowed_user_id=settings.allowed_telegram_user_id,
- )
-
# Create and register message handler
message_handler = ClaudeMessageHandler(
platform=messaging_platform,
@@ -95,33 +98,33 @@ async def lifespan(app: FastAPI):
)
# Restore tree state if available
- if session_store._trees:
+ saved_trees = session_store.get_all_trees()
+ if saved_trees:
logger.info(
- f"Restoring {len(session_store._trees)} conversation trees..."
+ f"Restoring {len(saved_trees)} conversation trees..."
)
from messaging.tree_queue import TreeQueueManager
message_handler.tree_queue = TreeQueueManager.from_dict(
{
- "trees": session_store._trees,
- "node_to_tree": session_store._node_to_tree,
+ "trees": saved_trees,
+ "node_to_tree": session_store.get_node_mapping(),
}
)
# Reconcile restored state - anything PENDING/IN_PROGRESS is lost across restart
if message_handler.tree_queue.cleanup_stale_nodes() > 0:
# Sync back and save
- session_store._trees = message_handler.tree_queue.to_dict()["trees"]
- session_store._node_to_tree = message_handler.tree_queue.to_dict()[
- "node_to_tree"
- ]
- session_store._save()
+ tree_data = message_handler.tree_queue.to_dict()
+ session_store.sync_from_tree_data(
+ tree_data["trees"], tree_data["node_to_tree"]
+ )
# Wire up the handler
messaging_platform.on_message(message_handler.handle_message)
# Start the platform
await messaging_platform.start()
- logger.info("Telegram platform started with message handler")
+ logger.info(f"{messaging_platform.name} platform started with message handler")
except ImportError as e:
logger.warning(f"Messaging module import error: {e}")
diff --git a/api/dependencies.py b/api/dependencies.py
index 5eac1a69..834743be 100644
--- a/api/dependencies.py
+++ b/api/dependencies.py
@@ -2,12 +2,11 @@
from typing import Optional
from config.settings import Settings, get_settings as _get_settings, NVIDIA_NIM_BASE_URL
-from providers.base import ProviderConfig
-from providers.nvidia_nim import NvidiaNimProvider
+from providers.base import BaseProvider, ProviderConfig
# Global provider instance (singleton)
-_provider: Optional[NvidiaNimProvider] = None
+_provider: Optional[BaseProvider] = None
def get_settings() -> Settings:
@@ -15,18 +14,53 @@ def get_settings() -> Settings:
return _get_settings()
-def get_provider() -> NvidiaNimProvider:
- """Get or create the NvidiaNimProvider instance."""
+def _build_nim_extra_params(settings: Settings) -> dict:
+ """Build NIM-specific extra_params from settings."""
+ params = {
+ "temperature": settings.nvidia_nim_temperature,
+ "top_p": settings.nvidia_nim_top_p,
+ "max_tokens": settings.nvidia_nim_max_tokens,
+ }
+ # Only include non-default values to avoid overriding request-level settings
+ if settings.nvidia_nim_top_k != -1:
+ params["top_k"] = settings.nvidia_nim_top_k
+ if settings.nvidia_nim_presence_penalty != 0.0:
+ params["presence_penalty"] = settings.nvidia_nim_presence_penalty
+ if settings.nvidia_nim_frequency_penalty != 0.0:
+ params["frequency_penalty"] = settings.nvidia_nim_frequency_penalty
+ if settings.nvidia_nim_min_p != 0.0:
+ params["min_p"] = settings.nvidia_nim_min_p
+ if settings.nvidia_nim_repetition_penalty != 1.0:
+ params["repetition_penalty"] = settings.nvidia_nim_repetition_penalty
+ if settings.nvidia_nim_seed is not None:
+ params["seed"] = settings.nvidia_nim_seed
+ if settings.nvidia_nim_stop:
+ params["stop"] = settings.nvidia_nim_stop
+ return params
+
+
+def get_provider() -> BaseProvider:
+ """Get or create the provider instance based on settings.provider_type."""
global _provider
if _provider is None:
settings = get_settings()
- config = ProviderConfig(
- api_key=settings.nvidia_nim_api_key,
- base_url=NVIDIA_NIM_BASE_URL, # Use constant, not from settings
- rate_limit=settings.nvidia_nim_rate_limit,
- rate_window=settings.nvidia_nim_rate_window,
- )
- _provider = NvidiaNimProvider(config)
+
+ if settings.provider_type == "nvidia_nim":
+ from providers.nvidia_nim import NvidiaNimProvider
+
+ config = ProviderConfig(
+ api_key=settings.nvidia_nim_api_key,
+ base_url=NVIDIA_NIM_BASE_URL,
+ rate_limit=settings.nvidia_nim_rate_limit,
+ rate_window=settings.nvidia_nim_rate_window,
+ extra_params=_build_nim_extra_params(settings),
+ )
+ _provider = NvidiaNimProvider(config)
+ else:
+ raise ValueError(
+ f"Unknown provider_type: '{settings.provider_type}'. "
+ f"Supported: 'nvidia_nim'"
+ )
return _provider
diff --git a/api/request_utils.py b/api/request_utils.py
index c19e66ed..e128b9a7 100644
--- a/api/request_utils.py
+++ b/api/request_utils.py
@@ -16,6 +16,24 @@ logger = logging.getLogger(__name__)
ENCODER = tiktoken.get_encoding("cl100k_base")
+def extract_text_from_content(content) -> str:
+ """Extract concatenated text from message content (str or list of content blocks).
+
+ Handles the common pattern of content being either a plain string
+ or a list of content blocks with a .text attribute.
+ """
+ if isinstance(content, str):
+ return content
+ if isinstance(content, list):
+ parts = []
+ for block in content:
+ text = getattr(block, "text", "")
+ if text and isinstance(text, str):
+ parts.append(text)
+ return "".join(parts)
+ return ""
+
+
def is_quota_check_request(request_data: MessagesRequest) -> bool:
"""Check if this is a quota probe request.
@@ -33,16 +51,9 @@ def is_quota_check_request(request_data: MessagesRequest) -> bool:
and len(request_data.messages) == 1
and request_data.messages[0].role == "user"
):
- content = request_data.messages[0].content
- # Check string content
- if isinstance(content, str) and "quota" in content.lower():
+ text = extract_text_from_content(request_data.messages[0].content)
+ if "quota" in text.lower():
return True
- # Check list content
- elif isinstance(content, list):
- for block in content:
- text = getattr(block, "text", "")
- if text and isinstance(text, str) and "quota" in text.lower():
- return True
return False
@@ -59,17 +70,9 @@ def is_title_generation_request(request_data: MessagesRequest) -> bool:
True if this is a title generation request
"""
if len(request_data.messages) > 0 and request_data.messages[-1].role == "user":
- content = request_data.messages[-1].content
- # Check string content
- target_phrase = "write a 5-10 word title"
- if isinstance(content, str) and target_phrase in content.lower():
+ text = extract_text_from_content(request_data.messages[-1].content)
+ if "write a 5-10 word title" in text.lower():
return True
- # Check list content
- elif isinstance(content, list):
- for block in content:
- text = getattr(block, "text", "")
- if text and isinstance(text, str) and target_phrase in text.lower():
- return True
return False
@@ -155,15 +158,7 @@ def is_prefix_detection_request(request_data: MessagesRequest) -> Tuple[bool, st
if len(request_data.messages) != 1 or request_data.messages[0].role != "user":
return False, ""
- msg = request_data.messages[0]
- content = ""
- if isinstance(msg.content, str):
- content = msg.content
- elif isinstance(msg.content, list):
- for block in msg.content:
- text = getattr(block, "text", "")
- if text and isinstance(text, str):
- content += text
+ content = extract_text_from_content(request_data.messages[0].content)
if "<policy_spec>" in content and "Command:" in content:
try:
@@ -189,15 +184,9 @@ def is_suggestion_mode_request(request_data: MessagesRequest) -> bool:
"""
for msg in request_data.messages:
if msg.role == "user":
- content = msg.content
- target = "[SUGGESTION MODE:"
- if isinstance(content, str) and target in content:
+ text = extract_text_from_content(msg.content)
+ if "[SUGGESTION MODE:" in text:
return True
- elif isinstance(content, list):
- for block in content:
- text = getattr(block, "text", "")
- if text and isinstance(text, str) and target in text:
- return True
return False
@@ -222,15 +211,7 @@ def is_filepath_extraction_request(
if request_data.tools:
return False, "", ""
- msg = request_data.messages[0]
- content = ""
- if isinstance(msg.content, str):
- content = msg.content
- elif isinstance(msg.content, list):
- for block in msg.content:
- text = getattr(block, "text", "")
- if text and isinstance(text, str):
- content += text
+ content = extract_text_from_content(request_data.messages[0].content)
# Must have Command: and Output: markers
if "Command:" not in content or "Output:" not in content:
diff --git a/api/routes.py b/api/routes.py
index c94e1263..19f39a69 100644
--- a/api/routes.py
+++ b/api/routes.py
@@ -25,7 +25,7 @@ from .request_utils import (
get_token_count,
)
from config.settings import Settings
-from providers.nvidia_nim import NvidiaNimProvider
+from providers.base import BaseProvider
from providers.exceptions import ProviderError
from providers.logging_utils import log_request_compact
@@ -43,7 +43,7 @@ router = APIRouter()
async def create_message(
request_data: MessagesRequest,
raw_request: Request,
- provider: NvidiaNimProvider = Depends(get_provider),
+ provider: BaseProvider = Depends(get_provider),
settings: Settings = Depends(get_settings),
):
"""Create a message (streaming or non-streaming)."""
@@ -183,7 +183,7 @@ async def stop_cli(request: Request):
if cli_manager:
await cli_manager.stop_all()
return {"status": "stopped", "source": "cli_manager"}
- return HTTPException(status_code=503, detail="Messaging system not initialized")
+ raise HTTPException(status_code=503, detail="Messaging system not initialized")
count = await handler.stop_all_tasks()
return {"status": "stopped", "cancelled_count": count}
diff --git a/config/settings.py b/config/settings.py
index ff72d535..35e93012 100644
--- a/config/settings.py
+++ b/config/settings.py
@@ -15,6 +15,12 @@ NVIDIA_NIM_BASE_URL = "https://integrate.api.nvidia.com/v1"
class Settings(BaseSettings):
"""Application settings loaded from environment variables."""
+ # ==================== Provider Selection ====================
+ provider_type: str = "nvidia_nim"
+
+ # ==================== Messaging Platform Selection ====================
+ messaging_platform: str = "telegram"
+
# ==================== NVIDIA NIM Config ====================
nvidia_nim_api_key: str = ""
@@ -65,8 +71,6 @@ class Settings(BaseSettings):
# ==================== Bot Wrapper Config ====================
telegram_bot_token: Optional[str] = None
- telegram_api_id: Optional[str] = None # Deprecated
- telegram_api_hash: Optional[str] = None # Deprecated
allowed_telegram_user_id: Optional[str] = None
claude_workspace: str = "./agent_workspace"
allowed_dir: str = ""
@@ -75,6 +79,7 @@ class Settings(BaseSettings):
# ==================== Server ====================
host: str = "0.0.0.0"
port: int = 8082
+ log_file: str = "server.log"
# Handle empty strings for optional int fields
@field_validator("nvidia_nim_seed", mode="before")
@@ -88,8 +93,6 @@ class Settings(BaseSettings):
@field_validator(
"nvidia_nim_stop",
"telegram_bot_token",
- "telegram_api_id",
- "telegram_api_hash",
"allowed_telegram_user_id",
mode="before",
)
diff --git a/messaging/__init__.py b/messaging/__init__.py
index 9499434e..f50ec252 100644
--- a/messaging/__init__.py
+++ b/messaging/__init__.py
@@ -1,7 +1,7 @@
"""Platform-agnostic messaging layer."""
from .base import MessagingPlatform, SessionManagerInterface, CLISession
-from .models import IncomingMessage, OutgoingMessage
+from .models import IncomingMessage
from .handler import ClaudeMessageHandler
from .session import SessionStore
from .tree_data import MessageTree, MessageNode, MessageState
@@ -13,7 +13,6 @@ __all__ = [
"SessionManagerInterface",
"CLISession",
"IncomingMessage",
- "OutgoingMessage",
"ClaudeMessageHandler",
"SessionStore",
"TreeQueueManager",
diff --git a/messaging/factory.py b/messaging/factory.py
new file mode 100644
index 00000000..dc701981
--- /dev/null
+++ b/messaging/factory.py
@@ -0,0 +1,49 @@
+"""Messaging platform factory.
+
+Creates the appropriate messaging platform adapter based on configuration.
+To add a new platform (e.g. Discord, Slack):
+1. Create a new class implementing MessagingPlatform in messaging/
+2. Add a case to create_messaging_platform() below
+"""
+
+import logging
+from typing import Optional
+
+from .base import MessagingPlatform
+
+logger = logging.getLogger(__name__)
+
+
+def create_messaging_platform(
+ platform_type: str,
+ **kwargs,
+) -> Optional[MessagingPlatform]:
+ """Create a messaging platform instance based on type.
+
+ Args:
+ platform_type: Platform identifier ("telegram", "discord", etc.)
+ **kwargs: Platform-specific configuration passed to the constructor.
+
+ Returns:
+ Configured MessagingPlatform instance, or None if not configured.
+ """
+ if platform_type == "telegram":
+ bot_token = kwargs.get("bot_token")
+ if not bot_token:
+ logger.info("No Telegram bot token configured, skipping platform setup")
+ return None
+
+ from .telegram import TelegramPlatform
+
+ return TelegramPlatform(
+ bot_token=bot_token,
+ allowed_user_id=kwargs.get("allowed_user_id"),
+ )
+
+ # Add new platforms here:
+ # elif platform_type == "discord":
+ # from .discord import DiscordPlatform
+ # return DiscordPlatform(...)
+
+ logger.warning(f"Unknown messaging platform: '{platform_type}'. Supported: 'telegram'")
+ return None
diff --git a/messaging/handler.py b/messaging/handler.py
index 2026051f..86a9e37b 100644
--- a/messaging/handler.py
+++ b/messaging/handler.py
@@ -9,7 +9,7 @@ Uses tree-based queuing for message ordering.
import time
import asyncio
import logging
-from typing import Optional, TYPE_CHECKING
+from typing import List, Optional
from .base import MessagingPlatform, SessionManagerInterface
from .models import IncomingMessage
@@ -17,9 +17,6 @@ from .session import SessionStore
from .tree_queue import TreeQueueManager, MessageNode, MessageState
from .event_parser import parse_cli_event
-if TYPE_CHECKING:
- pass
-
logger = logging.getLogger(__name__)
@@ -92,15 +89,10 @@ class ClaudeMessageHandler:
# Send initial status message
status_text = self._get_initial_status(tree, parent_node_id)
- # Using handle_message might still need the ID immediately, but we can queue it
- # and wait if needed, or fire and forget if the ID is generated by the platform.
- # For Telegram, we need the ID to track the status message.
status_msg_id = await self.platform.queue_send_message(
incoming.chat_id,
status_text,
- reply_to=incoming.message_id
- if incoming.message_id != incoming.message_id
- else None,
+ reply_to=incoming.message_id,
fire_and_forget=False,
)
@@ -198,8 +190,6 @@ class ClaudeMessageHandler:
display = self._build_message(components, status)
if display and display != last_displayed_text:
last_displayed_text = display
- # Use queued edit for non-blocking, thread-safe UI updates
- # Rate limiting and flood wait retries are handled by GlobalRateLimiter
await self.platform.queue_edit_message(
chat_id, status_msg_id, display, parse_mode="markdown"
)
@@ -280,7 +270,6 @@ class ClaudeMessageHandler:
if not any(components.values()):
components["content"].append("Done.")
logger.info("HANDLER: Task complete, updating UI")
- # Always force final complete status to bypass flood wait
await update_ui("✅ **Complete**", force=True)
# Update node state and session
@@ -299,75 +288,57 @@ class ClaudeMessageHandler:
)
components["errors"].append(error_msg)
logger.info("HANDLER: Updating UI with error status")
- # Always force error status to bypass flood wait
await update_ui("❌ **Error**", force=True)
if tree:
- # Mark this node and propagate to pending children
- affected = await self.tree_queue.mark_node_error(
- node_id, error_msg, propagate_to_children=True
+ await self._propagate_error_to_children(
+ node_id, error_msg, "Parent task failed"
)
- # Update status messages for all affected children
- for child in affected[1:]: # Skip first (current node)
- # Fire and forget these updates so they don't block the worker
- self.platform.fire_and_forget(
- self.platform.queue_edit_message(
- child.incoming.chat_id,
- child.status_message_id,
- "❌ **Cancelled:** Parent task failed",
- parse_mode="markdown",
- )
- )
except asyncio.CancelledError:
logger.warning(f"HANDLER: Task cancelled for node {node_id}")
components["errors"].append("Task was cancelled")
- # Always force cancelled status to bypass flood wait
await update_ui("❌ **Cancelled**", force=True)
if tree:
- # Mark this node and propagate to pending children
- affected = await self.tree_queue.mark_node_error(
- node_id, "Cancelled by user", propagate_to_children=True
+ await self._propagate_error_to_children(
+ node_id, "Cancelled by user", "Parent task was stopped"
)
- # Update status messages for all affected children
- for child in affected[1:]:
- # Fire and forget these updates
- self.platform.fire_and_forget(
- self.platform.queue_edit_message(
- child.incoming.chat_id,
- child.status_message_id,
- "❌ **Cancelled:** Parent task was stopped",
- parse_mode="markdown",
- )
- )
except Exception as e:
logger.error(
f"HANDLER: Task failed with exception: {type(e).__name__}: {e}"
)
error_msg = str(e)[:200]
components["errors"].append(error_msg)
- # Always force error status to bypass flood wait
await update_ui("💥 **Task Failed**", force=True)
if tree:
- # Mark this node and propagate to pending children
- affected = await self.tree_queue.mark_node_error(
- node_id, error_msg, propagate_to_children=True
+ await self._propagate_error_to_children(
+ node_id, error_msg, "Parent task failed"
)
- # Update status messages for all affected children
- for child in affected[1:]:
- # Fire and forget these updates
- self.platform.fire_and_forget(
- self.platform.queue_edit_message(
- child.incoming.chat_id,
- child.status_message_id,
- "❌ **Cancelled:** Parent task failed",
- parse_mode="markdown",
- )
- )
finally:
logger.info(
f"HANDLER: _process_node completed for node {node_id}, errors={len(components['errors'])}"
)
+ async def _propagate_error_to_children(
+ self,
+ node_id: str,
+ error_msg: str,
+ child_status_text: str,
+ ) -> None:
+ """Mark node as error and propagate to pending children with UI updates."""
+ affected = await self.tree_queue.mark_node_error(
+ node_id, error_msg, propagate_to_children=True
+ )
+ # Update status messages for all affected children (skip first = current node)
+ for child in affected[1:]:
+ self.platform.fire_and_forget(
+ self.platform.queue_edit_message(
+ child.incoming.chat_id,
+ child.status_message_id,
+ f"❌ **Cancelled:** {child_status_text}",
+ parse_mode="markdown",
+ )
+ )
+
def _build_message(
self,
components: dict,
@@ -386,7 +357,6 @@ class ClaudeMessageHandler:
if len(thinking_text) > 1000:
thinking_text = "..." + thinking_text[-995:]
- # Ensure it doesn't break a code block if we eventually support them inside thinking
lines.append(f"💭 **Thinking:**\n```\n{thinking_text}\n```")
# 2. Tools
@@ -423,8 +393,6 @@ class ClaudeMessageHandler:
# Filter out empty lines first for a clean join
lines = [l for l in lines if l]
- # The main content is everything EXCEPT the status if provided
- # We handle status separately to ensure it's always included
main_text = "\n".join(lines)
status_text = f"\n\n{status}" if status else ""
@@ -472,20 +440,14 @@ class ClaudeMessageHandler:
Stop all pending and in-progress tasks.
Order of operations:
- 1. Set stopping flag to prevent new tasks from starting
- 2. Cancel tree queue tasks
- 3. Stop CLI sessions
- 4. Update UI for all affected nodes
+ 1. Cancel tree queue tasks (uses internal locking)
+ 2. Stop CLI sessions
+ 3. Update UI for all affected nodes
"""
- # Set a temporary flag on the tree_queue manager if possible, or just lock everything
- # Since we are in the handler, we can use the manager's lock to ensure consistency
-
- async with self.tree_queue._lock:
- # 1. Cancel tree queue tasks FIRST while holding the manager lock
- # This ensures we capture the count of active tasks before they clean up
- logger.info("Cancelling tree queue tasks...")
- cancelled_nodes = self.tree_queue.cancel_all_sync()
- logger.info(f"Cancelled {len(cancelled_nodes)} nodes")
+ # 1. Cancel tree queue tasks using the public async method
+ logger.info("Cancelling tree queue tasks...")
+ cancelled_nodes = await self.tree_queue.cancel_all()
+ logger.info(f"Cancelled {len(cancelled_nodes)} nodes")
# 2. Stop CLI sessions - this kills subprocesses and ensures everything is dead
logger.info("Stopping all CLI sessions...")
@@ -493,7 +455,6 @@ class ClaudeMessageHandler:
# 3. Update UI and persist state for all cancelled nodes
for node in cancelled_nodes:
- # Fire and forget UI update
self.platform.fire_and_forget(
self.platform.queue_edit_message(
node.incoming.chat_id,
@@ -521,7 +482,7 @@ class ClaudeMessageHandler:
async def _handle_stats_command(self, incoming: IncomingMessage) -> None:
"""Handle /stats command."""
stats = self.cli_manager.get_stats()
- tree_count = len(self.tree_queue._trees)
+ tree_count = self.tree_queue.get_tree_count()
await self.platform.queue_send_message(
incoming.chat_id,
f"📊 **Stats**\n• Active CLI: {stats['active_sessions']}\n• Max CLI: {stats['max_sessions']}\n• Message Trees: {tree_count}",
diff --git a/messaging/limiter.py b/messaging/limiter.py
index 985f4df0..fdbbb449 100644
--- a/messaging/limiter.py
+++ b/messaging/limiter.py
@@ -14,7 +14,7 @@ from aiolimiter import AsyncLimiter
logger = logging.getLogger(__name__)
-class GlobalRateLimiter:
+class MessagingRateLimiter:
"""
A thread-safe global rate limiter for messaging.
@@ -22,16 +22,16 @@ class GlobalRateLimiter:
only the latest version of a message update is processed.
"""
- _instance: Optional["GlobalRateLimiter"] = None
+ _instance: Optional["MessagingRateLimiter"] = None
_lock = asyncio.Lock()
def __new__(cls, *args, **kwargs):
if not cls._instance:
pass
- return super(GlobalRateLimiter, cls).__new__(cls)
+ return super(MessagingRateLimiter, cls).__new__(cls)
@classmethod
- async def get_instance(cls) -> "GlobalRateLimiter":
+ async def get_instance(cls) -> "MessagingRateLimiter":
"""Get the singleton instance of the limiter."""
async with cls._lock:
if cls._instance is None:
@@ -60,12 +60,12 @@ class GlobalRateLimiter:
self._paused_until = 0
logger.info(
- f"GlobalRateLimiter initialized ({rate_limit} req / {rate_window}s with Task Compaction)"
+ f"MessagingRateLimiter initialized ({rate_limit} req / {rate_window}s with Task Compaction)"
)
async def _worker(self):
"""Background worker that processes queued messaging tasks."""
- logger.info("GlobalRateLimiter worker started")
+ logger.info("MessagingRateLimiter worker started")
while True:
try:
# Get a task from the queue
@@ -109,7 +109,7 @@ class GlobalRateLimiter:
parts = error_msg.split("after ")
if len(parts) > 1:
seconds = int(parts[1].split()[0])
- except:
+ except Exception:
pass
logger.error(
@@ -131,15 +131,11 @@ class GlobalRateLimiter:
break
except Exception as e:
logger.error(
- f"GlobalRateLimiter worker critical error: {e}", exc_info=True
+ f"MessagingRateLimiter worker critical error: {e}", exc_info=True
)
await asyncio.sleep(1)
async def _enqueue_internal(self, func, future, dedup_key, front=False):
- async def callback(f):
- # This is just a placeholder to use the same internal logic
- pass
-
await self._enqueue_internal_multi(func, [future], dedup_key, front)
async def _enqueue_internal_multi(self, func, futures, dedup_key, front=False):
diff --git a/messaging/models.py b/messaging/models.py
index d1aa11cb..7ef488e5 100644
--- a/messaging/models.py
+++ b/messaging/models.py
@@ -32,33 +32,3 @@ class IncomingMessage:
return self.reply_to_message_id is not None
-@dataclass
-class OutgoingMessage:
- """
- Platform-agnostic outgoing message.
-
- The handler creates these, adapters convert to platform-specific format.
- """
-
- text: str
- chat_id: str
-
- # Optional fields
- reply_to: Optional[str] = None
- parse_mode: Optional[str] = "markdown"
-
- # For editing existing messages
- edit_message_id: Optional[str] = None
-
-
-@dataclass
-class MessageContext:
- """
- Context for message processing.
-
- Passed to handlers to track state across a conversation.
- """
-
- session_id: Optional[str] = None
- is_new_session: bool = True
- status_message_id: Optional[str] = None
diff --git a/messaging/session.py b/messaging/session.py
index 9e82af31..7294eace 100644
--- a/messaging/session.py
+++ b/messaging/session.py
@@ -281,6 +281,25 @@ class SessionStore:
self._node_to_tree[node_id] = root_id
self._save()
+ def get_all_trees(self) -> Dict[str, dict]:
+ """Get all stored trees (public accessor)."""
+ with self._lock:
+ return dict(self._trees)
+
+ def get_node_mapping(self) -> Dict[str, str]:
+ """Get the node-to-tree mapping (public accessor)."""
+ with self._lock:
+ return dict(self._node_to_tree)
+
+ def sync_from_tree_data(
+ self, trees: Dict[str, dict], node_to_tree: Dict[str, str]
+ ) -> None:
+ """Sync internal tree state from external data and persist."""
+ with self._lock:
+ self._trees = trees
+ self._node_to_tree = node_to_tree
+ self._save()
+
def cleanup_old_trees(self, max_age_days: int = 30) -> int:
"""Remove trees older than max_age_days."""
with self._lock:
diff --git a/messaging/telegram.py b/messaging/telegram.py
index cc409213..3d5365ab 100644
--- a/messaging/telegram.py
+++ b/messaging/telegram.py
@@ -70,7 +70,7 @@ class TelegramPlatform(MessagingPlatform):
Callable[[IncomingMessage], Awaitable[None]]
] = None
self._connected = False
- self._limiter: Optional[Any] = None # Will be GlobalRateLimiter
+ self._limiter: Optional[Any] = None # Will be MessagingRateLimiter
async def start(self) -> None:
"""Initialize and connect to Telegram."""
@@ -124,9 +124,9 @@ class TelegramPlatform(MessagingPlatform):
raise
# Initialize rate limiter
- from .limiter import GlobalRateLimiter
+ from .limiter import MessagingRateLimiter
- self._limiter = await GlobalRateLimiter.get_instance()
+ self._limiter = await MessagingRateLimiter.get_instance()
# Send startup notification
try:
diff --git a/messaging/tree_queue.py b/messaging/tree_queue.py
index af87cb99..8c91c81e 100644
--- a/messaging/tree_queue.py
+++ b/messaging/tree_queue.py
@@ -315,6 +315,10 @@ class TreeQueueManager:
logger.info(f"Cleaned up {count} stale nodes during startup")
return count
+ def get_tree_count(self) -> int:
+ """Get the number of active message trees."""
+ return len(self._repository._trees)
+
def register_node(self, node_id: str, root_id: str) -> None:
"""Register a node ID to a tree (for external mapping)."""
self._repository.register_node(node_id, root_id)
diff --git a/providers/base.py b/providers/base.py
index 550eec94..2b3be8d1 100644
--- a/providers/base.py
+++ b/providers/base.py
@@ -1,17 +1,22 @@
"""Base provider interface - extend this to implement your own provider."""
from abc import ABC, abstractmethod
-from typing import Any, AsyncIterator, Optional
+from typing import Any, AsyncIterator, Dict, Optional
from pydantic import BaseModel
class ProviderConfig(BaseModel):
- """Configuration for a provider."""
+ """Configuration for a provider.
+
+ Base fields apply to all providers. Provider-specific parameters
+ (e.g. NIM temperature, top_p) are passed via extra_params.
+ """
api_key: str
base_url: Optional[str] = None
rate_limit: Optional[int] = None
rate_window: int = 60
+ extra_params: Dict[str, Any] = {}
class BaseProvider(ABC):
diff --git a/providers/logging_utils.py b/providers/logging_utils.py
index 0df59da4..2d87a8aa 100644
--- a/providers/logging_utils.py
+++ b/providers/logging_utils.py
@@ -12,6 +12,20 @@ from typing import Any, Dict, List
logger = logging.getLogger(__name__)
+def _extract_text_from_content(content) -> str:
+ """Extract concatenated text from message content (str or list of content blocks)."""
+ if isinstance(content, str):
+ return content
+ if isinstance(content, list):
+ parts = []
+ for block in content:
+ text = getattr(block, "text", "")
+ if text and isinstance(text, str):
+ parts.append(text)
+ return "".join(parts)
+ return ""
+
+
def generate_request_fingerprint(messages: List[Any]) -> str:
"""Generate unique short hash for message content.
@@ -42,20 +56,10 @@ def get_last_user_message_preview(messages: List[Any], max_len: int = 100) -> st
"""Extract a preview of the last user message."""
for msg in reversed(messages):
if hasattr(msg, "role") and msg.role == "user":
- content = msg.content
- if isinstance(content, str):
- preview = content.replace("\n", " ").replace("\r", "")
+ text = _extract_text_from_content(getattr(msg, "content", ""))
+ if text:
+ preview = text.replace("\n", " ").replace("\r", "")
return preview[:max_len] + "..." if len(preview) > max_len else preview
- elif isinstance(content, list):
- text_parts = []
- for block in content:
- if hasattr(block, "text"):
- text_parts.append(block.text)
- if text_parts:
- preview = " ".join(text_parts).replace("\n", " ")
- return (
- preview[:max_len] + "..." if len(preview) > max_len else preview
- )
return "(no user message)"
diff --git a/providers/nvidia_mixins.py b/providers/nvidia_mixins.py
index cb9530a7..c30e63a7 100644
--- a/providers/nvidia_mixins.py
+++ b/providers/nvidia_mixins.py
@@ -3,7 +3,6 @@
This module contains focused mixins that handle specific aspects of the
NVIDIA NIM provider functionality:
- RequestBuilderMixin: Builds request bodies
-- StreamProcessorMixin: Processes streaming responses
- ErrorMapperMixin: Maps HTTP errors to provider exceptions
- ResponseConverterMixin: Converts responses between formats
"""
@@ -35,25 +34,6 @@ class RequestBuilderMixin:
super().__init__(*args, **kwargs)
self._nim_params: Dict[str, Any] = {}
- def _load_nim_params(self) -> Dict[str, Any]:
- """Load NIM-specific parameters from environment.
-
- Reads NVIDIA_NIM_* environment variables to configure defaults.
-
- Returns:
- Dictionary of NIM-specific parameters
- """
- import os
-
- params: Dict[str, Any] = {}
- if val := os.getenv("NVIDIA_NIM_TEMPERATURE"):
- params["temperature"] = float(val)
- if val := os.getenv("NVIDIA_NIM_TOP_P"):
- params["top_p"] = float(val)
- if val := os.getenv("NVIDIA_NIM_MAX_TOKENS"):
- params["max_tokens"] = int(val)
- return params
-
def _build_request_body(self, request_data: Any, stream: bool = False) -> dict:
"""Build OpenAI-format request body from Anthropic request.
@@ -246,88 +226,3 @@ class ResponseConverterMixin:
}
-class StreamProcessorMixin:
- """Mixin for processing streaming responses from NIM API.
-
- Handles SSE parsing, content block management, tool call processing,
- and error handling during streaming.
- """
-
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
-
- def _parse_sse_event(self, event_data: str) -> Any:
- """Parse a single SSE event, return None if invalid/done.
-
- Args:
- event_data: Raw SSE event data
-
- Returns:
- Parsed JSON data or None for [DONE] or invalid events
- """
- if not event_data.strip():
- return None
-
- for line in event_data.splitlines():
- line = line.strip()
- if line.startswith("data:"):
- data_content = line[5:].lstrip()
- if data_content == "[DONE]":
- return None
- try:
- return json.loads(data_content)
- except json.JSONDecodeError:
- logger.debug(f"JSON decode failed for SSE data: {data_content}")
- return None
- return None
-
- def _process_tool_call(self, tc: dict, sse: Any):
- """Process a single tool call delta and yield SSE events.
-
- Args:
- tc: Tool call delta from OpenAI stream
- sse: SSEBuilder instance for generating events
-
- Yields:
- SSE event strings
- """
- import uuid
-
- tc_index = tc.get("index", 0)
- if tc_index < 0:
- tc_index = len(sse.blocks.tool_indices)
-
- # Update accumulated name if present
- fn_delta = tc.get("function", {})
- if fn_delta.get("name") is not None:
- sse.blocks.tool_names[tc_index] = (
- sse.blocks.tool_names.get(tc_index, "") + fn_delta["name"]
- )
-
- # Check if we should start the tool block
- if tc_index not in sse.blocks.tool_indices:
- name = sse.blocks.tool_names.get(tc_index, "")
- # Only start if name is non-empty or we have an ID (start of tool call)
- if name or tc.get("id"):
- tool_id = tc.get("id") or f"tool_{uuid.uuid4()}"
- yield sse.start_tool_block(tc_index, tool_id, name)
- sse.blocks.tool_started[tc_index] = True
- elif not sse.blocks.tool_started.get(tc_index) and sse.blocks.tool_names.get(
- tc_index
- ):
- # Block index exists (due to ID in previous chunk) but not started due to empty name
- tool_id = f"tool_{uuid.uuid4()}" # Should ideally reuse ID if we saved it
- name = sse.blocks.tool_names[tc_index]
- yield sse.start_tool_block(tc_index, tool_id, name)
- sse.blocks.tool_started[tc_index] = True
-
- args = fn_delta.get("arguments", "")
- if args:
- # Ensure block is started before emitting args (with a fallback name if still empty)
- if not sse.blocks.tool_started.get(tc_index):
- tool_id = tc.get("id") or f"tool_{uuid.uuid4()}"
- name = sse.blocks.tool_names.get(tc_index, "tool_call") or "tool_call"
- yield sse.start_tool_block(tc_index, tool_id, name)
- sse.blocks.tool_started[tc_index] = True
-
- yield sse.emit_tool_delta(tc_index, args)
diff --git a/providers/nvidia_nim.py b/providers/nvidia_nim.py
index 62359212..a72ba45c 100644
--- a/providers/nvidia_nim.py
+++ b/providers/nvidia_nim.py
@@ -1,5 +1,4 @@
import logging
-import os
import json
import uuid
from typing import Any, AsyncIterator
@@ -38,13 +37,13 @@ class NvidiaNimProvider(
def __init__(self, config: ProviderConfig):
super().__init__(config)
- self._api_key = config.api_key or os.getenv("NVIDIA_NIM_API_KEY", "")
- self._base_url = (
- config.base_url
- or os.getenv("NVIDIA_NIM_BASE_URL", "https://integrate.api.nvidia.com/v1")
- ).rstrip("/")
- self._nim_params = self._load_nim_params()
- self._global_rate_limiter = GlobalRateLimiter.get_instance()
+ self._api_key = config.api_key
+ self._base_url = (config.base_url or "https://integrate.api.nvidia.com/v1").rstrip("/")
+ self._nim_params = config.extra_params.copy() if config.extra_params else {}
+ self._global_rate_limiter = GlobalRateLimiter.get_instance(
+ rate_limit=config.rate_limit,
+ rate_window=config.rate_window,
+ )
self._client = AsyncOpenAI(
api_key=self._api_key,
base_url=self._base_url,
@@ -249,7 +248,6 @@ class NvidiaNimProvider(
tc: Tool call delta info dict
sse: SSEBuilder instance
"""
- import uuid
tc_index = tc.get("index", 0)
if tc_index < 0:
diff --git a/providers/rate_limit.py b/providers/rate_limit.py
index d2d28d15..d2bfeec2 100644
--- a/providers/rate_limit.py
+++ b/providers/rate_limit.py
@@ -3,7 +3,6 @@
import asyncio
import time
import logging
-import os
from typing import Optional
from aiolimiter import AsyncLimiter
@@ -22,14 +21,11 @@ class GlobalRateLimiter:
_instance: Optional["GlobalRateLimiter"] = None
- def __init__(self):
+ def __init__(self, rate_limit: int = 40, rate_window: float = 60.0):
# Prevent double initialization in singleton
if hasattr(self, "_initialized"):
return
- rate_limit = int(os.getenv("NVIDIA_NIM_RATE_LIMIT", "40"))
- rate_window = float(os.getenv("NVIDIA_NIM_RATE_WINDOW", "60.0"))
-
self.limiter = AsyncLimiter(rate_limit, rate_window)
self._blocked_until: float = 0
self._lock = asyncio.Lock()
@@ -40,10 +36,22 @@ class GlobalRateLimiter:
)
@classmethod
- def get_instance(cls) -> "GlobalRateLimiter":
- """Get or create the singleton instance."""
+ def get_instance(
+ cls,
+ rate_limit: Optional[int] = None,
+ rate_window: Optional[float] = None,
+ ) -> "GlobalRateLimiter":
+ """Get or create the singleton instance.
+
+ Args:
+ rate_limit: Requests per window (only used on first creation)
+ rate_window: Window in seconds (only used on first creation)
+ """
if cls._instance is None:
- cls._instance = cls()
+ cls._instance = cls(
+ rate_limit=rate_limit or 40,
+ rate_window=rate_window or 60.0,
+ )
return cls._instance
@classmethod
diff --git a/server.py b/server.py
index 306ec338..0e72e2d3 100644
--- a/server.py
+++ b/server.py
@@ -11,5 +11,7 @@ __all__ = ["app", "create_app"]
if __name__ == "__main__":
import uvicorn
+ from config.settings import get_settings
- uvicorn.run(app, host="0.0.0.0", port=8082, log_level="debug")
+ settings = get_settings()
+ uvicorn.run(app, host=settings.host, port=settings.port, log_level="debug")
diff --git a/tests/test_dependencies.py b/tests/test_dependencies.py
index ecb8af8a..93178547 100644
--- a/tests/test_dependencies.py
+++ b/tests/test_dependencies.py
@@ -4,6 +4,29 @@ from api.dependencies import get_provider, get_settings, cleanup_provider
from providers.nvidia_nim import NvidiaNimProvider
+def _make_mock_settings(**overrides):
+ """Create a mock settings object with all required fields for get_provider()."""
+ mock = MagicMock()
+ mock.provider_type = "nvidia_nim"
+ mock.nvidia_nim_api_key = "test_key"
+ mock.nvidia_nim_base_url = None
+ mock.nvidia_nim_rate_limit = 40
+ mock.nvidia_nim_rate_window = 60
+ mock.nvidia_nim_temperature = 0.6
+ mock.nvidia_nim_top_p = 0.95
+ mock.nvidia_nim_max_tokens = 16000
+ mock.nvidia_nim_top_k = -1
+ mock.nvidia_nim_presence_penalty = 0.0
+ mock.nvidia_nim_frequency_penalty = 0.0
+ mock.nvidia_nim_min_p = 0.0
+ mock.nvidia_nim_repetition_penalty = 1.0
+ mock.nvidia_nim_seed = None
+ mock.nvidia_nim_stop = None
+ for key, value in overrides.items():
+ setattr(mock, key, value)
+ return mock
+
+
@pytest.fixture(autouse=True)
def reset_provider():
"""Reset the global _provider singleton between tests."""
@@ -14,8 +37,7 @@ def reset_provider():
@pytest.mark.asyncio
async def test_get_provider_singleton():
with patch("api.dependencies.get_settings") as mock_settings:
- mock_settings.return_value = MagicMock()
- mock_settings.return_value.nvidia_nim_api_key = "test_key"
+ mock_settings.return_value = _make_mock_settings()
p1 = get_provider()
p2 = get_provider()
@@ -37,8 +59,7 @@ async def test_get_settings():
@pytest.mark.asyncio
async def test_cleanup_provider():
with patch("api.dependencies.get_settings") as mock_settings:
- mock_settings.return_value = MagicMock()
- mock_settings.return_value.nvidia_nim_api_key = "test_key"
+ mock_settings.return_value = _make_mock_settings()
provider = get_provider()
provider._client = AsyncMock()
@@ -46,16 +67,12 @@ async def test_cleanup_provider():
await cleanup_provider()
provider._client.aclose.assert_called_once()
- # The singleton should be None now, but since we patched the local _provider
- # we need to be careful how we verify.
- # Actually within the same test session without the patch, it would be None.
@pytest.mark.asyncio
async def test_cleanup_provider_no_client():
with patch("api.dependencies.get_settings") as mock_settings:
- mock_settings.return_value = MagicMock()
- mock_settings.return_value.nvidia_nim_api_key = "test_key"
+ mock_settings.return_value = _make_mock_settings()
provider = get_provider()
if hasattr(provider, "_client"):
@@ -63,3 +80,13 @@ async def test_cleanup_provider_no_client():
await cleanup_provider()
# Should not raise
+
+
+async def test_get_provider_unknown_type():
+ """Test that unknown provider_type raises ValueError."""
+ with patch("api.dependencies.get_settings") as mock_settings:
+ mock_settings.return_value = _make_mock_settings(provider_type="unknown")
+
+ with pytest.raises(ValueError, match="Unknown provider_type"):
+ get_provider()
diff --git a/tests/test_extract_text.py b/tests/test_extract_text.py
new file mode 100644
index 00000000..40092b3c
--- /dev/null
+++ b/tests/test_extract_text.py
@@ -0,0 +1,92 @@
+"""Tests for extract_text_from_content helper functions."""
+
+from unittest.mock import MagicMock
+
+from api.request_utils import extract_text_from_content
+from providers.logging_utils import _extract_text_from_content as logging_extract
+
+
+class TestExtractTextFromContent:
+ """Tests for api.request_utils.extract_text_from_content."""
+
+ def test_string_content(self):
+ """Return string content as-is."""
+ assert extract_text_from_content("hello world") == "hello world"
+
+ def test_empty_string(self):
+ """Return empty string for empty string input."""
+ assert extract_text_from_content("") == ""
+
+ def test_list_single_block(self):
+ """Extract text from a single content block."""
+ block = MagicMock()
+ block.text = "some text"
+ assert extract_text_from_content([block]) == "some text"
+
+ def test_list_multiple_blocks(self):
+ """Concatenate text from multiple content blocks."""
+ b1 = MagicMock()
+ b1.text = "hello "
+ b2 = MagicMock()
+ b2.text = "world"
+ assert extract_text_from_content([b1, b2]) == "hello world"
+
+ def test_list_with_non_text_block(self):
+ """Skip blocks without text attribute."""
+ b1 = MagicMock()
+ b1.text = "hello"
+ b2 = MagicMock(spec=[]) # No attributes
+ assert extract_text_from_content([b1, b2]) == "hello"
+
+ def test_list_with_empty_text(self):
+ """Skip blocks with empty text."""
+ b1 = MagicMock()
+ b1.text = ""
+ b2 = MagicMock()
+ b2.text = "world"
+ assert extract_text_from_content([b1, b2]) == "world"
+
+ def test_list_with_none_text(self):
+ """Skip blocks with None text."""
+ b1 = MagicMock()
+ b1.text = None
+ b2 = MagicMock()
+ b2.text = "world"
+ assert extract_text_from_content([b1, b2]) == "world"
+
+ def test_empty_list(self):
+ """Return empty string for empty list."""
+ assert extract_text_from_content([]) == ""
+
+ def test_non_string_non_list(self):
+ """Return empty string for unexpected types."""
+ assert extract_text_from_content(None) == ""
+ assert extract_text_from_content(42) == ""
+
+ def test_list_with_non_string_text_attr(self):
+ """Skip blocks where text is not a string."""
+ b1 = MagicMock()
+ b1.text = 123 # Not a string
+ b2 = MagicMock()
+ b2.text = "valid"
+ assert extract_text_from_content([b1, b2]) == "valid"
+
+
+class TestLoggingExtractText:
+ """Tests for providers.logging_utils._extract_text_from_content.
+
+ Verifies the local helper in logging_utils behaves the same.
+ """
+
+ def test_string_content(self):
+ assert logging_extract("hello") == "hello"
+
+ def test_list_content(self):
+ b = MagicMock()
+ b.text = "test"
+ assert logging_extract([b]) == "test"
+
+ def test_empty_inputs(self):
+ assert logging_extract("") == ""
+ assert logging_extract([]) == ""
+ assert logging_extract(None) == ""
diff --git a/tests/test_limiter.py b/tests/test_limiter.py
index 71679a1b..4746a0ff 100644
--- a/tests/test_limiter.py
+++ b/tests/test_limiter.py
@@ -9,36 +9,36 @@ import logging
os.environ["MESSAGING_RATE_LIMIT"] = "1"
os.environ["MESSAGING_RATE_WINDOW"] = "0.5"
-from messaging.limiter import GlobalRateLimiter
+from messaging.limiter import MessagingRateLimiter
# Configure logging for tests
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
-class TestGlobalRateLimiter:
- """Tests for GlobalRateLimiter."""
+class TestMessagingRateLimiter:
+ """Tests for MessagingRateLimiter."""
@pytest_asyncio.fixture(autouse=True)
async def reset_limiter(self):
"""Reset singleton and environment before each test."""
# Reset the singleton manually if needed (although __init__ protection exists)
- if GlobalRateLimiter._instance:
+ if MessagingRateLimiter._instance:
# Stop worker if possible or just reset instance
pass
- GlobalRateLimiter._instance = None
+ MessagingRateLimiter._instance = None
os.environ["MESSAGING_RATE_LIMIT"] = "1"
os.environ["MESSAGING_RATE_WINDOW"] = "0.5"
yield
- GlobalRateLimiter._instance = None
+ MessagingRateLimiter._instance = None
@pytest.mark.asyncio
async def test_singleton_pattern(self):
"""Test that get_instance returns the same object."""
- limiter1 = await GlobalRateLimiter.get_instance()
- limiter2 = await GlobalRateLimiter.get_instance()
+ limiter1 = await MessagingRateLimiter.get_instance()
+ limiter2 = await MessagingRateLimiter.get_instance()
assert limiter1 is limiter2
@pytest.mark.asyncio
@@ -52,8 +52,8 @@ class TestGlobalRateLimiter:
os.environ["MESSAGING_RATE_WINDOW"] = "1.0"
# Must reset instance to pick up new env vars
- GlobalRateLimiter._instance = None
- limiter = await GlobalRateLimiter.get_instance()
+ MessagingRateLimiter._instance = None
+ limiter = await MessagingRateLimiter.get_instance()
call_counts = {}
@@ -85,8 +85,8 @@ class TestGlobalRateLimiter:
"""
os.environ["MESSAGING_RATE_LIMIT"] = "1"
os.environ["MESSAGING_RATE_WINDOW"] = "0.5"
- GlobalRateLimiter._instance = None
- limiter = await GlobalRateLimiter.get_instance()
+ MessagingRateLimiter._instance = None
+ limiter = await MessagingRateLimiter.get_instance()
call_counts = {}
msg_id = "test_msg_hang"
@@ -121,8 +121,8 @@ class TestGlobalRateLimiter:
@pytest.mark.asyncio
async def test_flood_wait_handling(self):
"""Test that FloodWait exceptions pause the worker."""
- GlobalRateLimiter._instance = None
- limiter = await GlobalRateLimiter.get_instance()
+ MessagingRateLimiter._instance = None
+ limiter = await MessagingRateLimiter.get_instance()
# Mock exception with .seconds attribute
class FloodWait(Exception):
diff --git a/tests/test_messaging.py b/tests/test_messaging.py
index 8cd87115..f83a81db 100644
--- a/tests/test_messaging.py
+++ b/tests/test_messaging.py
@@ -43,26 +43,6 @@ class TestMessagingModels:
assert msg.is_reply() is True
assert msg.reply_to_message_id == "100"
- def test_outgoing_message_creation(self):
- """Test OutgoingMessage dataclass."""
- from messaging.models import OutgoingMessage
-
- msg = OutgoingMessage(
- text="Response",
- chat_id="123",
- parse_mode="markdown",
- )
- assert msg.text == "Response"
- assert msg.parse_mode == "markdown"
- assert msg.edit_message_id is None
-
- def test_message_context(self):
- """Test MessageContext dataclass."""
- from messaging.models import MessageContext
-
- ctx = MessageContext(session_id="sess123", is_new_session=False)
- assert ctx.session_id == "sess123"
- assert ctx.is_new_session is False
class TestMessagingBase:
diff --git a/tests/test_messaging_factory.py b/tests/test_messaging_factory.py
new file mode 100644
index 00000000..31c2eea7
--- /dev/null
+++ b/tests/test_messaging_factory.py
@@ -0,0 +1,43 @@
+"""Tests for messaging platform factory."""
+
+import pytest
+from unittest.mock import patch, MagicMock
+
+from messaging.factory import create_messaging_platform
+
+
+class TestCreateMessagingPlatform:
+ """Tests for create_messaging_platform factory function."""
+
+ def test_telegram_with_token(self):
+ """Create Telegram platform when bot_token is provided."""
+ mock_platform = MagicMock()
+ with patch("messaging.telegram.TELEGRAM_AVAILABLE", True):
+ with patch("messaging.telegram.TelegramPlatform", return_value=mock_platform):
+ result = create_messaging_platform(
+ "telegram",
+ bot_token="test_token",
+ allowed_user_id="12345",
+ )
+
+ assert result is mock_platform
+
+ def test_telegram_without_token(self):
+ """Return None when no bot_token for Telegram."""
+ result = create_messaging_platform("telegram")
+ assert result is None
+
+ def test_telegram_empty_token(self):
+ """Return None when bot_token is empty string."""
+ result = create_messaging_platform("telegram", bot_token="")
+ assert result is None
+
+ def test_unknown_platform(self):
+ """Return None for unknown platform types."""
+ result = create_messaging_platform("discord")
+ assert result is None
+
+ def test_unknown_platform_with_kwargs(self):
+ """Return None for unknown platform even with kwargs."""
+ result = create_messaging_platform("slack", bot_token="token")
+ assert result is None
diff --git a/tests/test_provider_rate_limit.py b/tests/test_provider_rate_limit.py
index f5a32795..a786013b 100644
--- a/tests/test_provider_rate_limit.py
+++ b/tests/test_provider_rate_limit.py
@@ -17,11 +17,8 @@ class TestProviderRateLimiter:
@pytest_asyncio.fixture(autouse=True)
async def reset_limiter(self):
- """Reset singleton and environment before each test."""
+ """Reset singleton before each test."""
GlobalRateLimiter.reset_instance()
- # Defaults
- os.environ["NVIDIA_NIM_RATE_LIMIT"] = "40"
- os.environ["NVIDIA_NIM_RATE_WINDOW"] = "60.0"
yield
GlobalRateLimiter.reset_instance()
@@ -31,13 +28,9 @@ class TestProviderRateLimiter:
Test proactive throttling using aiolimiter.
Logic ported from verify_provider_limiter.py
"""
- # Set limit: 1 request per 0.25 second
- os.environ["NVIDIA_NIM_RATE_LIMIT"] = "1"
- os.environ["NVIDIA_NIM_RATE_WINDOW"] = "0.25"
-
- # Re-init with new limits
+ # Re-init with tight limits: 1 request per 0.25 second
GlobalRateLimiter.reset_instance()
- limiter = GlobalRateLimiter.get_instance()
+ limiter = GlobalRateLimiter.get_instance(rate_limit=1, rate_window=0.25)
start_time = time.time()
diff --git a/tests/test_telegram.py b/tests/test_telegram.py
index c565fa0c..025e63b1 100644
--- a/tests/test_telegram.py
+++ b/tests/test_telegram.py
@@ -26,8 +26,8 @@ async def test_telegram_platform_start_success(telegram_platform):
mock_builder.return_value.token.return_value.request.return_value.build.return_value = mock_app
- # Mock GlobalRateLimiter
- with patch("messaging.limiter.GlobalRateLimiter.get_instance", AsyncMock()):
+ # Mock MessagingRateLimiter
+ with patch("messaging.limiter.MessagingRateLimiter.get_instance", AsyncMock()):
await telegram_platform.start()
assert telegram_platform._connected is True