chat history with the NPC modal
This commit is contained in:
@@ -12,6 +12,7 @@ import structlog
|
||||
import yaml
|
||||
import os
|
||||
from pathlib import Path
|
||||
from datetime import datetime, timezone
|
||||
|
||||
|
||||
logger = structlog.get_logger(__name__)
|
||||
@@ -61,6 +62,34 @@ def create_app():
|
||||
app.register_blueprint(character_bp)
|
||||
app.register_blueprint(game_bp)
|
||||
|
||||
# Register Jinja filters
|
||||
def format_timestamp(iso_string: str) -> str:
|
||||
"""Convert ISO timestamp to relative time (e.g., '2 mins ago')"""
|
||||
if not iso_string:
|
||||
return ""
|
||||
try:
|
||||
timestamp = datetime.fromisoformat(iso_string.replace('Z', '+00:00'))
|
||||
now = datetime.now(timezone.utc)
|
||||
diff = now - timestamp
|
||||
|
||||
seconds = diff.total_seconds()
|
||||
if seconds < 60:
|
||||
return "Just now"
|
||||
elif seconds < 3600:
|
||||
mins = int(seconds / 60)
|
||||
return f"{mins} min{'s' if mins != 1 else ''} ago"
|
||||
elif seconds < 86400:
|
||||
hours = int(seconds / 3600)
|
||||
return f"{hours} hr{'s' if hours != 1 else ''} ago"
|
||||
else:
|
||||
days = int(seconds / 86400)
|
||||
return f"{days} day{'s' if days != 1 else ''} ago"
|
||||
except Exception as e:
|
||||
logger.warning("timestamp_format_failed", iso_string=iso_string, error=str(e))
|
||||
return iso_string
|
||||
|
||||
app.jinja_env.filters['format_timestamp'] = format_timestamp
|
||||
|
||||
# Register dev blueprint only in development
|
||||
env = os.getenv("FLASK_ENV", "development")
|
||||
if env == "development":
|
||||
|
||||
@@ -748,6 +748,40 @@ def npc_chat_modal(session_id: str, npc_id: str):
|
||||
'''
|
||||
|
||||
|
||||
@game_bp.route('/session/<session_id>/npc/<npc_id>/history')
|
||||
@require_auth
|
||||
def npc_chat_history(session_id: str, npc_id: str):
|
||||
"""Get last 5 chat messages for history sidebar."""
|
||||
client = get_api_client()
|
||||
|
||||
try:
|
||||
# Get session to find character_id
|
||||
session_response = client.get(f'/api/v1/sessions/{session_id}')
|
||||
session_data = session_response.get('result', {})
|
||||
character_id = session_data.get('character_id')
|
||||
|
||||
# Fetch last 5 messages from chat service
|
||||
# API endpoint: GET /api/v1/characters/{character_id}/chats/{npc_id}?limit=5
|
||||
history_response = client.get(
|
||||
f'/api/v1/characters/{character_id}/chats/{npc_id}',
|
||||
params={'limit': 5, 'offset': 0}
|
||||
)
|
||||
result_data = history_response.get('result', {})
|
||||
messages = result_data.get('messages', []) # Extract messages array from result
|
||||
|
||||
# Render history partial
|
||||
return render_template(
|
||||
'game/partials/npc_chat_history.html',
|
||||
messages=messages,
|
||||
session_id=session_id,
|
||||
npc_id=npc_id
|
||||
)
|
||||
|
||||
except APIError as e:
|
||||
logger.error("failed_to_load_chat_history", session_id=session_id, npc_id=npc_id, error=str(e))
|
||||
return '<div class="history-empty">Failed to load history</div>', 500
|
||||
|
||||
|
||||
@game_bp.route('/session/<session_id>/npc/<npc_id>/talk', methods=['POST'])
|
||||
@require_auth
|
||||
def talk_to_npc(session_id: str, npc_id: str):
|
||||
|
||||
Reference in New Issue
Block a user