Gate chat interaction on Twitch live status

This commit is contained in:
2026-05-12 18:49:28 -05:00
parent e3b0fc5be8
commit b249f82631
5 changed files with 255 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
"""Agent Orchestrator - Routes messages and manages agent modes."""
import logging
from collections.abc import Awaitable, Callable
from datetime import datetime, timedelta
from app.agent.policies import (
@@ -54,9 +55,27 @@ class AgentOrchestrator:
# Track active sessions
self.active_sessions: dict[str, dict] = {}
self.chat_interaction_gate: Callable[[str], Awaitable[bool]] | None = None
logger.info("AgentOrchestrator initialized with all modes and policies")
def set_chat_interaction_gate(
self,
gate: Callable[[str], Awaitable[bool]] | None,
) -> None:
"""Set an async gate that must pass before the agent can post to chat."""
self.chat_interaction_gate = gate
async def can_interact_with_chat(self, channel_name: str) -> bool:
"""Return whether outbound chat interaction is currently allowed."""
if not self.chat_interaction_gate:
return True
try:
return await self.chat_interaction_gate(channel_name)
except Exception as e:
logger.warning("Chat interaction gate failed closed: %s", e)
return False
async def start_session(self, channel_name: str) -> str:
"""
Start a new stream session.
@@ -287,6 +306,13 @@ class AgentOrchestrator:
return {"sent": False, "reason": "session_not_found"}
channel_name = session_info["channel_name"]
if not await self.can_interact_with_chat(channel_name):
logger.info(
"Agent response suppressed because stream is not live. Session: %s",
session_id,
)
return {"sent": False, "reason": "stream_not_live"}
sent = await send_chat_message(channel_name=channel_name, message=message)
bot_username = settings.TWITCH_BOT_USERNAME or "sanctum_chronicler"
@@ -495,6 +521,9 @@ class AgentOrchestrator:
if not self.chat_activity.should_hearthkeeper_prompt(session_id):
return None
if not await self.can_interact_with_chat(session_info["channel_name"]):
return None
last_activity_at = self.chat_activity.last_activity_at(session_id)
last_prompt_at = session_info.get("last_hearthkeeper_prompt_at")
if last_activity_at and last_prompt_at and last_activity_at > last_prompt_at: