Add Hearthkeeper preview endpoint
This commit is contained in:
@@ -398,6 +398,38 @@ class AgentOrchestrator:
|
|||||||
"third_tick_after_interval": third_tick,
|
"third_tick_after_interval": third_tick,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async def preview_hearthkeeper_prompt(self, session_id: str) -> dict:
|
||||||
|
"""Generate a Hearthkeeper prompt preview without sending or recording it."""
|
||||||
|
session_info = self.active_sessions.get(session_id)
|
||||||
|
if not session_info:
|
||||||
|
return {"generated": False, "reason": "session_not_found"}
|
||||||
|
|
||||||
|
recent_discussion_messages = []
|
||||||
|
async for db_session in get_session():
|
||||||
|
repo = Repository(db_session)
|
||||||
|
recent_discussion_messages = await repo.get_recent_human_messages(
|
||||||
|
session_id=session_id,
|
||||||
|
limit=5,
|
||||||
|
)
|
||||||
|
|
||||||
|
recent_discussion = [
|
||||||
|
message.content for message in recent_discussion_messages[:5]
|
||||||
|
]
|
||||||
|
agent_response = await self.hearthkeeper.generate_prompt(
|
||||||
|
theme=session_info.get("theme"),
|
||||||
|
dashboard=session_info.get("dashboard"),
|
||||||
|
recent_discussion=recent_discussion,
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"generated": True,
|
||||||
|
"session_id": session_id,
|
||||||
|
"agent_response": agent_response,
|
||||||
|
"theme": session_info.get("theme"),
|
||||||
|
"dashboard": session_info.get("dashboard"),
|
||||||
|
"recent_discussion": recent_discussion,
|
||||||
|
}
|
||||||
|
|
||||||
async def _count_response_actions(self, session_id: str, mode: str) -> int:
|
async def _count_response_actions(self, session_id: str, mode: str) -> int:
|
||||||
"""Count response actions for a mode in a session."""
|
"""Count response actions for a mode in a session."""
|
||||||
async for db_session in get_session():
|
async for db_session in get_session():
|
||||||
|
|||||||
32
app/main.py
32
app/main.py
@@ -302,6 +302,38 @@ async def test_loop_inactivity(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/admin/hearthkeeper/preview", dependencies=[Depends(require_admin)])
|
||||||
|
async def preview_hearthkeeper_prompt(session_id: str | None = Form(None)) -> dict:
|
||||||
|
"""Generate a Hearthkeeper prompt preview without posting to Twitch."""
|
||||||
|
if not orchestrator:
|
||||||
|
raise HTTPException(status_code=503, detail="Orchestrator not initialized")
|
||||||
|
|
||||||
|
resolved_session_id = session_id or get_current_stream_session_id()
|
||||||
|
if not resolved_session_id:
|
||||||
|
raise HTTPException(status_code=404, detail="No active stream session found")
|
||||||
|
|
||||||
|
result = await orchestrator.preview_hearthkeeper_prompt(resolved_session_id)
|
||||||
|
if result.get("reason") == "session_not_found":
|
||||||
|
raise HTTPException(status_code=404, detail="Active session not found")
|
||||||
|
|
||||||
|
live_status = None
|
||||||
|
session_info = orchestrator.active_sessions.get(resolved_session_id)
|
||||||
|
if twitch_live_status and session_info:
|
||||||
|
live_status = (
|
||||||
|
await twitch_live_status.get_status(session_info["channel_name"])
|
||||||
|
).to_dict()
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "preview_generated" if result.get("generated") else "failed",
|
||||||
|
"preview": result,
|
||||||
|
"would_post_now": await orchestrator.can_interact_with_chat(
|
||||||
|
session_info["channel_name"]
|
||||||
|
) if session_info else False,
|
||||||
|
"live_status": live_status,
|
||||||
|
"timestamp": datetime.utcnow().isoformat(),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def serialize_dashboard(dashboard) -> dict:
|
def serialize_dashboard(dashboard) -> dict:
|
||||||
"""Serialize a dashboard database model into an API response."""
|
"""Serialize a dashboard database model into an API response."""
|
||||||
return Repository.serialize_dashboard(dashboard)
|
return Repository.serialize_dashboard(dashboard)
|
||||||
|
|||||||
Reference in New Issue
Block a user