mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 20:42:43 +00:00
The route gates on requireAuth (cookie or bearer), enforces the daily token budget, runs the prompt-injection / off-topic refusal scanner, and only then walks the user's provider chain. The conversation is created via withIdempotency() when no conversationId is supplied, so a retry under the same Idempotency-Key replays the original assistant reply rather than spinning up a second thread. Existing-thread turns skip the wrap. Replies stream as SSE: one "token" frame per word-sized chunk, then a "provenance" frame describing the windows + metrics the assistant could see, then a "done" frame carrying the persisted message id. Provider failure cascades to a single "error" frame and the assistant message is never persisted. Conversation history is capped at 20 turns in the prompt window via a synthetic summary placeholder so cost stays bounded. `buildCoachSnapshot()` reuses the analytics features pipeline so the Coach narrates the same numbers the dashboard tiles render. `getCoachSystemPrompt()` extends the strict-insight ground rules with conversational framing while keeping the zero-hallucination rule. 5 integration tests pin the round-trip, refusal short-circuit, budget 429, foreign-conversation 404, and validation 422 paths against the postgres testcontainer. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>