Files
hermes-agent/agent/crisis_hook.py
Alexander Whitestone e66156055f
Some checks failed
Docker Build and Publish / build-and-push (pull_request) Has been skipped
Nix / nix (ubuntu-latest) (pull_request) Failing after 13s
Contributor Attribution Check / check-attribution (pull_request) Failing after 49s
Supply Chain Audit / Scan PR for supply chain risks (pull_request) Successful in 1m11s
Tests / e2e (pull_request) Successful in 4m42s
Tests / test (pull_request) Failing after 59m1s
Nix / nix (macos-latest) (pull_request) Has been cancelled
feat(crisis): integrate 988 Lifeline into conversation loop
Fixes #695

- Add agent/crisis_resources.py — 988 Lifeline detection patterns and
  resource data (phone, text, chat, Spanish, Veterans Crisis Line)
- Add agent/crisis_hook.py — lightweight check_crisis() hook function
- Add agent/crisis_middleware.py — CrisisMiddleware class, global
  check_crisis() convenience function, and @crisis_aware decorator
- Integrate in run_agent.py: AIAgent.run_conversation() now calls
  check_crisis() immediately after input sanitization; crisis messages
  return 988 Lifeline resources before the LLM is invoked (api_calls=0,
  crisis_intercepted=True in result dict)
- Add tests/agent/test_crisis_integration.py — 47 tests covering
  detection patterns, response content, middleware class, hook function,
  convenience function, decorator, resource data integrity, and
  end-to-end integration path

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-15 07:57:44 -04:00

31 lines
1.1 KiB
Python

"""
Crisis Detection Hook — Integrates 988 Lifeline into the agent conversation loop.
Call check_crisis() before processing user messages. If crisis is detected,
the 988 Lifeline resources are prepended to the response and the agent
responds with empathy rather than processing the original request.
Usage in conversation loop:
from agent.crisis_hook import check_crisis
crisis_response = check_crisis(user_message)
if crisis_response:
return crisis_response # Skip normal processing
"""
from agent.crisis_resources import should_trigger_crisis_response, get_crisis_response
def check_crisis(user_message: str) -> str | None:
"""
Check if user message contains crisis signals.
Returns the crisis response string if crisis detected, None otherwise.
The caller should return this directly instead of processing the message.
"""
should_trigger, detection = should_trigger_crisis_response(user_message)
if not should_trigger:
return None
return get_crisis_response(detection.get("severity_label", "CRITICAL"))