File size: 2,864 Bytes
a0c836f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Lightweight dev harness for the medication-adherence layer.

Runs ONLY the new feature routers (accounts, schedule, doses, gamification,
caregiver) on SQLite — without loading the heavy AI chat pipeline. Use this to
demo/test the new features when langchain/chromadb/Postgres/API keys aren't set
up. The real production entry point remains `main.py` (uvicorn main:app).

    uvicorn run_adherence:app --reload
"""

import os
import sys

# Make console output UTF-8 safe on Windows (avoids emoji print crashes).
try:
    sys.stdout.reconfigure(encoding="utf-8")
    sys.stderr.reconfigure(encoding="utf-8")
except Exception:
    pass

# Must be set BEFORE importing database/routers so the engine picks it up.
os.environ.setdefault("DATABASE_URL", "sqlite:///./adherence_dev.db")

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from database import Base, engine
from routers import (
    auth_router, medications_router, schedule_router, notifications_router,
    gamification_router, settings_router, conversations_router,
)

Base.metadata.create_all(bind=engine)

app = FastAPI(title="PharmaChat — Adherence (dev harness)")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:5173"],
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(auth_router.router)
app.include_router(medications_router.router)
app.include_router(schedule_router.router)
app.include_router(notifications_router.router)
app.include_router(gamification_router.router)
app.include_router(settings_router.router)
app.include_router(conversations_router.router)


@app.get("/health")
def health():
    return {"status": "ok", "mode": "adherence-dev-harness", "db": os.environ["DATABASE_URL"]}


# In this lightweight harness the AI chat pipeline isn't loaded. Return a clear
# explanation instead of a connection error so the chat page stays friendly.
from pydantic import BaseModel
from typing import Optional


class _ChatStub(BaseModel):
    query: Optional[str] = None
    image_base64: Optional[str] = None
    conversation_id: Optional[int] = None


@app.post("/chat")
def chat_stub(_req: _ChatStub):
    return {
        "response": (
            "ℹ️ Chat is disabled in this demo mode. The medication-assistant chat "
            "needs the full AI stack (Groq/Gemini + ChromaDB), which isn't installed "
            "here. All the new features work though — try Schedule, Today's Doses, "
            "Leaderboard, and Profile from the sidebar!"
        )
    }


@app.get("/conversations/{conversation_id}/messages")
def messages_stub(conversation_id: int):
    return {"messages": []}


@app.on_event("startup")
def _startup():
    try:
        from scheduler import start_scheduler
        start_scheduler()
    except Exception as e:
        print(f"[harness] scheduler not started: {e}")