Spaces:
Running
Running
feat: enhance time parsing for log entries, introduce a health pattern checking tool, and adjust wakeword detection test parameters.
Browse files- frontend/src/components/ComponentOverlay.tsx +1 -1
- frontend/src/registry/MedStatus.tsx +31 -33
- src/reachy_mini_conversation_app/appointment_export.py +73 -0
- src/reachy_mini_conversation_app/database.py +14 -8
- src/reachy_mini_conversation_app/entry_state.py +91 -8
- src/reachy_mini_conversation_app/langgraph_agent/nodes/report_builder.py +45 -3
- src/reachy_mini_conversation_app/langgraph_agent/nodes/trend_analyzer.py +42 -1
- src/reachy_mini_conversation_app/main.py +1 -0
- src/reachy_mini_conversation_app/memory_graph.py +106 -1
- src/reachy_mini_conversation_app/openai_realtime.py +53 -1
- src/reachy_mini_conversation_app/profiles/_reachy_mini_minder_locked_profile/log_entry.py +128 -3
- src/reachy_mini_conversation_app/profiles/_reachy_mini_minder_locked_profile/tools.txt +1 -0
- src/reachy_mini_conversation_app/tools/check_health_patterns.py +111 -0
- src/reachy_mini_conversation_app/tools/check_medication.py +76 -1
- tests/test_wakeword_and_end_session.py +10 -13
frontend/src/components/ComponentOverlay.tsx
CHANGED
|
@@ -11,7 +11,7 @@ interface ComponentOverlayProps {
|
|
| 11 |
}
|
| 12 |
|
| 13 |
// Components that render full-screen (TV-distance readable)
|
| 14 |
-
const FULLSCREEN_COMPONENTS = new Set(["HeadacheLog", "MedLog", "SessionSummary", "OnboardingProgress", "OnboardingSummary", "MyMedsList"]);
|
| 15 |
|
| 16 |
export function ComponentOverlay({ component, onDismiss }: ComponentOverlayProps) {
|
| 17 |
const isFullscreen = FULLSCREEN_COMPONENTS.has(component.name);
|
|
|
|
| 11 |
}
|
| 12 |
|
| 13 |
// Components that render full-screen (TV-distance readable)
|
| 14 |
+
const FULLSCREEN_COMPONENTS = new Set(["HeadacheLog", "MedLog", "MedStatus", "SessionSummary", "OnboardingProgress", "OnboardingSummary", "MyMedsList", "QuickReply"]);
|
| 15 |
|
| 16 |
export function ComponentOverlay({ component, onDismiss }: ComponentOverlayProps) {
|
| 17 |
const isFullscreen = FULLSCREEN_COMPONENTS.has(component.name);
|
frontend/src/registry/MedStatus.tsx
CHANGED
|
@@ -27,20 +27,20 @@ function StatusIcon({ status }: { status: Medication["status"] }) {
|
|
| 27 |
switch (status) {
|
| 28 |
case "logged":
|
| 29 |
return (
|
| 30 |
-
<div className="w-
|
| 31 |
-
<Check className="w-
|
| 32 |
</div>
|
| 33 |
);
|
| 34 |
case "pending":
|
| 35 |
return (
|
| 36 |
-
<div className="w-
|
| 37 |
-
<Circle className="w-
|
| 38 |
</div>
|
| 39 |
);
|
| 40 |
case "missed_window":
|
| 41 |
return (
|
| 42 |
-
<div className="w-
|
| 43 |
-
<AlertCircle className="w-
|
| 44 |
</div>
|
| 45 |
);
|
| 46 |
}
|
|
@@ -67,29 +67,27 @@ export function MedStatus({
|
|
| 67 |
|
| 68 |
return (
|
| 69 |
<div
|
| 70 |
-
className="relative
|
| 71 |
-
bg-gradient-to-br from-surface-elevated/90 to-surface-subtle/95
|
| 72 |
-
border border-success/15
|
| 73 |
-
shadow-[0_4px_24px_rgba(0,0,0,0.4),inset_0_1px_0_rgba(255,255,255,0.05)]"
|
| 74 |
>
|
| 75 |
{/* Top accent line */}
|
| 76 |
-
<div className="absolute top-0 left-[20%] right-[20%] h-
|
| 77 |
|
| 78 |
{/* Header */}
|
| 79 |
-
<div className="flex items-center gap-
|
| 80 |
<div
|
| 81 |
-
className="relative w-
|
| 82 |
bg-gradient-to-br from-success/25 to-success/10
|
| 83 |
border border-success/30"
|
| 84 |
>
|
| 85 |
-
<Pill className="w-
|
| 86 |
</div>
|
| 87 |
<div className="flex-1">
|
| 88 |
-
<h3 className="text-
|
| 89 |
-
<p className="text-
|
| 90 |
</div>
|
| 91 |
{total > 0 && (
|
| 92 |
-
<span className="text-
|
| 93 |
{logged} of {total}
|
| 94 |
</span>
|
| 95 |
)}
|
|
@@ -97,11 +95,11 @@ export function MedStatus({
|
|
| 97 |
|
| 98 |
{/* Scheduled Medication List */}
|
| 99 |
{medications.length > 0 && (
|
| 100 |
-
<div className="flex flex-col gap-
|
| 101 |
{medications.map((med, index) => (
|
| 102 |
<div
|
| 103 |
key={index}
|
| 104 |
-
className={`flex items-center gap-
|
| 105 |
bg-surface-subtle/60 border transition-all
|
| 106 |
${med.status === "logged"
|
| 107 |
? "border-success/30"
|
|
@@ -112,10 +110,10 @@ export function MedStatus({
|
|
| 112 |
>
|
| 113 |
<StatusIcon status={med.status} />
|
| 114 |
<div className="flex-1 min-w-0">
|
| 115 |
-
<p className="text-
|
| 116 |
{med.name}
|
| 117 |
</p>
|
| 118 |
-
<p className="text-
|
| 119 |
{med.status === "logged" && med.logged_at
|
| 120 |
? `Logged at ${med.logged_at}`
|
| 121 |
: `${med.scheduled_window} — not yet logged`}
|
|
@@ -128,30 +126,30 @@ export function MedStatus({
|
|
| 128 |
|
| 129 |
{/* Ad-Hoc Medications Section */}
|
| 130 |
{hasAdHoc && (
|
| 131 |
-
<div className="mt-
|
| 132 |
-
<div className="flex items-center gap-2 mb-
|
| 133 |
<div className="flex-1 h-px bg-white/5" />
|
| 134 |
-
<span className="text-
|
| 135 |
Other medications logged
|
| 136 |
</span>
|
| 137 |
<div className="flex-1 h-px bg-white/5" />
|
| 138 |
</div>
|
| 139 |
-
<div className="flex flex-col gap-2">
|
| 140 |
{ad_hoc_medications!.map((med, index) => (
|
| 141 |
<div
|
| 142 |
key={`adhoc-${index}`}
|
| 143 |
-
className="flex items-center gap-
|
| 144 |
bg-surface-subtle/40 border border-white/5 transition-all"
|
| 145 |
>
|
| 146 |
-
<div className="w-
|
| 147 |
-
<Plus className="w-
|
| 148 |
</div>
|
| 149 |
<div className="flex-1 min-w-0">
|
| 150 |
-
<p className="text-
|
| 151 |
{med.name}
|
| 152 |
{med.dose && <span className="text-muted ml-1.5">({med.dose})</span>}
|
| 153 |
</p>
|
| 154 |
-
<p className="text-
|
| 155 |
{med.logged_at ? `Logged at ${med.logged_at}` : "Logged today"}
|
| 156 |
{" · "}
|
| 157 |
<span className="text-cta/70">Not in regular medications</span>
|
|
@@ -165,13 +163,13 @@ export function MedStatus({
|
|
| 165 |
|
| 166 |
{/* Robot Message */}
|
| 167 |
{robot_message && (
|
| 168 |
-
<div className="mt-
|
| 169 |
-
<p className="text-
|
| 170 |
</div>
|
| 171 |
)}
|
| 172 |
|
| 173 |
{/* Disclaimer */}
|
| 174 |
-
<p className="mt-
|
| 175 |
This shows what was logged in the app, not verified medication intake.
|
| 176 |
</p>
|
| 177 |
</div>
|
|
|
|
| 27 |
switch (status) {
|
| 28 |
case "logged":
|
| 29 |
return (
|
| 30 |
+
<div className="w-10 h-10 rounded-full flex items-center justify-center bg-success/15 text-success">
|
| 31 |
+
<Check className="w-5 h-5" strokeWidth={3} />
|
| 32 |
</div>
|
| 33 |
);
|
| 34 |
case "pending":
|
| 35 |
return (
|
| 36 |
+
<div className="w-10 h-10 rounded-full flex items-center justify-center bg-warning/15 text-warning">
|
| 37 |
+
<Circle className="w-5 h-5" />
|
| 38 |
</div>
|
| 39 |
);
|
| 40 |
case "missed_window":
|
| 41 |
return (
|
| 42 |
+
<div className="w-10 h-10 rounded-full flex items-center justify-center bg-error/15 text-error">
|
| 43 |
+
<AlertCircle className="w-5 h-5" />
|
| 44 |
</div>
|
| 45 |
);
|
| 46 |
}
|
|
|
|
| 67 |
|
| 68 |
return (
|
| 69 |
<div
|
| 70 |
+
className="relative h-full overflow-y-auto p-10
|
| 71 |
+
bg-gradient-to-br from-surface-elevated/90 to-surface-subtle/95"
|
|
|
|
|
|
|
| 72 |
>
|
| 73 |
{/* Top accent line */}
|
| 74 |
+
<div className="absolute top-0 left-[20%] right-[20%] h-1 bg-gradient-to-r from-transparent via-success to-transparent opacity-60" />
|
| 75 |
|
| 76 |
{/* Header */}
|
| 77 |
+
<div className="flex items-center gap-5 mb-8 pb-6 border-b border-white/5">
|
| 78 |
<div
|
| 79 |
+
className="relative w-14 h-14 rounded-xl flex items-center justify-center
|
| 80 |
bg-gradient-to-br from-success/25 to-success/10
|
| 81 |
border border-success/30"
|
| 82 |
>
|
| 83 |
+
<Pill className="w-7 h-7 text-success" />
|
| 84 |
</div>
|
| 85 |
<div className="flex-1">
|
| 86 |
+
<h3 className="text-3xl font-bold tracking-tight">Today's Medications</h3>
|
| 87 |
+
<p className="text-lg text-muted mt-1">{formatDate(date)}</p>
|
| 88 |
</div>
|
| 89 |
{total > 0 && (
|
| 90 |
+
<span className="text-sm font-extrabold px-3.5 py-1.5 rounded-full bg-success/15 text-success border border-success/20">
|
| 91 |
{logged} of {total}
|
| 92 |
</span>
|
| 93 |
)}
|
|
|
|
| 95 |
|
| 96 |
{/* Scheduled Medication List */}
|
| 97 |
{medications.length > 0 && (
|
| 98 |
+
<div className="flex flex-col gap-3">
|
| 99 |
{medications.map((med, index) => (
|
| 100 |
<div
|
| 101 |
key={index}
|
| 102 |
+
className={`flex items-center gap-4 p-5 rounded-xl
|
| 103 |
bg-surface-subtle/60 border transition-all
|
| 104 |
${med.status === "logged"
|
| 105 |
? "border-success/30"
|
|
|
|
| 110 |
>
|
| 111 |
<StatusIcon status={med.status} />
|
| 112 |
<div className="flex-1 min-w-0">
|
| 113 |
+
<p className="text-xl font-semibold text-primary truncate">
|
| 114 |
{med.name}
|
| 115 |
</p>
|
| 116 |
+
<p className="text-base text-muted mt-1">
|
| 117 |
{med.status === "logged" && med.logged_at
|
| 118 |
? `Logged at ${med.logged_at}`
|
| 119 |
: `${med.scheduled_window} — not yet logged`}
|
|
|
|
| 126 |
|
| 127 |
{/* Ad-Hoc Medications Section */}
|
| 128 |
{hasAdHoc && (
|
| 129 |
+
<div className="mt-6">
|
| 130 |
+
<div className="flex items-center gap-2 mb-3">
|
| 131 |
<div className="flex-1 h-px bg-white/5" />
|
| 132 |
+
<span className="text-sm font-semibold uppercase tracking-wider text-muted">
|
| 133 |
Other medications logged
|
| 134 |
</span>
|
| 135 |
<div className="flex-1 h-px bg-white/5" />
|
| 136 |
</div>
|
| 137 |
+
<div className="flex flex-col gap-2.5">
|
| 138 |
{ad_hoc_medications!.map((med, index) => (
|
| 139 |
<div
|
| 140 |
key={`adhoc-${index}`}
|
| 141 |
+
className="flex items-center gap-4 p-4 rounded-xl
|
| 142 |
bg-surface-subtle/40 border border-white/5 transition-all"
|
| 143 |
>
|
| 144 |
+
<div className="w-9 h-9 rounded-full flex items-center justify-center bg-cta/10 text-cta">
|
| 145 |
+
<Plus className="w-5 h-5" strokeWidth={2.5} />
|
| 146 |
</div>
|
| 147 |
<div className="flex-1 min-w-0">
|
| 148 |
+
<p className="text-lg font-medium text-primary/80 truncate">
|
| 149 |
{med.name}
|
| 150 |
{med.dose && <span className="text-muted ml-1.5">({med.dose})</span>}
|
| 151 |
</p>
|
| 152 |
+
<p className="text-sm text-muted mt-0.5">
|
| 153 |
{med.logged_at ? `Logged at ${med.logged_at}` : "Logged today"}
|
| 154 |
{" · "}
|
| 155 |
<span className="text-cta/70">Not in regular medications</span>
|
|
|
|
| 163 |
|
| 164 |
{/* Robot Message */}
|
| 165 |
{robot_message && (
|
| 166 |
+
<div className="mt-6 px-6 py-4 rounded-xl bg-cta/10 border border-cta/20">
|
| 167 |
+
<p className="text-lg text-cta font-medium">{robot_message}</p>
|
| 168 |
</div>
|
| 169 |
)}
|
| 170 |
|
| 171 |
{/* Disclaimer */}
|
| 172 |
+
<p className="mt-6 text-sm text-muted text-center italic">
|
| 173 |
This shows what was logged in the app, not verified medication intake.
|
| 174 |
</p>
|
| 175 |
</div>
|
src/reachy_mini_conversation_app/appointment_export.py
CHANGED
|
@@ -104,6 +104,11 @@ async def generate_appointment_summary(
|
|
| 104 |
else f'- "{excerpt}"\n'
|
| 105 |
)
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
# Generate summary via LLM
|
| 108 |
try:
|
| 109 |
# PII guard: redact excerpts before sending to cloud LLM
|
|
@@ -155,6 +160,74 @@ async def generate_appointment_summary(
|
|
| 155 |
}
|
| 156 |
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
def _generate_fallback_summary(
|
| 159 |
headaches: List[Dict],
|
| 160 |
medications: List[Dict],
|
|
|
|
| 104 |
else f'- "{excerpt}"\n'
|
| 105 |
)
|
| 106 |
|
| 107 |
+
# Add graph-derived summary if available
|
| 108 |
+
graph_summary = _generate_graph_summary()
|
| 109 |
+
if graph_summary:
|
| 110 |
+
data_summary += f"\n{graph_summary}"
|
| 111 |
+
|
| 112 |
# Generate summary via LLM
|
| 113 |
try:
|
| 114 |
# PII guard: redact excerpts before sending to cloud LLM
|
|
|
|
| 160 |
}
|
| 161 |
|
| 162 |
|
| 163 |
+
def _generate_graph_summary() -> str:
|
| 164 |
+
"""Query Neo4j for appointment-relevant patterns.
|
| 165 |
+
|
| 166 |
+
Returns a formatted text block for inclusion in the LLM prompt.
|
| 167 |
+
Returns empty string if Neo4j is unavailable.
|
| 168 |
+
"""
|
| 169 |
+
try:
|
| 170 |
+
from reachy_mini_conversation_app.session_enrichment import (
|
| 171 |
+
get_session_enrichment,
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
enrichment = get_session_enrichment()
|
| 175 |
+
if not enrichment or not enrichment._graph:
|
| 176 |
+
return ""
|
| 177 |
+
|
| 178 |
+
graph = enrichment._graph
|
| 179 |
+
lines = ["### Knowledge Graph Observations\n"]
|
| 180 |
+
|
| 181 |
+
# Medication adherence from events
|
| 182 |
+
try:
|
| 183 |
+
from reachy_mini_conversation_app.pattern_detector import PatternDetector
|
| 184 |
+
|
| 185 |
+
detector = PatternDetector(graph)
|
| 186 |
+
insights = detector.run_analysis()
|
| 187 |
+
if insights:
|
| 188 |
+
lines.append("**Detected Patterns:**")
|
| 189 |
+
for insight in insights[:5]:
|
| 190 |
+
lines.append(f"- {insight.pattern_type}: {insight.summary}")
|
| 191 |
+
if insight.detail:
|
| 192 |
+
lines.append(f" ({insight.detail})")
|
| 193 |
+
lines.append("")
|
| 194 |
+
except Exception as e:
|
| 195 |
+
logger.debug("Pattern detection for appointment export failed: %s", e)
|
| 196 |
+
|
| 197 |
+
# Cross-session co-occurrences
|
| 198 |
+
try:
|
| 199 |
+
from reachy_mini_conversation_app.database import MiniMinderDB
|
| 200 |
+
from reachy_mini_conversation_app.config import DB_PATH
|
| 201 |
+
|
| 202 |
+
db = MiniMinderDB(DB_PATH)
|
| 203 |
+
profile = db.get_or_create_profile()
|
| 204 |
+
db.close()
|
| 205 |
+
patient_name = profile.get("display_name") or profile.get("name", "Patient")
|
| 206 |
+
|
| 207 |
+
co_occurrences = graph.get_cross_session_co_occurrences(
|
| 208 |
+
patient_name, days=30
|
| 209 |
+
)
|
| 210 |
+
if co_occurrences:
|
| 211 |
+
lines.append("**Recurring Co-Occurrences:**")
|
| 212 |
+
for co in co_occurrences[:5]:
|
| 213 |
+
lines.append(
|
| 214 |
+
f"- {co.get('type_a', '?')} and {co.get('type_b', '?')} "
|
| 215 |
+
f"co-occurred {co.get('co_count', 0)} time(s)"
|
| 216 |
+
)
|
| 217 |
+
lines.append("")
|
| 218 |
+
except Exception as e:
|
| 219 |
+
logger.debug("Co-occurrence query for appointment failed: %s", e)
|
| 220 |
+
|
| 221 |
+
# Only return if we actually found something
|
| 222 |
+
if len(lines) > 1:
|
| 223 |
+
return "\n".join(lines)
|
| 224 |
+
return ""
|
| 225 |
+
|
| 226 |
+
except Exception as e:
|
| 227 |
+
logger.debug("Graph summary for appointment export unavailable: %s", e)
|
| 228 |
+
return ""
|
| 229 |
+
|
| 230 |
+
|
| 231 |
def _generate_fallback_summary(
|
| 232 |
headaches: List[Dict],
|
| 233 |
medications: List[Dict],
|
src/reachy_mini_conversation_app/database.py
CHANGED
|
@@ -750,17 +750,23 @@ h1 {{ color: #333; }}
|
|
| 750 |
if logged_entry:
|
| 751 |
# Format logged time
|
| 752 |
logged_at = None
|
| 753 |
-
actual_time = logged_entry.get("actual_time")
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 757 |
try:
|
| 758 |
-
dt = datetime.fromisoformat(
|
| 759 |
-
str(actual_time).replace("Z", "+00:00")
|
| 760 |
-
)
|
| 761 |
logged_at = dt.strftime("%I:%M %p").lstrip("0")
|
| 762 |
except ValueError:
|
| 763 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 764 |
|
| 765 |
scheduled_status.append(
|
| 766 |
{
|
|
|
|
| 750 |
if logged_entry:
|
| 751 |
# Format logged time
|
| 752 |
logged_at = None
|
| 753 |
+
actual_time = logged_entry.get("actual_time")
|
| 754 |
+
created_at = logged_entry.get("created_at")
|
| 755 |
+
|
| 756 |
+
# Try actual_time first, then created_at
|
| 757 |
+
for ts in (actual_time, created_at):
|
| 758 |
+
if not ts or logged_at:
|
| 759 |
+
continue
|
| 760 |
+
ts_str = str(ts)
|
| 761 |
try:
|
| 762 |
+
dt = datetime.fromisoformat(ts_str.replace("Z", "+00:00"))
|
|
|
|
|
|
|
| 763 |
logged_at = dt.strftime("%I:%M %p").lstrip("0")
|
| 764 |
except ValueError:
|
| 765 |
+
# Not a valid datetime (e.g. "now", "morning")
|
| 766 |
+
continue
|
| 767 |
+
|
| 768 |
+
if not logged_at:
|
| 769 |
+
logged_at = "earlier today"
|
| 770 |
|
| 771 |
scheduled_status.append(
|
| 772 |
{
|
src/reachy_mini_conversation_app/entry_state.py
CHANGED
|
@@ -19,13 +19,16 @@ EntryMode = Literal["idle", "headache", "medication"]
|
|
| 19 |
class EntryStateManager:
|
| 20 |
"""Manages the active entry being built via voice conversation."""
|
| 21 |
|
| 22 |
-
def __init__(self, database: MiniMinderDB) -> None:
|
| 23 |
self._db = database
|
|
|
|
| 24 |
self.entry_mode: EntryMode = "idle"
|
| 25 |
self.active_headache: Dict[str, Any] = {}
|
| 26 |
self.active_medication: Dict[str, Any] = {}
|
| 27 |
|
| 28 |
-
def start_headache(
|
|
|
|
|
|
|
| 29 |
"""Begin a new headache diary entry."""
|
| 30 |
self.entry_mode = "headache"
|
| 31 |
self.active_headache = dict(initial_data) if initial_data else {}
|
|
@@ -35,26 +38,100 @@ class EntryStateManager:
|
|
| 35 |
def update_headache(self, updates: Dict[str, Any]) -> Dict[str, Any]:
|
| 36 |
"""Update fields on the active headache entry."""
|
| 37 |
if self.entry_mode != "headache":
|
| 38 |
-
return {
|
|
|
|
|
|
|
| 39 |
self.active_headache.update(updates)
|
| 40 |
logger.info("Updated headache entry: %s", self.active_headache)
|
| 41 |
return {"status": "updated", "fields": self.active_headache}
|
| 42 |
|
| 43 |
-
def start_medication(
|
|
|
|
|
|
|
| 44 |
"""Begin a new medication log entry."""
|
| 45 |
self.entry_mode = "medication"
|
| 46 |
self.active_medication = dict(initial_data) if initial_data else {}
|
| 47 |
logger.info("Started medication entry: %s", self.active_medication)
|
| 48 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def update_medication(self, updates: Dict[str, Any]) -> Dict[str, Any]:
|
| 51 |
"""Update fields on the active medication entry."""
|
| 52 |
if self.entry_mode != "medication":
|
| 53 |
-
return {
|
|
|
|
|
|
|
| 54 |
self.active_medication.update(updates)
|
| 55 |
logger.info("Updated medication entry: %s", self.active_medication)
|
| 56 |
return {"status": "updated", "fields": self.active_medication}
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
def save_current(self) -> Dict[str, Any]:
|
| 59 |
"""Validate and persist the active entry to the database."""
|
| 60 |
if self.entry_mode == "headache":
|
|
@@ -62,6 +139,7 @@ class EntryStateManager:
|
|
| 62 |
if not data:
|
| 63 |
return {"error": "Headache entry is empty. Nothing to save."}
|
| 64 |
row_id = self._db.insert_headache(data)
|
|
|
|
| 65 |
self.active_headache = {}
|
| 66 |
self.entry_mode = "idle"
|
| 67 |
return {"status": "saved", "type": "headache", "id": row_id}
|
|
@@ -71,6 +149,7 @@ class EntryStateManager:
|
|
| 71 |
if not data.get("medication_name"):
|
| 72 |
return {"error": "Medication name is required before saving."}
|
| 73 |
row_id = self._db.insert_medication(data)
|
|
|
|
| 74 |
self.active_medication = {}
|
| 75 |
self.entry_mode = "idle"
|
| 76 |
return {"status": "saved", "type": "medication", "id": row_id}
|
|
@@ -89,6 +168,10 @@ class EntryStateManager:
|
|
| 89 |
"""Return the current entry state."""
|
| 90 |
return {
|
| 91 |
"mode": self.entry_mode,
|
| 92 |
-
"active_headache":
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
}
|
|
|
|
| 19 |
class EntryStateManager:
|
| 20 |
"""Manages the active entry being built via voice conversation."""
|
| 21 |
|
| 22 |
+
def __init__(self, database: MiniMinderDB, graph_memory: Any = None) -> None:
|
| 23 |
self._db = database
|
| 24 |
+
self._graph = graph_memory
|
| 25 |
self.entry_mode: EntryMode = "idle"
|
| 26 |
self.active_headache: Dict[str, Any] = {}
|
| 27 |
self.active_medication: Dict[str, Any] = {}
|
| 28 |
|
| 29 |
+
def start_headache(
|
| 30 |
+
self, initial_data: Optional[Dict[str, Any]] = None
|
| 31 |
+
) -> Dict[str, Any]:
|
| 32 |
"""Begin a new headache diary entry."""
|
| 33 |
self.entry_mode = "headache"
|
| 34 |
self.active_headache = dict(initial_data) if initial_data else {}
|
|
|
|
| 38 |
def update_headache(self, updates: Dict[str, Any]) -> Dict[str, Any]:
|
| 39 |
"""Update fields on the active headache entry."""
|
| 40 |
if self.entry_mode != "headache":
|
| 41 |
+
return {
|
| 42 |
+
"error": "No active headache entry. Use start_headache_entry first."
|
| 43 |
+
}
|
| 44 |
self.active_headache.update(updates)
|
| 45 |
logger.info("Updated headache entry: %s", self.active_headache)
|
| 46 |
return {"status": "updated", "fields": self.active_headache}
|
| 47 |
|
| 48 |
+
def start_medication(
|
| 49 |
+
self, initial_data: Optional[Dict[str, Any]] = None
|
| 50 |
+
) -> Dict[str, Any]:
|
| 51 |
"""Begin a new medication log entry."""
|
| 52 |
self.entry_mode = "medication"
|
| 53 |
self.active_medication = dict(initial_data) if initial_data else {}
|
| 54 |
logger.info("Started medication entry: %s", self.active_medication)
|
| 55 |
+
return {
|
| 56 |
+
"status": "started",
|
| 57 |
+
"mode": "medication",
|
| 58 |
+
"fields": self.active_medication,
|
| 59 |
+
}
|
| 60 |
|
| 61 |
def update_medication(self, updates: Dict[str, Any]) -> Dict[str, Any]:
|
| 62 |
"""Update fields on the active medication entry."""
|
| 63 |
if self.entry_mode != "medication":
|
| 64 |
+
return {
|
| 65 |
+
"error": "No active medication entry. Use start_medication_entry first."
|
| 66 |
+
}
|
| 67 |
self.active_medication.update(updates)
|
| 68 |
logger.info("Updated medication entry: %s", self.active_medication)
|
| 69 |
return {"status": "updated", "fields": self.active_medication}
|
| 70 |
|
| 71 |
+
def _get_patient_name(self) -> str:
|
| 72 |
+
"""Resolve patient name from profile for graph writes."""
|
| 73 |
+
try:
|
| 74 |
+
profile = self._db.get_or_create_profile()
|
| 75 |
+
return profile.get("display_name") or profile.get("name", "Patient")
|
| 76 |
+
except Exception:
|
| 77 |
+
return "Patient"
|
| 78 |
+
|
| 79 |
+
def _write_headache_to_graph(self, data: Dict[str, Any]) -> None:
|
| 80 |
+
"""Write-through: mirror headache entry to Neo4j graph."""
|
| 81 |
+
if not self._graph or not getattr(self._graph, "is_connected", False):
|
| 82 |
+
return
|
| 83 |
+
try:
|
| 84 |
+
patient_name = self._get_patient_name()
|
| 85 |
+
notes_parts = []
|
| 86 |
+
if data.get("location"):
|
| 87 |
+
notes_parts.append(f"location: {data['location']}")
|
| 88 |
+
if data.get("intensity"):
|
| 89 |
+
notes_parts.append(f"intensity: {data['intensity']}/10")
|
| 90 |
+
if data.get("triggers"):
|
| 91 |
+
notes_parts.append(f"triggers: {data['triggers']}")
|
| 92 |
+
if data.get("notes"):
|
| 93 |
+
notes_parts.append(data["notes"])
|
| 94 |
+
notes = "; ".join(notes_parts) if notes_parts else None
|
| 95 |
+
|
| 96 |
+
self._graph.add_event(
|
| 97 |
+
event_type="headache",
|
| 98 |
+
notes=notes,
|
| 99 |
+
patient_name=patient_name,
|
| 100 |
+
)
|
| 101 |
+
logger.info("Graph write-through: headache event for %s", patient_name)
|
| 102 |
+
except Exception as e:
|
| 103 |
+
logger.warning("Graph write-through (headache) failed: %s", e)
|
| 104 |
+
|
| 105 |
+
def _write_medication_to_graph(self, data: Dict[str, Any]) -> None:
|
| 106 |
+
"""Write-through: mirror medication entry to Neo4j graph."""
|
| 107 |
+
if not self._graph or not getattr(self._graph, "is_connected", False):
|
| 108 |
+
return
|
| 109 |
+
try:
|
| 110 |
+
patient_name = self._get_patient_name()
|
| 111 |
+
med_name = data.get("medication_name", "")
|
| 112 |
+
notes = data.get("notes")
|
| 113 |
+
|
| 114 |
+
self._graph.add_event(
|
| 115 |
+
event_type="medication_taken",
|
| 116 |
+
notes=f"{med_name}" + (f" — {notes}" if notes else ""),
|
| 117 |
+
patient_name=patient_name,
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# Ensure TAKES relationship exists
|
| 121 |
+
if med_name:
|
| 122 |
+
try:
|
| 123 |
+
self._graph.link_patient_takes_medication(patient_name, med_name)
|
| 124 |
+
except Exception:
|
| 125 |
+
pass # Relationship may not exist yet if med node missing
|
| 126 |
+
|
| 127 |
+
logger.info(
|
| 128 |
+
"Graph write-through: medication_taken (%s) for %s",
|
| 129 |
+
med_name,
|
| 130 |
+
patient_name,
|
| 131 |
+
)
|
| 132 |
+
except Exception as e:
|
| 133 |
+
logger.warning("Graph write-through (medication) failed: %s", e)
|
| 134 |
+
|
| 135 |
def save_current(self) -> Dict[str, Any]:
|
| 136 |
"""Validate and persist the active entry to the database."""
|
| 137 |
if self.entry_mode == "headache":
|
|
|
|
| 139 |
if not data:
|
| 140 |
return {"error": "Headache entry is empty. Nothing to save."}
|
| 141 |
row_id = self._db.insert_headache(data)
|
| 142 |
+
self._write_headache_to_graph(data)
|
| 143 |
self.active_headache = {}
|
| 144 |
self.entry_mode = "idle"
|
| 145 |
return {"status": "saved", "type": "headache", "id": row_id}
|
|
|
|
| 149 |
if not data.get("medication_name"):
|
| 150 |
return {"error": "Medication name is required before saving."}
|
| 151 |
row_id = self._db.insert_medication(data)
|
| 152 |
+
self._write_medication_to_graph(data)
|
| 153 |
self.active_medication = {}
|
| 154 |
self.entry_mode = "idle"
|
| 155 |
return {"status": "saved", "type": "medication", "id": row_id}
|
|
|
|
| 168 |
"""Return the current entry state."""
|
| 169 |
return {
|
| 170 |
"mode": self.entry_mode,
|
| 171 |
+
"active_headache": (
|
| 172 |
+
self.active_headache if self.entry_mode == "headache" else None
|
| 173 |
+
),
|
| 174 |
+
"active_medication": (
|
| 175 |
+
self.active_medication if self.entry_mode == "medication" else None
|
| 176 |
+
),
|
| 177 |
}
|
src/reachy_mini_conversation_app/langgraph_agent/nodes/report_builder.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import asyncio
|
|
|
|
| 2 |
import uuid
|
| 3 |
from datetime import datetime
|
| 4 |
-
from typing import Dict, Any
|
| 5 |
|
| 6 |
from langchain_core.messages import AIMessage
|
| 7 |
from langgraph.graph.ui import push_ui_message
|
|
@@ -9,6 +10,8 @@ from langgraph.graph.ui import push_ui_message
|
|
| 9 |
from reachy_mini_conversation_app.database import MiniMinderDB
|
| 10 |
from reachy_mini_conversation_app.config import DB_PATH
|
| 11 |
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def _fetch_report_data():
|
| 14 |
"""Synchronous DB access — runs in a thread to avoid blocking the event loop."""
|
|
@@ -20,6 +23,27 @@ def _fetch_report_data():
|
|
| 20 |
return headaches, medications, profile
|
| 21 |
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def _save_report(
|
| 24 |
report_type: str, title: str, content: str, metadata: dict | None = None
|
| 25 |
) -> int:
|
|
@@ -33,6 +57,9 @@ def _save_report(
|
|
| 33 |
async def report_builder_node(state: Dict[str, Any]):
|
| 34 |
headaches, medications, profile = await asyncio.to_thread(_fetch_report_data)
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
display_name = profile.get("display_name", "User")
|
| 37 |
title = f"Health Report for {display_name}"
|
| 38 |
|
|
@@ -63,10 +90,25 @@ async def report_builder_node(state: Dict[str, Any]):
|
|
| 63 |
f"Total entries: {len(medications)}",
|
| 64 |
"\n### Headache Diary",
|
| 65 |
f"Total episodes logged: {len(headaches)}",
|
| 66 |
-
"\n### Professional Notes",
|
| 67 |
-
"Patient shows consistent tracking. Intensity remains stable.",
|
| 68 |
]
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
accumulated_content = ""
|
| 71 |
for line in contentLines:
|
| 72 |
accumulated_content += line + "\n"
|
|
|
|
| 1 |
import asyncio
|
| 2 |
+
import logging
|
| 3 |
import uuid
|
| 4 |
from datetime import datetime
|
| 5 |
+
from typing import Dict, Any, List
|
| 6 |
|
| 7 |
from langchain_core.messages import AIMessage
|
| 8 |
from langgraph.graph.ui import push_ui_message
|
|
|
|
| 10 |
from reachy_mini_conversation_app.database import MiniMinderDB
|
| 11 |
from reachy_mini_conversation_app.config import DB_PATH
|
| 12 |
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
|
| 16 |
def _fetch_report_data():
|
| 17 |
"""Synchronous DB access — runs in a thread to avoid blocking the event loop."""
|
|
|
|
| 23 |
return headaches, medications, profile
|
| 24 |
|
| 25 |
|
| 26 |
+
def _fetch_graph_insights() -> List[Dict[str, Any]]:
|
| 27 |
+
"""Attempt to fetch pattern insights from Neo4j — runs in a thread."""
|
| 28 |
+
try:
|
| 29 |
+
from reachy_mini_conversation_app.session_enrichment import (
|
| 30 |
+
get_session_enrichment,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
enrichment = get_session_enrichment()
|
| 34 |
+
if not enrichment or not enrichment._graph:
|
| 35 |
+
return []
|
| 36 |
+
|
| 37 |
+
from reachy_mini_conversation_app.pattern_detector import PatternDetector
|
| 38 |
+
|
| 39 |
+
detector = PatternDetector(enrichment._graph)
|
| 40 |
+
insights = detector.run_analysis()
|
| 41 |
+
return [i.to_dict() for i in insights]
|
| 42 |
+
except Exception as e:
|
| 43 |
+
logger.debug("Graph insights not available for report: %s", e)
|
| 44 |
+
return []
|
| 45 |
+
|
| 46 |
+
|
| 47 |
def _save_report(
|
| 48 |
report_type: str, title: str, content: str, metadata: dict | None = None
|
| 49 |
) -> int:
|
|
|
|
| 57 |
async def report_builder_node(state: Dict[str, Any]):
|
| 58 |
headaches, medications, profile = await asyncio.to_thread(_fetch_report_data)
|
| 59 |
|
| 60 |
+
# Fetch graph insights
|
| 61 |
+
graph_insights = await asyncio.to_thread(_fetch_graph_insights)
|
| 62 |
+
|
| 63 |
display_name = profile.get("display_name", "User")
|
| 64 |
title = f"Health Report for {display_name}"
|
| 65 |
|
|
|
|
| 90 |
f"Total entries: {len(medications)}",
|
| 91 |
"\n### Headache Diary",
|
| 92 |
f"Total episodes logged: {len(headaches)}",
|
|
|
|
|
|
|
| 93 |
]
|
| 94 |
|
| 95 |
+
# Add graph pattern insights if available
|
| 96 |
+
if graph_insights:
|
| 97 |
+
contentLines.append("\n### Patterns & Correlations")
|
| 98 |
+
for gi in graph_insights:
|
| 99 |
+
summary = gi.get("summary", "")
|
| 100 |
+
pattern_type = gi.get("pattern_type", "")
|
| 101 |
+
contentLines.append(f"- **{pattern_type}:** {summary}")
|
| 102 |
+
if gi.get("detail"):
|
| 103 |
+
contentLines.append(f" _{gi['detail']}_")
|
| 104 |
+
|
| 105 |
+
contentLines.extend(
|
| 106 |
+
[
|
| 107 |
+
"\n### Professional Notes",
|
| 108 |
+
"Patient shows consistent tracking. Intensity remains stable.",
|
| 109 |
+
]
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
accumulated_content = ""
|
| 113 |
for line in contentLines:
|
| 114 |
accumulated_content += line + "\n"
|
src/reachy_mini_conversation_app/langgraph_agent/nodes/trend_analyzer.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import asyncio
|
| 2 |
import json
|
| 3 |
-
|
|
|
|
| 4 |
from langchain_core.messages import AIMessage
|
| 5 |
from langgraph.graph.ui import push_ui_message
|
| 6 |
from datetime import datetime
|
|
@@ -9,6 +10,8 @@ from collections import Counter
|
|
| 9 |
from reachy_mini_conversation_app.database import MiniMinderDB
|
| 10 |
from reachy_mini_conversation_app.config import DB_PATH
|
| 11 |
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def _fetch_trend_data():
|
| 14 |
"""Synchronous DB access — runs in a thread to avoid blocking the event loop."""
|
|
@@ -19,11 +22,33 @@ def _fetch_trend_data():
|
|
| 19 |
return headaches, profile
|
| 20 |
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def _save_trend_report(
|
| 23 |
chart_data: list,
|
| 24 |
total_episodes: int,
|
| 25 |
worst_day: str,
|
| 26 |
insight: str | None,
|
|
|
|
| 27 |
metadata: dict | None = None,
|
| 28 |
) -> int:
|
| 29 |
"""Save trend analysis as a report — runs in a thread."""
|
|
@@ -38,6 +63,17 @@ def _save_trend_report(
|
|
| 38 |
]
|
| 39 |
for entry in chart_data:
|
| 40 |
lines.append(f"- **{entry['day']}:** {entry['count']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
if insight:
|
| 42 |
lines.append("")
|
| 43 |
lines.append(f"### AI Insight")
|
|
@@ -55,6 +91,9 @@ def _save_trend_report(
|
|
| 55 |
async def trend_analyzer_node(state: Dict[str, Any]):
|
| 56 |
headaches, profile = await asyncio.to_thread(_fetch_trend_data)
|
| 57 |
|
|
|
|
|
|
|
|
|
|
| 58 |
# 2. Analyze trends
|
| 59 |
# Count headaches per day of week
|
| 60 |
days_of_week = []
|
|
@@ -92,6 +131,7 @@ async def trend_analyzer_node(state: Dict[str, Any]):
|
|
| 92 |
len(headaches),
|
| 93 |
worst_day,
|
| 94 |
insight_text,
|
|
|
|
| 95 |
{
|
| 96 |
"doctorName": profile.get("neurologist_name"),
|
| 97 |
"doctorEmail": profile.get("neurologist_email"),
|
|
@@ -111,6 +151,7 @@ async def trend_analyzer_node(state: Dict[str, Any]):
|
|
| 111 |
"total_episodes": len(headaches),
|
| 112 |
"worst_day": worst_day,
|
| 113 |
"insight": insight_text,
|
|
|
|
| 114 |
"doctorName": profile.get("neurologist_name"),
|
| 115 |
"doctorEmail": profile.get("neurologist_email"),
|
| 116 |
"savedId": saved_id,
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import json
|
| 3 |
+
import logging
|
| 4 |
+
from typing import Dict, Any, List
|
| 5 |
from langchain_core.messages import AIMessage
|
| 6 |
from langgraph.graph.ui import push_ui_message
|
| 7 |
from datetime import datetime
|
|
|
|
| 10 |
from reachy_mini_conversation_app.database import MiniMinderDB
|
| 11 |
from reachy_mini_conversation_app.config import DB_PATH
|
| 12 |
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
|
| 16 |
def _fetch_trend_data():
|
| 17 |
"""Synchronous DB access — runs in a thread to avoid blocking the event loop."""
|
|
|
|
| 22 |
return headaches, profile
|
| 23 |
|
| 24 |
|
| 25 |
+
def _fetch_graph_insights() -> List[Dict[str, Any]]:
|
| 26 |
+
"""Attempt to fetch pattern insights from Neo4j — runs in a thread."""
|
| 27 |
+
try:
|
| 28 |
+
from reachy_mini_conversation_app.session_enrichment import (
|
| 29 |
+
get_session_enrichment,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
enrichment = get_session_enrichment()
|
| 33 |
+
if not enrichment or not enrichment._graph:
|
| 34 |
+
return []
|
| 35 |
+
|
| 36 |
+
from reachy_mini_conversation_app.pattern_detector import PatternDetector
|
| 37 |
+
|
| 38 |
+
detector = PatternDetector(enrichment._graph)
|
| 39 |
+
insights = detector.run_analysis()
|
| 40 |
+
return [i.to_dict() for i in insights]
|
| 41 |
+
except Exception as e:
|
| 42 |
+
logger.debug("Graph insights not available for trend analysis: %s", e)
|
| 43 |
+
return []
|
| 44 |
+
|
| 45 |
+
|
| 46 |
def _save_trend_report(
|
| 47 |
chart_data: list,
|
| 48 |
total_episodes: int,
|
| 49 |
worst_day: str,
|
| 50 |
insight: str | None,
|
| 51 |
+
graph_insights: list,
|
| 52 |
metadata: dict | None = None,
|
| 53 |
) -> int:
|
| 54 |
"""Save trend analysis as a report — runs in a thread."""
|
|
|
|
| 63 |
]
|
| 64 |
for entry in chart_data:
|
| 65 |
lines.append(f"- **{entry['day']}:** {entry['count']}")
|
| 66 |
+
|
| 67 |
+
if graph_insights:
|
| 68 |
+
lines.append("")
|
| 69 |
+
lines.append("### Patterns & Correlations")
|
| 70 |
+
for gi in graph_insights:
|
| 71 |
+
lines.append(
|
| 72 |
+
f"- **{gi.get('pattern_type', 'pattern')}:** {gi.get('summary', '')}"
|
| 73 |
+
)
|
| 74 |
+
if gi.get("detail"):
|
| 75 |
+
lines.append(f" _{gi['detail']}_")
|
| 76 |
+
|
| 77 |
if insight:
|
| 78 |
lines.append("")
|
| 79 |
lines.append(f"### AI Insight")
|
|
|
|
| 91 |
async def trend_analyzer_node(state: Dict[str, Any]):
|
| 92 |
headaches, profile = await asyncio.to_thread(_fetch_trend_data)
|
| 93 |
|
| 94 |
+
# Fetch graph insights in parallel with analysis
|
| 95 |
+
graph_insights = await asyncio.to_thread(_fetch_graph_insights)
|
| 96 |
+
|
| 97 |
# 2. Analyze trends
|
| 98 |
# Count headaches per day of week
|
| 99 |
days_of_week = []
|
|
|
|
| 131 |
len(headaches),
|
| 132 |
worst_day,
|
| 133 |
insight_text,
|
| 134 |
+
graph_insights,
|
| 135 |
{
|
| 136 |
"doctorName": profile.get("neurologist_name"),
|
| 137 |
"doctorEmail": profile.get("neurologist_email"),
|
|
|
|
| 151 |
"total_episodes": len(headaches),
|
| 152 |
"worst_day": worst_day,
|
| 153 |
"insight": insight_text,
|
| 154 |
+
"graphInsights": graph_insights,
|
| 155 |
"doctorName": profile.get("neurologist_name"),
|
| 156 |
"doctorEmail": profile.get("neurologist_email"),
|
| 157 |
"savedId": saved_id,
|
src/reachy_mini_conversation_app/main.py
CHANGED
|
@@ -150,6 +150,7 @@ def run(
|
|
| 150 |
if graph_memory.connect():
|
| 151 |
init_session_enrichment(graph_memory=graph_memory)
|
| 152 |
enable_session_enrichment()
|
|
|
|
| 153 |
logger.info("Session enrichment enabled with Neo4j")
|
| 154 |
else:
|
| 155 |
init_session_enrichment(graph_memory=None)
|
|
|
|
| 150 |
if graph_memory.connect():
|
| 151 |
init_session_enrichment(graph_memory=graph_memory)
|
| 152 |
enable_session_enrichment()
|
| 153 |
+
entry_state._graph = graph_memory # Enable write-through
|
| 154 |
logger.info("Session enrichment enabled with Neo4j")
|
| 155 |
else:
|
| 156 |
init_session_enrichment(graph_memory=None)
|
src/reachy_mini_conversation_app/memory_graph.py
CHANGED
|
@@ -7,7 +7,7 @@ and events to support relationship-aware context for Elena and caregiver triage
|
|
| 7 |
from __future__ import annotations
|
| 8 |
|
| 9 |
import logging
|
| 10 |
-
from datetime import datetime
|
| 11 |
from typing import Any, Dict, List, Optional
|
| 12 |
|
| 13 |
try:
|
|
@@ -556,3 +556,108 @@ class GraphMemory:
|
|
| 556 |
lines.append("")
|
| 557 |
|
| 558 |
return "\n".join(lines)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from __future__ import annotations
|
| 8 |
|
| 9 |
import logging
|
| 10 |
+
from datetime import datetime, timedelta
|
| 11 |
from typing import Any, Dict, List, Optional
|
| 12 |
|
| 13 |
try:
|
|
|
|
| 556 |
lines.append("")
|
| 557 |
|
| 558 |
return "\n".join(lines)
|
| 559 |
+
|
| 560 |
+
# -----------------------------------------------------------------
|
| 561 |
+
# Cross-session entity memory
|
| 562 |
+
# -----------------------------------------------------------------
|
| 563 |
+
|
| 564 |
+
def get_entity_timeline(
|
| 565 |
+
self, entity_name: str, days: int = 30
|
| 566 |
+
) -> List[Dict[str, Any]]:
|
| 567 |
+
"""Return sessions where an entity was mentioned, with context.
|
| 568 |
+
|
| 569 |
+
Args:
|
| 570 |
+
entity_name: Name of the entity to trace (case-insensitive).
|
| 571 |
+
days: Look-back window in days.
|
| 572 |
+
|
| 573 |
+
Returns:
|
| 574 |
+
List of dicts with session_id, timestamp, relationship, and context.
|
| 575 |
+
"""
|
| 576 |
+
cutoff = (datetime.now() - timedelta(days=days)).isoformat()
|
| 577 |
+
query = """
|
| 578 |
+
MATCH (n)-[r]-(e)
|
| 579 |
+
WHERE toLower(n.name) = toLower($entity_name)
|
| 580 |
+
AND (e.timestamp IS NULL OR e.timestamp >= $cutoff)
|
| 581 |
+
RETURN
|
| 582 |
+
labels(n)[0] AS entity_type,
|
| 583 |
+
n.name AS entity_name,
|
| 584 |
+
type(r) AS relationship,
|
| 585 |
+
labels(e)[0] AS related_type,
|
| 586 |
+
e.type AS event_type,
|
| 587 |
+
e.timestamp AS timestamp,
|
| 588 |
+
e.notes AS notes
|
| 589 |
+
ORDER BY e.timestamp DESC
|
| 590 |
+
LIMIT 20
|
| 591 |
+
"""
|
| 592 |
+
try:
|
| 593 |
+
rows = self._execute(query, {"entity_name": entity_name, "cutoff": cutoff})
|
| 594 |
+
return [dict(r) for r in rows] if rows else []
|
| 595 |
+
except Exception as e:
|
| 596 |
+
logger.debug("Entity timeline query failed: %s", e)
|
| 597 |
+
return []
|
| 598 |
+
|
| 599 |
+
def get_cross_session_co_occurrences(
|
| 600 |
+
self, patient_name: str, days: int = 30, min_sessions: int = 2
|
| 601 |
+
) -> List[Dict[str, Any]]:
|
| 602 |
+
"""Find entities that appear together across multiple sessions.
|
| 603 |
+
|
| 604 |
+
Uses MENTIONED_WITH relationships to identify recurring co-occurrences.
|
| 605 |
+
|
| 606 |
+
Args:
|
| 607 |
+
patient_name: Patient to scope the query to.
|
| 608 |
+
days: Look-back window in days.
|
| 609 |
+
min_sessions: Minimum number of co-occurrences to include.
|
| 610 |
+
|
| 611 |
+
Returns:
|
| 612 |
+
List of co-occurrence dicts with entity pairs and counts.
|
| 613 |
+
"""
|
| 614 |
+
query = """
|
| 615 |
+
MATCH (p:Person {name: $patient_name})-[:EXPERIENCED]->(e1:Event)
|
| 616 |
+
MATCH (e1)-[:MENTIONED_WITH]-(e2:Event)
|
| 617 |
+
WHERE e1.timestamp >= $cutoff AND e2.timestamp >= $cutoff
|
| 618 |
+
AND id(e1) < id(e2)
|
| 619 |
+
WITH e1.type AS type_a, e2.type AS type_b,
|
| 620 |
+
e1.notes AS notes_a, e2.notes AS notes_b,
|
| 621 |
+
count(*) AS co_count
|
| 622 |
+
WHERE co_count >= $min_sessions
|
| 623 |
+
RETURN type_a, type_b, notes_a, notes_b, co_count
|
| 624 |
+
ORDER BY co_count DESC
|
| 625 |
+
LIMIT 10
|
| 626 |
+
"""
|
| 627 |
+
cutoff = (datetime.now() - timedelta(days=days)).isoformat()
|
| 628 |
+
try:
|
| 629 |
+
rows = self._execute(
|
| 630 |
+
query,
|
| 631 |
+
{
|
| 632 |
+
"patient_name": patient_name,
|
| 633 |
+
"cutoff": cutoff,
|
| 634 |
+
"min_sessions": min_sessions,
|
| 635 |
+
},
|
| 636 |
+
)
|
| 637 |
+
return [dict(r) for r in rows] if rows else []
|
| 638 |
+
except Exception as e:
|
| 639 |
+
logger.debug("Co-occurrence query failed: %s", e)
|
| 640 |
+
return []
|
| 641 |
+
|
| 642 |
+
def format_entity_recall_for_prompt(
|
| 643 |
+
self, patient_name: str, days: int = 30, max_items: int = 3
|
| 644 |
+
) -> str:
|
| 645 |
+
"""Format structured entity recall for system prompt injection.
|
| 646 |
+
|
| 647 |
+
Returns a block of text summarising cross-session entity patterns
|
| 648 |
+
that can be appended to the LLM's context.
|
| 649 |
+
"""
|
| 650 |
+
co_occurrences = self.get_cross_session_co_occurrences(patient_name, days=days)
|
| 651 |
+
if not co_occurrences:
|
| 652 |
+
return ""
|
| 653 |
+
|
| 654 |
+
lines = ["## Cross-Session Patterns\n"]
|
| 655 |
+
for co in co_occurrences[:max_items]:
|
| 656 |
+
type_a = co.get("type_a", "event")
|
| 657 |
+
type_b = co.get("type_b", "event")
|
| 658 |
+
count = co.get("co_count", 0)
|
| 659 |
+
lines.append(
|
| 660 |
+
f"- {type_a} and {type_b} have co-occurred {count} time(s) recently"
|
| 661 |
+
)
|
| 662 |
+
|
| 663 |
+
return "\n".join(lines)
|
src/reachy_mini_conversation_app/openai_realtime.py
CHANGED
|
@@ -153,7 +153,11 @@ class OpenaiRealtimeHandler(RealtimeHandler):
|
|
| 153 |
|
| 154 |
# Patient context from Neo4j
|
| 155 |
if enrichment and enrichment._graph:
|
| 156 |
-
patient_name =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
if patient_name:
|
| 158 |
ctx = enrichment._graph.format_context_for_prompt(patient_name)
|
| 159 |
if ctx:
|
|
@@ -170,6 +174,17 @@ class OpenaiRealtimeHandler(RealtimeHandler):
|
|
| 170 |
if insights_text:
|
| 171 |
graph_parts.append(insights_text)
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
if graph_parts:
|
| 174 |
state.graph_context = "\n\n".join(graph_parts)
|
| 175 |
logger.info(
|
|
@@ -909,6 +924,43 @@ class OpenaiRealtimeHandler(RealtimeHandler):
|
|
| 909 |
"If this IS the welcome step, introduce yourself warmly. "
|
| 910 |
"You MUST respond in British English only."
|
| 911 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 912 |
else:
|
| 913 |
resp_instructions = (
|
| 914 |
"Use the tool result just returned and answer concisely in speech. "
|
|
|
|
| 153 |
|
| 154 |
# Patient context from Neo4j
|
| 155 |
if enrichment and enrichment._graph:
|
| 156 |
+
patient_name = (
|
| 157 |
+
(profile.get("display_name") or profile.get("name"))
|
| 158 |
+
if profile
|
| 159 |
+
else None
|
| 160 |
+
)
|
| 161 |
if patient_name:
|
| 162 |
ctx = enrichment._graph.format_context_for_prompt(patient_name)
|
| 163 |
if ctx:
|
|
|
|
| 174 |
if insights_text:
|
| 175 |
graph_parts.append(insights_text)
|
| 176 |
|
| 177 |
+
# Cross-session entity memory (co-occurrences)
|
| 178 |
+
if enrichment and enrichment._graph and patient_name:
|
| 179 |
+
try:
|
| 180 |
+
entity_recall = enrichment._graph.format_entity_recall_for_prompt(
|
| 181 |
+
patient_name
|
| 182 |
+
)
|
| 183 |
+
if entity_recall:
|
| 184 |
+
graph_parts.append(entity_recall)
|
| 185 |
+
except Exception:
|
| 186 |
+
pass # Entity recall is optional
|
| 187 |
+
|
| 188 |
if graph_parts:
|
| 189 |
state.graph_context = "\n\n".join(graph_parts)
|
| 190 |
logger.info(
|
|
|
|
| 924 |
"If this IS the welcome step, introduce yourself warmly. "
|
| 925 |
"You MUST respond in British English only."
|
| 926 |
)
|
| 927 |
+
elif tool_name in (
|
| 928 |
+
"check_medication",
|
| 929 |
+
"check_health_patterns",
|
| 930 |
+
"session_summary",
|
| 931 |
+
"query_health_history",
|
| 932 |
+
):
|
| 933 |
+
# These tools emit GenUI *and* return a message
|
| 934 |
+
# that MUST be spoken aloud so the user hears it.
|
| 935 |
+
force_speech = True
|
| 936 |
+
resp_instructions = (
|
| 937 |
+
"The information is now displayed on screen. "
|
| 938 |
+
"You MUST speak the 'message' from the tool result aloud to the user "
|
| 939 |
+
"so they understand what they are seeing. Be warm and conversational. "
|
| 940 |
+
"You MUST respond in British English only."
|
| 941 |
+
)
|
| 942 |
+
elif tool_name == "log_entry":
|
| 943 |
+
# After logging an entry, the robot MUST speak to
|
| 944 |
+
# confirm details with the user before saving.
|
| 945 |
+
force_speech = True
|
| 946 |
+
resp_instructions = (
|
| 947 |
+
"The entry details are now shown on screen. "
|
| 948 |
+
"You MUST speak to the user. Summarise what you have recorded "
|
| 949 |
+
"and ask the user to confirm it is correct before saving. "
|
| 950 |
+
"Say something like 'I have [details]. Shall I save this?' "
|
| 951 |
+
"NEVER call entry_control(save) without the user's explicit verbal confirmation. "
|
| 952 |
+
"You MUST respond in British English only."
|
| 953 |
+
)
|
| 954 |
+
elif tool_name == "entry_control":
|
| 955 |
+
# After saving/discarding, the robot should confirm
|
| 956 |
+
force_speech = True
|
| 957 |
+
resp_instructions = (
|
| 958 |
+
"The entry has been processed. "
|
| 959 |
+
"Briefly confirm to the user what happened — "
|
| 960 |
+
"e.g. 'Done, that's been saved.' or 'No worries, I've discarded it.' "
|
| 961 |
+
"Keep it short and warm. "
|
| 962 |
+
"You MUST respond in British English only."
|
| 963 |
+
)
|
| 964 |
else:
|
| 965 |
resp_instructions = (
|
| 966 |
"Use the tool result just returned and answer concisely in speech. "
|
src/reachy_mini_conversation_app/profiles/_reachy_mini_minder_locked_profile/log_entry.py
CHANGED
|
@@ -7,6 +7,7 @@ Auto-detects whether to start a new entry or update the active one.
|
|
| 7 |
|
| 8 |
import logging
|
| 9 |
import uuid
|
|
|
|
| 10 |
from typing import Any, Dict
|
| 11 |
|
| 12 |
from reachy_mini_conversation_app.tools.core_tools import Tool, ToolDependencies
|
|
@@ -97,11 +98,11 @@ class LogEntry(Tool):
|
|
| 97 |
},
|
| 98 |
"scheduled_time": {
|
| 99 |
"type": "string",
|
| 100 |
-
"description": "When the medication was supposed to be taken",
|
| 101 |
},
|
| 102 |
"actual_time": {
|
| 103 |
"type": "string",
|
| 104 |
-
"description": "When the medication was actually taken",
|
| 105 |
},
|
| 106 |
# ── Shared fields ──────────────────────────────────────────
|
| 107 |
"medication_taken": {
|
|
@@ -217,6 +218,75 @@ class LogEntry(Tool):
|
|
| 217 |
return result
|
| 218 |
|
| 219 |
# ── Medication ─────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
async def _handle_medication(
|
| 221 |
self, deps: ToolDependencies, fields: Dict[str, Any]
|
| 222 |
) -> Dict[str, Any]:
|
|
@@ -296,6 +366,34 @@ class LogEntry(Tool):
|
|
| 296 |
else:
|
| 297 |
is_ad_hoc = True
|
| 298 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
# Convert bools to ints for SQLite
|
| 300 |
for field in ("taken", "taken_late"):
|
| 301 |
if field in fields:
|
|
@@ -310,8 +408,35 @@ class LogEntry(Tool):
|
|
| 310 |
capture_status = "capturing"
|
| 311 |
robot_message = None
|
| 312 |
|
| 313 |
-
#
|
| 314 |
med_data = dict(esm.active_medication or {})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
taken_val = med_data.get("taken")
|
| 316 |
await emit_ui_component(
|
| 317 |
component_name="MedLog",
|
|
|
|
| 7 |
|
| 8 |
import logging
|
| 9 |
import uuid
|
| 10 |
+
from datetime import datetime, timedelta
|
| 11 |
from typing import Any, Dict
|
| 12 |
|
| 13 |
from reachy_mini_conversation_app.tools.core_tools import Tool, ToolDependencies
|
|
|
|
| 98 |
},
|
| 99 |
"scheduled_time": {
|
| 100 |
"type": "string",
|
| 101 |
+
"description": "When the medication was supposed to be taken (ISO datetime, or relative like '8am', 'morning')",
|
| 102 |
},
|
| 103 |
"actual_time": {
|
| 104 |
"type": "string",
|
| 105 |
+
"description": "When the medication was actually taken (ISO datetime, or relative like 'now', '2 hours ago', '8am')",
|
| 106 |
},
|
| 107 |
# ── Shared fields ──────────────────────────────────────────
|
| 108 |
"medication_taken": {
|
|
|
|
| 218 |
return result
|
| 219 |
|
| 220 |
# ── Medication ─────────────────────────────────────────────────────
|
| 221 |
+
|
| 222 |
+
@staticmethod
|
| 223 |
+
def _normalize_time(raw: str) -> str:
|
| 224 |
+
"""Convert LLM-provided time text to ISO datetime string.
|
| 225 |
+
|
| 226 |
+
Handles: 'now', 'just now', '8am', '8:30 PM', '2 hours ago',
|
| 227 |
+
'morning', 'evening', and already-valid ISO datetime strings.
|
| 228 |
+
"""
|
| 229 |
+
import re
|
| 230 |
+
|
| 231 |
+
now = datetime.now()
|
| 232 |
+
text = raw.strip().lower()
|
| 233 |
+
|
| 234 |
+
# Already a valid ISO datetime? Return as-is.
|
| 235 |
+
try:
|
| 236 |
+
datetime.fromisoformat(raw.replace("Z", "+00:00"))
|
| 237 |
+
return raw
|
| 238 |
+
except (ValueError, TypeError):
|
| 239 |
+
pass
|
| 240 |
+
|
| 241 |
+
# "now", "just now", "right now"
|
| 242 |
+
if text in ("now", "just now", "right now"):
|
| 243 |
+
return now.strftime("%Y-%m-%d %H:%M:%S")
|
| 244 |
+
|
| 245 |
+
# Relative: "X hours/minutes ago"
|
| 246 |
+
m = re.match(r"(\d+)\s*(hours?|minutes?|mins?)\s*ago", text)
|
| 247 |
+
if m:
|
| 248 |
+
amount = int(m.group(1))
|
| 249 |
+
unit = m.group(2)
|
| 250 |
+
if unit.startswith("hour"):
|
| 251 |
+
dt = now - timedelta(hours=amount)
|
| 252 |
+
else:
|
| 253 |
+
dt = now - timedelta(minutes=amount)
|
| 254 |
+
return dt.strftime("%Y-%m-%d %H:%M:%S")
|
| 255 |
+
|
| 256 |
+
# Bare time: "8am", "8:30pm", "14:00", "8 am"
|
| 257 |
+
m = re.match(r"(\d{1,2}):?(\d{2})?\s*(am|pm)?$", text)
|
| 258 |
+
if m:
|
| 259 |
+
hour = int(m.group(1))
|
| 260 |
+
minute = int(m.group(2) or 0)
|
| 261 |
+
ampm = m.group(3)
|
| 262 |
+
if ampm == "pm" and hour < 12:
|
| 263 |
+
hour += 12
|
| 264 |
+
elif ampm == "am" and hour == 12:
|
| 265 |
+
hour = 0
|
| 266 |
+
dt = now.replace(hour=hour, minute=minute, second=0, microsecond=0)
|
| 267 |
+
return dt.strftime("%Y-%m-%d %H:%M:%S")
|
| 268 |
+
|
| 269 |
+
# Time-of-day words
|
| 270 |
+
_TOD_MAP = {
|
| 271 |
+
"morning": 8,
|
| 272 |
+
"this morning": 8,
|
| 273 |
+
"afternoon": 13,
|
| 274 |
+
"this afternoon": 13,
|
| 275 |
+
"evening": 18,
|
| 276 |
+
"this evening": 18,
|
| 277 |
+
"night": 21,
|
| 278 |
+
"tonight": 21,
|
| 279 |
+
"last night": 21,
|
| 280 |
+
"noon": 12,
|
| 281 |
+
"midday": 12,
|
| 282 |
+
}
|
| 283 |
+
if text in _TOD_MAP:
|
| 284 |
+
dt = now.replace(hour=_TOD_MAP[text], minute=0, second=0, microsecond=0)
|
| 285 |
+
return dt.strftime("%Y-%m-%d %H:%M:%S")
|
| 286 |
+
|
| 287 |
+
# Can't parse — default to now
|
| 288 |
+
return now.strftime("%Y-%m-%d %H:%M:%S")
|
| 289 |
+
|
| 290 |
async def _handle_medication(
|
| 291 |
self, deps: ToolDependencies, fields: Dict[str, Any]
|
| 292 |
) -> Dict[str, Any]:
|
|
|
|
| 366 |
else:
|
| 367 |
is_ad_hoc = True
|
| 368 |
|
| 369 |
+
# ── Duplicate-awareness (soft warning, never blocks) ─────────────
|
| 370 |
+
# If this medication was already logged today, note it so the robot
|
| 371 |
+
# can mention it — but always proceed with logging. The patient
|
| 372 |
+
# said they took it, so we record it.
|
| 373 |
+
duplicate_logged_at = None
|
| 374 |
+
if med_name and esm.entry_mode != "medication" and deps.database:
|
| 375 |
+
today_status = deps.database.get_todays_medication_status()
|
| 376 |
+
for entry in today_status.get("scheduled", []):
|
| 377 |
+
if (
|
| 378 |
+
entry.get("name", "").lower() == med_name.lower()
|
| 379 |
+
and entry.get("status") == "logged"
|
| 380 |
+
):
|
| 381 |
+
duplicate_logged_at = entry.get("logged_at", "earlier today")
|
| 382 |
+
break
|
| 383 |
+
if not duplicate_logged_at:
|
| 384 |
+
for entry in today_status.get("ad_hoc", []):
|
| 385 |
+
if entry.get("name", "").lower() == med_name.lower():
|
| 386 |
+
duplicate_logged_at = entry.get("logged_at", "earlier today")
|
| 387 |
+
break
|
| 388 |
+
|
| 389 |
+
# ── Normalize time fields to ISO datetime ─���──────────────────────
|
| 390 |
+
# The LLM may send raw text like "now", "8am", "2 hours ago".
|
| 391 |
+
# Convert to proper datetime strings before storage.
|
| 392 |
+
for time_field in ("actual_time", "scheduled_time"):
|
| 393 |
+
raw = fields.get(time_field)
|
| 394 |
+
if raw and isinstance(raw, str):
|
| 395 |
+
fields[time_field] = self._normalize_time(raw)
|
| 396 |
+
|
| 397 |
# Convert bools to ints for SQLite
|
| 398 |
for field in ("taken", "taken_late"):
|
| 399 |
if field in fields:
|
|
|
|
| 408 |
capture_status = "capturing"
|
| 409 |
robot_message = None
|
| 410 |
|
| 411 |
+
# If the key field (medication_name) is present, switch to confirmation
|
| 412 |
med_data = dict(esm.active_medication or {})
|
| 413 |
+
has_med_name = bool(
|
| 414 |
+
med_data.get("medication_name") or fields.get("medication_name")
|
| 415 |
+
)
|
| 416 |
+
if has_med_name and capture_status == "capturing":
|
| 417 |
+
capture_status = "needs_confirmation"
|
| 418 |
+
if duplicate_logged_at:
|
| 419 |
+
robot_message = (
|
| 420 |
+
f"I have a record that you already took {med_name} "
|
| 421 |
+
f"at {duplicate_logged_at} today."
|
| 422 |
+
)
|
| 423 |
+
result["confirmation_required"] = True
|
| 424 |
+
result["duplicate_logged_at"] = duplicate_logged_at
|
| 425 |
+
result["voice_instruction"] = (
|
| 426 |
+
f"Factually tell the user: 'I have a record that you took "
|
| 427 |
+
f"{med_name} at {duplicate_logged_at} today.' "
|
| 428 |
+
f"Then ask: 'Would you still like me to log this?' "
|
| 429 |
+
f"Do NOT save until the user confirms. Be factual, not judgmental."
|
| 430 |
+
)
|
| 431 |
+
else:
|
| 432 |
+
robot_message = "Does this look right?"
|
| 433 |
+
result["confirmation_required"] = True
|
| 434 |
+
result["voice_instruction"] = (
|
| 435 |
+
"Summarise the medication entry and ask the user to confirm "
|
| 436 |
+
"before saving. Do NOT save until the user says yes."
|
| 437 |
+
)
|
| 438 |
+
|
| 439 |
+
# Emit MedLog GenUI
|
| 440 |
taken_val = med_data.get("taken")
|
| 441 |
await emit_ui_component(
|
| 442 |
component_name="MedLog",
|
src/reachy_mini_conversation_app/profiles/_reachy_mini_minder_locked_profile/tools.txt
CHANGED
|
@@ -8,6 +8,7 @@ entry_control
|
|
| 8 |
get_recent_entries
|
| 9 |
check_medication
|
| 10 |
query_health_history
|
|
|
|
| 11 |
|
| 12 |
# Onboarding & setup tools (unchanged)
|
| 13 |
get_current_datetime
|
|
|
|
| 8 |
get_recent_entries
|
| 9 |
check_medication
|
| 10 |
query_health_history
|
| 11 |
+
check_health_patterns
|
| 12 |
|
| 13 |
# Onboarding & setup tools (unchanged)
|
| 14 |
get_current_datetime
|
src/reachy_mini_conversation_app/tools/check_health_patterns.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Check health patterns tool.
|
| 2 |
+
|
| 3 |
+
A lightweight tool the LLM can invoke mid-conversation when the user
|
| 4 |
+
mentions symptoms. Queries the Neo4j PatternDetector for relevant
|
| 5 |
+
correlations, temporal clusters, and frequency changes, returning
|
| 6 |
+
structured insight text the LLM can weave into its response.
|
| 7 |
+
|
| 8 |
+
Examples:
|
| 9 |
+
- User says "I have a headache" → LLM calls this to check for patterns
|
| 10 |
+
- User says "I forgot my medication" → LLM checks missed-dose impact
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
from __future__ import annotations
|
| 14 |
+
|
| 15 |
+
import logging
|
| 16 |
+
from typing import Any, Dict
|
| 17 |
+
|
| 18 |
+
from reachy_mini_conversation_app.tools.core_tools import Tool, ToolDependencies
|
| 19 |
+
|
| 20 |
+
logger = logging.getLogger(__name__)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class CheckHealthPatternsTool(Tool):
|
| 24 |
+
"""Check for relevant health patterns in the knowledge graph."""
|
| 25 |
+
|
| 26 |
+
name = "check_health_patterns"
|
| 27 |
+
description = (
|
| 28 |
+
"Check the knowledge graph for health patterns relevant to what the user "
|
| 29 |
+
"just mentioned. Call this when the user reports a symptom (headache, pain, "
|
| 30 |
+
"fatigue, etc.) or mentions missing medication. Returns observational insights "
|
| 31 |
+
"about correlations and temporal patterns. Use the returned insights naturally "
|
| 32 |
+
"in conversation — never state them as medical facts."
|
| 33 |
+
)
|
| 34 |
+
parameters_schema = {
|
| 35 |
+
"type": "object",
|
| 36 |
+
"properties": {
|
| 37 |
+
"symptom_or_context": {
|
| 38 |
+
"type": "string",
|
| 39 |
+
"description": (
|
| 40 |
+
"What the user mentioned (e.g., 'headache', 'missed medication', "
|
| 41 |
+
"'feeling tired'). Used to filter relevant patterns."
|
| 42 |
+
),
|
| 43 |
+
},
|
| 44 |
+
},
|
| 45 |
+
"required": ["symptom_or_context"],
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
async def __call__(
|
| 49 |
+
self,
|
| 50 |
+
deps: ToolDependencies,
|
| 51 |
+
symptom_or_context: str = "",
|
| 52 |
+
**kwargs: Any,
|
| 53 |
+
) -> Dict[str, Any]:
|
| 54 |
+
graph = deps.graph_memory
|
| 55 |
+
if not graph or not getattr(graph, "is_connected", False):
|
| 56 |
+
logger.debug("Neo4j not available for pattern check")
|
| 57 |
+
return {"patterns_found": 0, "insights": []}
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
from reachy_mini_conversation_app.pattern_detector import PatternDetector
|
| 61 |
+
|
| 62 |
+
detector = PatternDetector(graph)
|
| 63 |
+
all_insights = detector.run_analysis()
|
| 64 |
+
|
| 65 |
+
if not all_insights:
|
| 66 |
+
return {"patterns_found": 0, "insights": []}
|
| 67 |
+
|
| 68 |
+
# Filter for relevance to the mentioned symptom/context
|
| 69 |
+
keyword = symptom_or_context.lower()
|
| 70 |
+
relevant = []
|
| 71 |
+
for insight in all_insights:
|
| 72 |
+
summary_lower = insight.summary.lower()
|
| 73 |
+
detail_lower = (insight.detail or "").lower()
|
| 74 |
+
# Include if the insight mentions the keyword or is high-confidence
|
| 75 |
+
if (
|
| 76 |
+
keyword in summary_lower
|
| 77 |
+
or keyword in detail_lower
|
| 78 |
+
or insight.confidence >= 0.7
|
| 79 |
+
):
|
| 80 |
+
relevant.append(
|
| 81 |
+
{
|
| 82 |
+
"pattern_type": insight.pattern_type,
|
| 83 |
+
"summary": insight.summary,
|
| 84 |
+
"detail": insight.detail,
|
| 85 |
+
"confidence": insight.confidence,
|
| 86 |
+
}
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Cap at 3 most relevant to keep response concise
|
| 90 |
+
relevant = relevant[:3]
|
| 91 |
+
|
| 92 |
+
if relevant:
|
| 93 |
+
logger.info(
|
| 94 |
+
"Pattern check for '%s': %d relevant insights",
|
| 95 |
+
symptom_or_context,
|
| 96 |
+
len(relevant),
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
return {
|
| 100 |
+
"patterns_found": len(relevant),
|
| 101 |
+
"insights": relevant,
|
| 102 |
+
"guidance": (
|
| 103 |
+
"Share these observations naturally and conversationally. "
|
| 104 |
+
"Use language like 'I've noticed...' or 'It seems like...'. "
|
| 105 |
+
"Never state patterns as medical facts or give medical advice."
|
| 106 |
+
),
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
except Exception as e:
|
| 110 |
+
logger.warning("Pattern check failed: %s", e)
|
| 111 |
+
return {"patterns_found": 0, "insights": []}
|
src/reachy_mini_conversation_app/tools/check_medication.py
CHANGED
|
@@ -69,6 +69,13 @@ class CheckMedicationTool(Tool):
|
|
| 69 |
await self._emit_med_status_ui(deps, conv_log_result.get("message", ""))
|
| 70 |
return conv_log_result
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
# No record found — still show the UI
|
| 73 |
result = self._format_no_record(medication_name, time_of_day, deps)
|
| 74 |
await self._emit_med_status_ui(deps, result.get("message", ""))
|
|
@@ -121,7 +128,9 @@ class CheckMedicationTool(Tool):
|
|
| 121 |
patient_name = "Patient" # Default
|
| 122 |
if deps.database:
|
| 123 |
profile = deps.database.get_or_create_profile()
|
| 124 |
-
patient_name = profile.get("
|
|
|
|
|
|
|
| 125 |
|
| 126 |
result = graph.check_medication_today(
|
| 127 |
patient_name=patient_name,
|
|
@@ -227,6 +236,7 @@ class CheckMedicationTool(Tool):
|
|
| 227 |
return {
|
| 228 |
"logged": True,
|
| 229 |
"message": message,
|
|
|
|
| 230 |
"details": result,
|
| 231 |
}
|
| 232 |
|
|
@@ -256,9 +266,72 @@ class CheckMedicationTool(Tool):
|
|
| 256 |
return {
|
| 257 |
"logged": True,
|
| 258 |
"message": message,
|
|
|
|
| 259 |
"details": {"source": "conversation_log", "turn": turn},
|
| 260 |
}
|
| 261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
def _format_no_record(
|
| 263 |
self,
|
| 264 |
medication_name: Optional[str],
|
|
@@ -287,6 +360,7 @@ class CheckMedicationTool(Tool):
|
|
| 287 |
"What medication do you usually take? I can help you set it up "
|
| 288 |
"so I can remind you."
|
| 289 |
),
|
|
|
|
| 290 |
"guidance": "setup_medication",
|
| 291 |
"next_action": "Ask about their regular medications",
|
| 292 |
"details": {"reason": "no_medications_configured"},
|
|
@@ -305,6 +379,7 @@ class CheckMedicationTool(Tool):
|
|
| 305 |
return {
|
| 306 |
"logged": False,
|
| 307 |
"message": message,
|
|
|
|
| 308 |
"guidance": "log_medication",
|
| 309 |
"details": {},
|
| 310 |
}
|
|
|
|
| 69 |
await self._emit_med_status_ui(deps, conv_log_result.get("message", ""))
|
| 70 |
return conv_log_result
|
| 71 |
|
| 72 |
+
# Fall back to SQLite medication_entries (the source of truth for logged meds)
|
| 73 |
+
if deps.database:
|
| 74 |
+
db_result = self._check_database(deps, medication_name, time_of_day)
|
| 75 |
+
if db_result.get("logged"):
|
| 76 |
+
await self._emit_med_status_ui(deps, db_result.get("message", ""))
|
| 77 |
+
return db_result
|
| 78 |
+
|
| 79 |
# No record found — still show the UI
|
| 80 |
result = self._format_no_record(medication_name, time_of_day, deps)
|
| 81 |
await self._emit_med_status_ui(deps, result.get("message", ""))
|
|
|
|
| 128 |
patient_name = "Patient" # Default
|
| 129 |
if deps.database:
|
| 130 |
profile = deps.database.get_or_create_profile()
|
| 131 |
+
patient_name = profile.get("display_name") or profile.get(
|
| 132 |
+
"name", "Patient"
|
| 133 |
+
)
|
| 134 |
|
| 135 |
result = graph.check_medication_today(
|
| 136 |
patient_name=patient_name,
|
|
|
|
| 236 |
return {
|
| 237 |
"logged": True,
|
| 238 |
"message": message,
|
| 239 |
+
"voice_instruction": "Speak the message aloud to the user while they view the medication status on screen.",
|
| 240 |
"details": result,
|
| 241 |
}
|
| 242 |
|
|
|
|
| 266 |
return {
|
| 267 |
"logged": True,
|
| 268 |
"message": message,
|
| 269 |
+
"voice_instruction": "Speak the message aloud to the user while they view the medication status on screen.",
|
| 270 |
"details": {"source": "conversation_log", "turn": turn},
|
| 271 |
}
|
| 272 |
|
| 273 |
+
def _check_database(
|
| 274 |
+
self,
|
| 275 |
+
deps: ToolDependencies,
|
| 276 |
+
medication_name: Optional[str],
|
| 277 |
+
time_of_day: Optional[str],
|
| 278 |
+
) -> Dict[str, Any]:
|
| 279 |
+
"""Check SQLite medication_entries via get_todays_medication_status."""
|
| 280 |
+
try:
|
| 281 |
+
today_status = deps.database.get_todays_medication_status()
|
| 282 |
+
scheduled = today_status.get("scheduled", [])
|
| 283 |
+
ad_hoc = today_status.get("ad_hoc", [])
|
| 284 |
+
|
| 285 |
+
# Collect all logged entries
|
| 286 |
+
logged_entries = []
|
| 287 |
+
for entry in scheduled:
|
| 288 |
+
if entry.get("status") == "logged":
|
| 289 |
+
if medication_name:
|
| 290 |
+
if entry.get("name", "").lower() == medication_name.lower():
|
| 291 |
+
logged_entries.append(entry)
|
| 292 |
+
else:
|
| 293 |
+
logged_entries.append(entry)
|
| 294 |
+
for entry in ad_hoc:
|
| 295 |
+
if medication_name:
|
| 296 |
+
if entry.get("name", "").lower() == medication_name.lower():
|
| 297 |
+
logged_entries.append(entry)
|
| 298 |
+
else:
|
| 299 |
+
logged_entries.append(entry)
|
| 300 |
+
|
| 301 |
+
if not logged_entries:
|
| 302 |
+
return {"logged": False}
|
| 303 |
+
|
| 304 |
+
# Build factual message
|
| 305 |
+
med_phrase = medication_name or "your medication"
|
| 306 |
+
if time_of_day:
|
| 307 |
+
med_phrase = f"your {time_of_day} medication"
|
| 308 |
+
|
| 309 |
+
if len(logged_entries) == 1:
|
| 310 |
+
logged_at = logged_entries[0].get("logged_at", "earlier today")
|
| 311 |
+
message = f"Yes, I have a record that you took {med_phrase} at {logged_at} today."
|
| 312 |
+
else:
|
| 313 |
+
times = [e.get("logged_at", "earlier") for e in logged_entries]
|
| 314 |
+
names = [e.get("name", "medication") for e in logged_entries]
|
| 315 |
+
if medication_name:
|
| 316 |
+
message = (
|
| 317 |
+
f"I have {len(logged_entries)} records of you taking "
|
| 318 |
+
f"{med_phrase} today."
|
| 319 |
+
)
|
| 320 |
+
else:
|
| 321 |
+
med_list = ", ".join(f"{n} at {t}" for n, t in zip(names, times))
|
| 322 |
+
message = f"Yes, I have records of you taking: {med_list}."
|
| 323 |
+
|
| 324 |
+
return {
|
| 325 |
+
"logged": True,
|
| 326 |
+
"message": message,
|
| 327 |
+
"voice_instruction": "Speak the message aloud to the user while they view the medication status on screen.",
|
| 328 |
+
"details": {"source": "database", "entries": logged_entries},
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
except Exception as e:
|
| 332 |
+
logger.warning("Database medication check failed: %s", e)
|
| 333 |
+
return {"logged": False}
|
| 334 |
+
|
| 335 |
def _format_no_record(
|
| 336 |
self,
|
| 337 |
medication_name: Optional[str],
|
|
|
|
| 360 |
"What medication do you usually take? I can help you set it up "
|
| 361 |
"so I can remind you."
|
| 362 |
),
|
| 363 |
+
"voice_instruction": "Speak the message aloud to the user.",
|
| 364 |
"guidance": "setup_medication",
|
| 365 |
"next_action": "Ask about their regular medications",
|
| 366 |
"details": {"reason": "no_medications_configured"},
|
|
|
|
| 379 |
return {
|
| 380 |
"logged": False,
|
| 381 |
"message": message,
|
| 382 |
+
"voice_instruction": "Speak the message aloud to the user while they view the medication status on screen.",
|
| 383 |
"guidance": "log_medication",
|
| 384 |
"details": {},
|
| 385 |
}
|
tests/test_wakeword_and_end_session.py
CHANGED
|
@@ -102,8 +102,8 @@ class TestWakewordDetector:
|
|
| 102 |
|
| 103 |
detector = WakewordDetector()
|
| 104 |
assert detector.model_name == "hey_reachy"
|
| 105 |
-
assert detector.threshold == 0.
|
| 106 |
-
assert detector.enabled is
|
| 107 |
|
| 108 |
def test_disabled_returns_false(self):
|
| 109 |
"""When disabled, feed() should always return False."""
|
|
@@ -176,8 +176,8 @@ class TestWakewordDetector:
|
|
| 176 |
def test_feed_triggers_on_sustained_high_confidence(self):
|
| 177 |
"""Sustained high-confidence predictions should trigger detection.
|
| 178 |
|
| 179 |
-
With _PATIENCE_FRAMES=
|
| 180 |
-
|
| 181 |
"""
|
| 182 |
from reachy_mini_conversation_app.wakeword_detector import (
|
| 183 |
WakewordDetector,
|
|
@@ -186,7 +186,7 @@ class TestWakewordDetector:
|
|
| 186 |
from collections import defaultdict, deque
|
| 187 |
from functools import partial
|
| 188 |
|
| 189 |
-
detector = WakewordDetector(threshold=0.5)
|
| 190 |
mock_model = MagicMock()
|
| 191 |
mock_model.predict.return_value = {"hey_reachy": 0.95}
|
| 192 |
# Pre-populate prediction_buffer with enough consecutive high scores
|
|
@@ -213,7 +213,7 @@ class TestWakewordDetector:
|
|
| 213 |
from collections import defaultdict, deque
|
| 214 |
from functools import partial
|
| 215 |
|
| 216 |
-
detector = WakewordDetector(threshold=0.5)
|
| 217 |
mock_model = MagicMock()
|
| 218 |
# Simulate: one high score, then low
|
| 219 |
mock_model.predict.side_effect = [
|
|
@@ -230,11 +230,8 @@ class TestWakewordDetector:
|
|
| 230 |
audio = np.random.randint(-5000, 5000, 1280 * 3, dtype=np.int16)
|
| 231 |
result = detector.feed(audio, 16000)
|
| 232 |
|
| 233 |
-
# The spike should NOT trigger because
|
| 234 |
-
#
|
| 235 |
-
# and we only have 1.
|
| 236 |
-
# Note: with mock, prediction_buffer isn't auto-populated by predict(),
|
| 237 |
-
# so the first spike will see an empty buffer and be blocked.
|
| 238 |
# Verify predict was called WITHOUT patience kwargs
|
| 239 |
call_kwargs = mock_model.predict.call_args_list[0]
|
| 240 |
assert len(call_kwargs[1]) == 0 # no keyword args (no patience/threshold)
|
|
@@ -243,14 +240,14 @@ class TestWakewordDetector:
|
|
| 243 |
"""Input at 48kHz should be resampled to 16kHz internally."""
|
| 244 |
from reachy_mini_conversation_app.wakeword_detector import WakewordDetector
|
| 245 |
|
| 246 |
-
detector = WakewordDetector()
|
| 247 |
mock_model = MagicMock()
|
| 248 |
mock_model.predict.return_value = {"hey_reachy": 0.0}
|
| 249 |
detector._model = mock_model
|
| 250 |
detector._initialised = True
|
| 251 |
|
| 252 |
# 4800 samples at 48kHz → 1600 samples at 16kHz (> 1280 chunk)
|
| 253 |
-
# Use loud audio to pass the RMS energy gate
|
| 254 |
audio_48k = np.random.randint(-5000, 5000, 4800, dtype=np.int16)
|
| 255 |
detector.feed(audio_48k, 48000)
|
| 256 |
# Model should have been called with prediction
|
|
|
|
| 102 |
|
| 103 |
detector = WakewordDetector()
|
| 104 |
assert detector.model_name == "hey_reachy"
|
| 105 |
+
assert detector.threshold == 0.7
|
| 106 |
+
assert detector.enabled is False
|
| 107 |
|
| 108 |
def test_disabled_returns_false(self):
|
| 109 |
"""When disabled, feed() should always return False."""
|
|
|
|
| 176 |
def test_feed_triggers_on_sustained_high_confidence(self):
|
| 177 |
"""Sustained high-confidence predictions should trigger detection.
|
| 178 |
|
| 179 |
+
With _PATIENCE_FRAMES=1, the model's prediction_buffer must contain
|
| 180 |
+
at least 1 above-threshold score before detection fires.
|
| 181 |
"""
|
| 182 |
from reachy_mini_conversation_app.wakeword_detector import (
|
| 183 |
WakewordDetector,
|
|
|
|
| 186 |
from collections import defaultdict, deque
|
| 187 |
from functools import partial
|
| 188 |
|
| 189 |
+
detector = WakewordDetector(threshold=0.5, enabled=True)
|
| 190 |
mock_model = MagicMock()
|
| 191 |
mock_model.predict.return_value = {"hey_reachy": 0.95}
|
| 192 |
# Pre-populate prediction_buffer with enough consecutive high scores
|
|
|
|
| 213 |
from collections import defaultdict, deque
|
| 214 |
from functools import partial
|
| 215 |
|
| 216 |
+
detector = WakewordDetector(threshold=0.5, enabled=True)
|
| 217 |
mock_model = MagicMock()
|
| 218 |
# Simulate: one high score, then low
|
| 219 |
mock_model.predict.side_effect = [
|
|
|
|
| 230 |
audio = np.random.randint(-5000, 5000, 1280 * 3, dtype=np.int16)
|
| 231 |
result = detector.feed(audio, 16000)
|
| 232 |
|
| 233 |
+
# The spike should NOT trigger because the prediction_buffer is empty
|
| 234 |
+
# (mock doesn't auto-populate it), so the consecutive-hit check blocks.
|
|
|
|
|
|
|
|
|
|
| 235 |
# Verify predict was called WITHOUT patience kwargs
|
| 236 |
call_kwargs = mock_model.predict.call_args_list[0]
|
| 237 |
assert len(call_kwargs[1]) == 0 # no keyword args (no patience/threshold)
|
|
|
|
| 240 |
"""Input at 48kHz should be resampled to 16kHz internally."""
|
| 241 |
from reachy_mini_conversation_app.wakeword_detector import WakewordDetector
|
| 242 |
|
| 243 |
+
detector = WakewordDetector(enabled=True)
|
| 244 |
mock_model = MagicMock()
|
| 245 |
mock_model.predict.return_value = {"hey_reachy": 0.0}
|
| 246 |
detector._model = mock_model
|
| 247 |
detector._initialised = True
|
| 248 |
|
| 249 |
# 4800 samples at 48kHz → 1600 samples at 16kHz (> 1280 chunk)
|
| 250 |
+
# Use loud audio to pass the RMS energy gate (> _MIN_RMS_ENERGY)
|
| 251 |
audio_48k = np.random.randint(-5000, 5000, 4800, dtype=np.int16)
|
| 252 |
detector.feed(audio_48k, 48000)
|
| 253 |
# Model should have been called with prediction
|