Skip to main content

Chatbot — Pastoral Care Response Integration Plan

Summary

Create pastoral-care-library.ts with care intent detection and context loading. Insert ~60 lines in route.ts to intercept pastoral care escalation and serve from pre-built patterns via Haiku instead of Sonnet. Sonnet path preserved as fallback.

Changes (2 files)

1. New file: src/lib/pastoral-care-library.ts

Must start with import 'server-only' (CLAUDE.md client/server boundary rule).

Function 1: detectCareIntent(message, history?)

  • Returns { category, subcategory } | null
  • Ordered array of { patterns: RegExp[], category, subcategory } entries
  • Most specific first (suicidal ideation → DV → miscarriage → grief → addiction → divorce → illness → spiritual)
  • Returns null if no pattern matches (falls through to Sonnet)

Function 2: loadCareLibraryContext(category, subcategory, denomination)

  • CRITICAL FIX from review: Use church.denominationDENOMINATION_TO_TRADITION_KEY mapping, NOT lensId
  • Port DENOMINATION_TO_TRADITION_KEY from Python module to TypeScript
  • Two parallel Supabase queries:
    1. pastoral_care_responses filtered by category + subcategory + tradition
    2. tradition_care_context by tradition_key
  • Returns null if 0 rows (triggers Sonnet fallback)

Function 3: buildCareSystemPrompt(params)

  • Assembles: tradition context_prompt + grouped responses (openers, followups, articulations, bridges, avoids) + safety resources + graceful fallback phrase
  • Uses church's actual clergy title and pastor name in bridge language

2. Modified: src/app/api/chatbot/stream/route.ts

Insert ~60 lines after shouldEscalate() (line 1542), before LLM loop (line 1553).

CRITICAL from review: Do NOT modify shouldEscalate(). Introduce separate useCareLibrary flag:

if (escalate) {
careIntent = detectCareIntent(message, history);
if (careIntent) {
careLibraryResult = await loadCareLibraryContext(
careIntent.category, careIntent.subcategory, church.denomination
);
}
}

if (escalate && careLibraryResult?.responses.length > 0) {
// Care library path: Haiku + pre-built responses
// escalate: false forces Haiku, not Sonnet
// Preserve: trackUsage, logQuestion, cacheResponse, moderation
// Apply: stripEmoji, SAFETY_PATTERNS check, auto-flag
// New source type: 'llm_care_library'
// New cascade tier: 'llm_care_library'
}
// FALLBACK: falls through to existing Sonnet path unchanged

Critical issues from review

  1. C1: Do NOT use lensId → tradition_key mapping — lossy (lens 4 covers PCA, PCUSA, CRC). Use church.denominationDENOMINATION_TO_TRADITION_KEY with universal fallback.
  2. C3: Empty table state must be transparent — 0 rows in pastoral_care_responses → null → Sonnet fallback. Zero behavioral change.
  3. I1: Do NOT modify shouldEscalate() — introduce separate flag.
  4. I2: Preserve all tracking — usage, logging, caching, moderation must run on care library path.
  5. I3: Use existing moderation infrastructurecheckRestriction, logViolation, not a parallel system.

Performance target

  • Current pastoral care: 10-15 seconds (Haiku tool round + Sonnet text)
  • With care library: sub-2 seconds (Haiku + pre-built patterns)
  • Empty table fallback: identical to current (Sonnet path)