Compare commits
2 Commits
fix/format
...
feat/673
| Author | SHA1 | Date | |
|---|---|---|---|
| 34e7de6a4c | |||
| dbabe0e6ae |
149
agent/crisis_resources.py
Normal file
149
agent/crisis_resources.py
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
"""
|
||||||
|
988 Suicide & Crisis Lifeline Integration (#673).
|
||||||
|
|
||||||
|
When crisis is detected, provides immediate access to help:
|
||||||
|
- Phone: 988 (call or text)
|
||||||
|
- Text: Text HOME to 988
|
||||||
|
- Chat: 988lifeline.org/chat
|
||||||
|
- Spanish: 1-888-628-9454
|
||||||
|
- Emergency: 911
|
||||||
|
|
||||||
|
This module provides the resource data. agent/crisis_protocol.py
|
||||||
|
handles detection. This module formats the resources for display.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class CrisisResource:
|
||||||
|
"""A crisis support contact method."""
|
||||||
|
name: str
|
||||||
|
contact: str
|
||||||
|
description: str
|
||||||
|
url: str = ""
|
||||||
|
available: str = "24/7"
|
||||||
|
language: str = "English"
|
||||||
|
|
||||||
|
|
||||||
|
# 988 Suicide & Crisis Lifeline — all channels
|
||||||
|
LIFELINE_988 = CrisisResource(
|
||||||
|
name="988 Suicide and Crisis Lifeline",
|
||||||
|
contact="Call or text 988",
|
||||||
|
description="Free, confidential support for people in suicidal crisis or emotional distress.",
|
||||||
|
url="https://988lifeline.org",
|
||||||
|
available="24/7",
|
||||||
|
language="English",
|
||||||
|
)
|
||||||
|
|
||||||
|
LIFELINE_988_TEXT = CrisisResource(
|
||||||
|
name="988 Crisis Text Line",
|
||||||
|
contact="Text HOME to 988",
|
||||||
|
description="Free, 24/7 crisis support via text message.",
|
||||||
|
url="",
|
||||||
|
available="24/7",
|
||||||
|
language="English",
|
||||||
|
)
|
||||||
|
|
||||||
|
LIFELINE_988_CHAT = CrisisResource(
|
||||||
|
name="988 Lifeline Chat",
|
||||||
|
contact="988lifeline.org/chat",
|
||||||
|
description="Free, confidential online chat with a trained crisis counselor.",
|
||||||
|
url="https://988lifeline.org/chat",
|
||||||
|
available="24/7",
|
||||||
|
language="English",
|
||||||
|
)
|
||||||
|
|
||||||
|
LIFELINE_988_SPANISH = CrisisResource(
|
||||||
|
name="988 Lifeline (Spanish)",
|
||||||
|
contact="1-888-628-9454",
|
||||||
|
description="Línea de prevención del suicidio en español.",
|
||||||
|
url="https://988lifeline.org/help-yourself/en-espanol/",
|
||||||
|
available="24/7",
|
||||||
|
language="Spanish",
|
||||||
|
)
|
||||||
|
|
||||||
|
CRISIS_TEXT_LINE = CrisisResource(
|
||||||
|
name="Crisis Text Line",
|
||||||
|
contact="Text HOME to 741741",
|
||||||
|
description="Free, 24/7 crisis support via text message.",
|
||||||
|
url="https://www.crisistextline.org",
|
||||||
|
available="24/7",
|
||||||
|
language="English",
|
||||||
|
)
|
||||||
|
|
||||||
|
EMERGENCY_911 = CrisisResource(
|
||||||
|
name="Emergency Services",
|
||||||
|
contact="911",
|
||||||
|
description="Immediate danger — police, fire, ambulance.",
|
||||||
|
url="",
|
||||||
|
available="24/7",
|
||||||
|
language="Any",
|
||||||
|
)
|
||||||
|
|
||||||
|
# All resources in priority order
|
||||||
|
ALL_RESOURCES: List[CrisisResource] = [
|
||||||
|
EMERGENCY_911,
|
||||||
|
LIFELINE_988,
|
||||||
|
LIFELINE_988_TEXT,
|
||||||
|
LIFELINE_988_CHAT,
|
||||||
|
CRISIS_TEXT_LINE,
|
||||||
|
LIFELINE_988_SPANISH,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def get_crisis_resources(language: str = None) -> List[CrisisResource]:
|
||||||
|
"""Get crisis resources, optionally filtered by language.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
language: Filter by language ("English", "Spanish", or None for all)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List of CrisisResource objects
|
||||||
|
"""
|
||||||
|
if language:
|
||||||
|
return [r for r in ALL_RESOURCES if r.language.lower() == language.lower()]
|
||||||
|
return ALL_RESOURCES
|
||||||
|
|
||||||
|
|
||||||
|
def format_crisis_resources(resources: List[CrisisResource] = None) -> str:
|
||||||
|
"""Format crisis resources as a user-facing message.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
resources: List of resources to format. Defaults to all resources.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Formatted string suitable for displaying to a user in crisis.
|
||||||
|
"""
|
||||||
|
if resources is None:
|
||||||
|
resources = ALL_RESOURCES
|
||||||
|
|
||||||
|
lines = ["**Please reach out — help is available right now:**
|
||||||
|
"]
|
||||||
|
|
||||||
|
for r in resources:
|
||||||
|
if r.url:
|
||||||
|
lines.append(f"- **{r.name}:** {r.contact} ({r.url})")
|
||||||
|
else:
|
||||||
|
lines.append(f"- **{r.name}:** {r.contact}")
|
||||||
|
|
||||||
|
lines.append("")
|
||||||
|
lines.append("All services are free, confidential, and available 24/7.")
|
||||||
|
lines.append("You are not alone.")
|
||||||
|
|
||||||
|
return "
|
||||||
|
".join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
def get_immediate_help_message() -> str:
|
||||||
|
"""Get the most urgent crisis help message.
|
||||||
|
|
||||||
|
Used when crisis is detected at CRITICAL level.
|
||||||
|
"""
|
||||||
|
return (
|
||||||
|
"If you are in immediate danger, call **911** right now.
|
||||||
|
|
||||||
|
"
|
||||||
|
+ format_crisis_resources()
|
||||||
|
)
|
||||||
79
tests/test_crisis_resources.py
Normal file
79
tests/test_crisis_resources.py
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
"""Tests for 988 Crisis Lifeline integration (#673)."""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from agent.crisis_resources import (
|
||||||
|
LIFELINE_988,
|
||||||
|
LIFELINE_988_TEXT,
|
||||||
|
LIFELINE_988_CHAT,
|
||||||
|
LIFELINE_988_SPANISH,
|
||||||
|
CRISIS_TEXT_LINE,
|
||||||
|
EMERGENCY_911,
|
||||||
|
ALL_RESOURCES,
|
||||||
|
get_crisis_resources,
|
||||||
|
format_crisis_resources,
|
||||||
|
get_immediate_help_message,
|
||||||
|
CrisisResource,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestCrisisResources:
|
||||||
|
def test_988_phone(self):
|
||||||
|
assert "988" in LIFELINE_988.contact
|
||||||
|
assert "24/7" in LIFELINE_988.available
|
||||||
|
|
||||||
|
def test_988_text(self):
|
||||||
|
assert "HOME" in LIFELINE_988_TEXT.contact
|
||||||
|
assert "988" in LIFELINE_988_TEXT.contact
|
||||||
|
|
||||||
|
def test_988_chat(self):
|
||||||
|
assert "988lifeline.org/chat" in LIFELINE_988_CHAT.url
|
||||||
|
|
||||||
|
def test_988_spanish(self):
|
||||||
|
assert "1-888-628-9454" in LIFELINE_988_SPANISH.contact
|
||||||
|
assert LIFELINE_988_SPANISH.language == "Spanish"
|
||||||
|
|
||||||
|
def test_crisis_text_line(self):
|
||||||
|
assert "741741" in CRISIS_TEXT_LINE.contact
|
||||||
|
|
||||||
|
def test_911(self):
|
||||||
|
assert "911" in EMERGENCY_911.contact
|
||||||
|
|
||||||
|
def test_all_resources_not_empty(self):
|
||||||
|
assert len(ALL_RESOURCES) >= 5
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetResources:
|
||||||
|
def test_returns_all_by_default(self):
|
||||||
|
assert len(get_crisis_resources()) == len(ALL_RESOURCES)
|
||||||
|
|
||||||
|
def test_filter_english(self):
|
||||||
|
english = get_crisis_resources("English")
|
||||||
|
assert all(r.language == "English" for r in english)
|
||||||
|
assert len(english) > 0
|
||||||
|
|
||||||
|
def test_filter_spanish(self):
|
||||||
|
spanish = get_crisis_resources("Spanish")
|
||||||
|
assert len(spanish) >= 1
|
||||||
|
assert all(r.language == "Spanish" for r in spanish)
|
||||||
|
|
||||||
|
|
||||||
|
class TestFormatting:
|
||||||
|
def test_format_includes_988(self):
|
||||||
|
msg = format_crisis_resources()
|
||||||
|
assert "988" in msg
|
||||||
|
|
||||||
|
def test_format_includes_741741(self):
|
||||||
|
msg = format_crisis_resources()
|
||||||
|
assert "741741" in msg
|
||||||
|
|
||||||
|
def test_format_includes_911(self):
|
||||||
|
msg = format_crisis_resources()
|
||||||
|
assert "911" in msg
|
||||||
|
|
||||||
|
def test_immediate_help_includes_911_first(self):
|
||||||
|
msg = get_immediate_help_message()
|
||||||
|
assert msg.startswith("If you are in immediate danger")
|
||||||
|
|
||||||
|
def test_format_not_empty(self):
|
||||||
|
msg = format_crisis_resources()
|
||||||
|
assert len(msg) > 100
|
||||||
Reference in New Issue
Block a user