File size: 6,531 Bytes
6bc6eba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import hmac
import json
import os
from pathlib import Path
from typing import Any, Iterable

from fastapi import FastAPI, HTTPException, Query, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field

from .app import answer_question, search_archives, stream_answer_question


BASE_DIR = Path(__file__).resolve().parent
WEB_DIR = BASE_DIR / "web"
ANSWER_MODES = {"Strict Mode", "Study Mode", "Summary Mode"}
APP_PASSWORD = os.getenv("APP_PASSWORD", "")

api = FastAPI(title="The 7th Handle API", version="1.0.0")

api.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=False,
    allow_methods=["GET", "POST"],
    allow_headers=["*"],
)


class AnswerRequest(BaseModel):
    question: str = Field(..., min_length=1, max_length=4000)
    answer_mode: str = Field("Strict Mode")
    sermon_scope: str | None = Field(None, max_length=20)
    conversation_mode: bool = Field(False)
    history: list[dict[str, str]] = Field(default_factory=list)


def require_password(request: Request) -> None:
    if not APP_PASSWORD:
        return
    provided = request.headers.get("X-App-Password", "")
    if not hmac.compare_digest(provided, APP_PASSWORD):
        raise HTTPException(status_code=401, detail="Invalid app password.")


def normalize_mode(answer_mode: str) -> str:
    return answer_mode if answer_mode in ANSWER_MODES else "Strict Mode"


def sanitize_history(history: list[dict[str, str]]) -> list[dict[str, str]]:
    clean = []
    for item in history[-8:]:
        role = str(item.get("role", "")).strip().lower()
        content = " ".join(str(item.get("content", "")).split())
        if role not in {"user", "assistant"} or not content:
            continue
        clean.append({"role": role, "content": content[:1200]})
    return clean


def messagehub_link(filename: str) -> str:
    if not filename:
        return "#"
    code = filename.replace(".pdf", "").replace(".PDF", "").strip().split()[0]
    return f"https://www.messagehub.info/en/read.do?ref_num={code}"


def serialize_doc(doc) -> dict[str, Any]:
    source = doc.metadata.get("source", "")
    paragraph_text = doc.page_content or ""
    return {
        "source": source,
        "paragraph": doc.metadata.get("paragraph", ""),
        "content": paragraph_text,
        "full_text": paragraph_text,
        "retrieval_origin": doc.metadata.get("retrieval_origin", "unknown"),
        "retrieval_rank": doc.metadata.get("retrieval_rank", ""),
        "messagehub_url": messagehub_link(source),
    }


def serialize_result(result: dict[str, Any], question: str, answer_mode: str) -> dict[str, Any]:
    sources = [serialize_doc(doc) for doc in result.get("source_documents", [])]
    return {
        "answer": result.get("answer", ""),
        "sources": sources,
        "grounding_status": result.get("grounding_status", "unknown"),
        "diagnostics": result.get("diagnostics", {}),
        "question": question,
        "answer_mode": answer_mode,
    }


def serialize_stream_data(data: dict[str, Any], question: str, answer_mode: str) -> dict[str, Any]:
    payload = dict(data)
    if "source_documents" in payload:
        payload["sources"] = [serialize_doc(doc) for doc in payload.pop("source_documents")]
    if "sources" in payload:
        payload["sources"] = [
            serialize_doc(doc) if hasattr(doc, "metadata") else doc
            for doc in payload["sources"]
        ]
    payload.setdefault("question", question)
    payload.setdefault("answer_mode", answer_mode)
    return payload


def apply_sermon_scope(question: str, sermon_scope: str | None) -> str:
    scope = (sermon_scope or "").strip()
    if not scope:
        return question.strip()
    if scope.upper() in question.upper():
        return question.strip()
    return f"{scope} {question.strip()}"


def sse(event: str, data: dict[str, Any]) -> str:
    return f"event: {event}\ndata: {json.dumps(data, ensure_ascii=True)}\n\n"


@api.get("/api/health")
def health() -> dict[str, Any]:
    return {"status": "ok", "password_required": bool(APP_PASSWORD)}


@api.post("/api/answer")
def answer(request: Request, payload: AnswerRequest) -> dict[str, Any]:
    require_password(request)
    mode = normalize_mode(payload.answer_mode)
    question = apply_sermon_scope(payload.question, payload.sermon_scope)
    try:
        result = answer_question(
            question,
            answer_mode=mode,
            history=sanitize_history(payload.history),
            conversational=payload.conversation_mode,
        )
    except ValueError as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc
    return serialize_result(result, question, mode)


@api.post("/api/answer/stream")
def answer_stream(request: Request, payload: AnswerRequest) -> StreamingResponse:
    require_password(request)
    mode = normalize_mode(payload.answer_mode)
    question = apply_sermon_scope(payload.question, payload.sermon_scope)
    history = sanitize_history(payload.history)

    def events() -> Iterable[str]:
        try:
            for event in stream_answer_question(
                question,
                answer_mode=mode,
                history=history,
                conversational=payload.conversation_mode,
            ):
                yield sse(
                    event["event"],
                    serialize_stream_data(event["data"], question, mode),
                )
        except Exception as exc:
            yield sse("error", {"detail": str(exc), "question": question, "answer_mode": mode})

    return StreamingResponse(events(), media_type="text/event-stream")


@api.get("/api/search")
def search(
    request: Request,
    q: str = Query(..., min_length=1, max_length=4000),
) -> dict[str, Any]:
    require_password(request)
    docs, debug_log = search_archives(q)
    return {
        "query": q,
        "results": [serialize_doc(doc) for doc in docs],
        "debug_log": debug_log,
    }


if (WEB_DIR / "assets").exists():
    api.mount("/assets", StaticFiles(directory=WEB_DIR / "assets"), name="assets")


@api.get("/")
def index() -> FileResponse:
    return FileResponse(WEB_DIR / "index.html")


@api.get("/{path:path}")
def spa_fallback(path: str) -> FileResponse:
    if path.startswith("api/"):
        raise HTTPException(status_code=404, detail="Not found")
    return FileResponse(WEB_DIR / "index.html")