debug: add logging and partial name matching for med context lookup

This commit is contained in:
2026-02-16 12:30:25 -06:00
parent a53187d6a9
commit 7cf0681deb

View File

@@ -11,14 +11,18 @@ import ai.parser as ai_parser
async def _get_scheduled_time_from_context(message, med_name):
"""Fetch recent messages and extract scheduled time from medication reminder.
Looks for bot messages in the last 5 messages that match the pattern:
Looks for bot messages in the last 10 messages that match the pattern:
"Time to take {med_name} (...) · HH:MM"
Returns the scheduled time string (e.g., "12:00") or None if not found.
"""
try:
# Get last 5 messages from channel history
async for msg in message.channel.history(limit=5):
print(
f"[DEBUG] Looking for reminder for '{med_name}' in channel history",
flush=True,
)
# Get last 10 messages from channel history (increased from 5)
async for msg in message.channel.history(limit=10):
# Skip the current message
if msg.id == message.id:
continue
@@ -28,17 +32,40 @@ async def _get_scheduled_time_from_context(message, med_name):
continue
content = msg.content
print(f"[DEBUG] Checking bot message: {content[:100]}...", flush=True)
# Look for reminder pattern: "Time to take {med_name} (...) · HH:MM"
# Use case-insensitive match for medication name
# First try exact match
pattern = rf"Time to take {re.escape(med_name)}.*?·\s*(\d{{1,2}}:\d{{2}})"
match = re.search(pattern, content, re.IGNORECASE)
if match:
return match.group(1)
scheduled_time = match.group(1)
print(f"[DEBUG] Found scheduled time: {scheduled_time}", flush=True)
return scheduled_time
except Exception:
pass
# If no exact match, try to extract any medication name from reminder
# Pattern: "Time to take (name) (dosage) · time"
general_pattern = r"Time to take\s+(\w+)\s+\(.*?\)\s+·\s*(\d{1,2}:\d{2})"
general_match = re.search(general_pattern, content, re.IGNORECASE)
if general_match:
reminder_med_name = general_match.group(1)
# Check if the names match (case insensitive, or one contains the other)
if (
med_name.lower() in reminder_med_name.lower()
or reminder_med_name.lower() in med_name.lower()
):
scheduled_time = general_match.group(2)
print(
f"[DEBUG] Found scheduled time via partial match: {scheduled_time}",
flush=True,
)
return scheduled_time
print(f"[DEBUG] No reminder found for '{med_name}'", flush=True)
except Exception as e:
print(f"[DEBUG] Error in _get_scheduled_time_from_context: {e}", flush=True)
return None