fixes to make quest tracking work better, also quest rejection in via the converation with the NPC

This commit is contained in:
2025-11-29 17:51:53 -06:00
parent df26abd207
commit 72cf92021e
11 changed files with 885 additions and 22 deletions

View File

@@ -26,6 +26,7 @@ Usage:
"""
import json
import re
import uuid
from datetime import datetime, timezone
from enum import Enum
@@ -712,7 +713,9 @@ def _process_npc_dialogue_task(
npc_relationship=context.get('npc_relationship'),
previous_dialogue=context.get('previous_dialogue'),
npc_knowledge=context.get('npc_knowledge'),
quest_offering_context=context.get('quest_offering_context')
quest_offering_context=context.get('quest_offering_context'),
quest_ineligibility_context=context.get('quest_ineligibility_context'),
player_asking_for_quests=context.get('player_asking_for_quests', False)
)
# Get NPC info for result
@@ -723,8 +726,44 @@ def _process_npc_dialogue_task(
# Get previous dialogue for display (before adding new exchange)
previous_dialogue = context.get('previous_dialogue', [])
# Parse for quest offer marker and extract structured data for UI
dialogue_text = response.narrative
quest_offer_data = None
quest_offer_match = re.search(r'\[QUEST_OFFER:([^\]]+)\]', dialogue_text)
if quest_offer_match:
offered_quest_id = quest_offer_match.group(1).strip()
# Remove the marker from displayed dialogue (clean up whitespace around it)
dialogue_text = re.sub(r'\s*\[QUEST_OFFER:[^\]]+\]\s*', ' ', dialogue_text).strip()
# Get quest details from the offering context for UI display
quest_ctx = context.get('quest_offering_context')
if quest_ctx:
quest_offer_data = {
'quest_id': offered_quest_id,
'quest_name': quest_ctx.get('quest_name', 'Unknown Quest'),
'quest_description': quest_ctx.get('quest_description', ''),
'rewards': quest_ctx.get('rewards', {}),
'npc_id': npc_id,
}
logger.info(
"Quest offer detected in NPC dialogue",
quest_id=offered_quest_id,
quest_name=quest_ctx.get('quest_name'),
npc_id=npc_id,
character_id=character_id
)
else:
# AI mentioned a quest but no context was provided - log warning
logger.warning(
"Quest offer marker found but no quest_offering_context available",
quest_id=offered_quest_id,
npc_id=npc_id
)
result = {
"dialogue": response.narrative,
"dialogue": dialogue_text,
"tokens_used": response.tokens_used,
"model": response.model,
"context_type": response.context_type,
@@ -734,6 +773,7 @@ def _process_npc_dialogue_task(
"character_name": character_name,
"player_line": context['conversation_topic'],
"conversation_history": previous_dialogue, # History before this exchange
"quest_offer": quest_offer_data, # Structured quest offer for UI (None if no quest offered)
}
# Save dialogue exchange to chat_messages collection and update character's recent_messages cache
@@ -743,15 +783,16 @@ def _process_npc_dialogue_task(
location_id = context.get('game_state', {}).get('current_location')
# Save to chat_messages collection (also updates character's recent_messages)
# Note: Save the cleaned dialogue_text (without quest markers) for display
chat_service = get_chat_message_service()
chat_service.save_dialogue_exchange(
character_id=character_id,
user_id=user_id,
npc_id=npc_id,
player_message=context['conversation_topic'],
npc_response=response.narrative,
npc_response=dialogue_text, # Use cleaned text without quest markers
context=MessageContext.DIALOGUE, # Default context, can be enhanced based on quest/shop interactions
metadata={}, # Can add quest_id, item_id, etc. when those systems are implemented
metadata={'quest_offer_id': quest_offer_data.get('quest_id') if quest_offer_data else None},
session_id=session_id,
location_id=location_id
)