Phase 2: Chat & AI Core — Claude API streaming, chat UI, admin context
Backend: - Chat, Message, ContextFile models + Alembic migration - Chat CRUD with per-user limit enforcement (max_chats) - SSE streaming endpoint: saves user message, streams Claude response, saves assistant message with token usage metadata - Context assembly: primary context file + conversation history - Admin context CRUD (GET/PUT with version tracking) - Anthropic SDK integration with async streaming - Chat ownership isolation (users can't access each other's chats) Frontend: - Chat page with sidebar chat list + main chat window - Real-time SSE streaming via fetch + ReadableStream - Message bubbles with Markdown rendering (react-markdown) - Auto-growing message input (Enter to send, Shift+Enter newline) - Zustand chat store for streaming state management - Admin primary context editor with unsaved changes warning - Updated routing: /chat, /chat/:chatId, /admin/context - Enabled Chat and Admin sidebar navigation - English + Russian translations for all new UI Infrastructure: - nginx: disabled proxy buffering for SSE support - Added ANTHROPIC_API_KEY and CLAUDE_MODEL to config Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
44
backend/app/services/context_service.py
Normal file
44
backend/app/services/context_service.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import uuid
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.context_file import ContextFile
|
||||
|
||||
DEFAULT_SYSTEM_PROMPT = """You are a personal AI health assistant. Your role is to:
|
||||
- Help users understand their health data and medical documents
|
||||
- Provide health-related recommendations based on uploaded information
|
||||
- Schedule reminders for checkups, medications, and health-related activities
|
||||
- Compile health summaries when requested
|
||||
- Answer health questions clearly and compassionately
|
||||
|
||||
Always be empathetic, accurate, and clear. When uncertain, recommend consulting a healthcare professional.
|
||||
You can communicate in English and Russian based on the user's preference."""
|
||||
|
||||
|
||||
async def get_primary_context(db: AsyncSession) -> ContextFile | None:
|
||||
result = await db.execute(
|
||||
select(ContextFile).where(ContextFile.type == "primary", ContextFile.user_id.is_(None))
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def upsert_primary_context(
|
||||
db: AsyncSession, content: str, admin_user_id: uuid.UUID
|
||||
) -> ContextFile:
|
||||
ctx = await get_primary_context(db)
|
||||
if ctx:
|
||||
ctx.content = content
|
||||
ctx.version = ctx.version + 1
|
||||
ctx.updated_by = admin_user_id
|
||||
else:
|
||||
ctx = ContextFile(
|
||||
type="primary",
|
||||
user_id=None,
|
||||
content=content,
|
||||
version=1,
|
||||
updated_by=admin_user_id,
|
||||
)
|
||||
db.add(ctx)
|
||||
await db.flush()
|
||||
return ctx
|
||||
Reference in New Issue
Block a user