File size: 13,729 Bytes
ad8bc9a
 
45b7965
ad8bc9a
 
 
 
 
 
 
 
 
 
 
45b7965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad8bc9a
2b17a70
45b7965
ad8bc9a
 
 
 
 
 
 
45b7965
 
ad8bc9a
 
 
 
2b17a70
ad8bc9a
 
45b7965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad8bc9a
 
 
 
2b17a70
ad8bc9a
2b17a70
ad8bc9a
 
 
2b17a70
ad8bc9a
 
 
 
 
 
 
 
 
 
 
45b7965
 
ad8bc9a
 
 
 
 
 
 
 
 
45b7965
 
ad8bc9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b17a70
 
 
ad8bc9a
 
 
2b17a70
 
ad8bc9a
2b17a70
ad8bc9a
 
45b7965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad8bc9a
 
 
 
 
 
 
 
 
 
 
 
45b7965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad8bc9a
 
45b7965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad8bc9a
45b7965
ad8bc9a
 
 
2b17a70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad8bc9a
45b7965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad8bc9a
 
2b17a70
ad8bc9a
 
 
 
 
 
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from __future__ import annotations

import logging
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager

import httpx
from fastapi import FastAPI, HTTPException, Request, Response
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse

from app.config import Settings
from app.knowledge import system_prompt
from app.memory import (
    InvalidMemorySession,
    MemoryConsentRequired,
    MemoryLimitReached,
    MemoryRecord,
    MemoryStore,
    SensitiveMemoryRejected,
    extract_explicit_memory,
)
from app.models import (
    FuugiRequest,
    FuugiResponse,
    MemoryConsentRequest,
    MemoryEvent,
    MemoryItem,
    MemoryListResponse,
)
from app.openrouter import OpenRouterClient, OpenRouterNotConfigured, OpenRouterUnavailable
from app.worker import WorkerClient, WorkerNotConfigured, WorkerUnavailable
from app.research import ResearchStore, ResearchWriteError, classify_research
from app.security import (
    BodySizeLimitMiddleware,
    SlidingWindowRateLimiter,
    client_identity,
    request_messages,
)

logger = logging.getLogger(__name__)


def create_app(
    settings: Settings | None = None,
    transport: httpx.AsyncBaseTransport | None = None,
    worker_transport: httpx.AsyncBaseTransport | None = None,
) -> FastAPI:
    active_settings = settings or Settings.from_env()
    memory_store = (
        MemoryStore(
            database_path=active_settings.memory_database_path,
            pepper=active_settings.memory_session_pepper,
            retention_days=active_settings.memory_retention_days,
            max_items=active_settings.memory_max_items,
            max_item_characters=active_settings.memory_max_item_characters,
            max_context_characters=active_settings.memory_max_context_characters,
        )
        if active_settings.memory_enabled
        else None
    )
    research_store = (
        ResearchStore(
            database_path=active_settings.market_research_database_path,
            retention_days=active_settings.market_research_retention_days,
        )
        if active_settings.market_research_enabled
        else None
    )

    @asynccontextmanager
    async def lifespan(application: FastAPI) -> AsyncIterator[None]:
        client = OpenRouterClient(active_settings, transport=transport)
        worker = WorkerClient(active_settings, transport=worker_transport)
        application.state.openrouter = client
        application.state.worker = worker
        try:
            yield
        finally:
            await worker.close()
            await client.close()

    application = FastAPI(
        title="Fuugi Companion API",
        version="1.0.0",
        docs_url=None,
        redoc_url=None,
        openapi_url=None,
        lifespan=lifespan,
    )
    application.state.settings = active_settings
    application.state.memory = memory_store
    application.state.research = research_store
    application.state.rate_limiter = SlidingWindowRateLimiter(
        active_settings.rate_limit_requests,
        active_settings.rate_limit_window_seconds,
    )

    application.add_middleware(
        CORSMiddleware,
        allow_origins=list(active_settings.allowed_origins),
        allow_credentials=False,
        allow_methods=["GET", "POST", "DELETE", "OPTIONS"],
        allow_headers=["Accept", "Content-Type", "X-Fuugi-Session"],
        max_age=600,
    )
    application.add_middleware(
        BodySizeLimitMiddleware,
        max_body_bytes=active_settings.max_body_bytes,
    )

    @application.exception_handler(RequestValidationError)
    async def invalid_request(_request: Request, _error: RequestValidationError) -> JSONResponse:
        return JSONResponse({"detail": "Invalid request."}, status_code=400)

    async def enforce_rate_limit(request: Request) -> None:
        limiter: SlidingWindowRateLimiter = request.app.state.rate_limiter
        allowed, retry_after = await limiter.check(client_identity(request, active_settings))
        if not allowed:
            raise HTTPException(
                status_code=429,
                detail="Too many requests.",
                headers={"Retry-After": str(retry_after)},
            )

    @application.get("/health")
    async def health() -> dict[str, str | bool]:
        worker_on = bool(
            active_settings.fuugi_worker_url and active_settings.fuugi_worker_token
        )
        return {
            "status": "ok",
            "service": "fuugi-api",
            "source": "fuugi-worker" if worker_on else "openrouter",
            "model": "fuugi-worker" if worker_on else active_settings.primary_model,
            "upstream_configured": bool(active_settings.openrouter_api_key),
            "worker_configured": worker_on,
        }

    def memory_access(request: Request) -> tuple[MemoryStore, str]:
        store: MemoryStore | None = request.app.state.memory
        if store is None:
            raise HTTPException(status_code=404, detail="Memory is not enabled.")
        token = request.headers.get("x-fuugi-session", "").strip()
        if not token:
            raise HTTPException(status_code=400, detail="Memory session is required.")
        return store, token

    def memory_items(records: list[MemoryRecord]) -> list[MemoryItem]:
        return [
            MemoryItem(
                id=record.id,
                content=record.content,
                created_at=record.created_at,
                expires_at=record.expires_at,
            )
            for record in records
        ]

    @application.get(
        "/api/fuugi", response_model=FuugiResponse, response_model_exclude_none=True
    )
    async def fuugi_info(request: Request) -> FuugiResponse:
        await enforce_rate_limit(request)
        return FuugiResponse(
            content="Fuugi companion API is online. POST the widget JSON payload to this endpoint.",
            source="api",
            model=active_settings.primary_model,
        )

    @application.options("/api/fuugi", status_code=204)
    async def fuugi_options() -> Response:
        return Response(status_code=204)

    @application.post("/api/fuugi/memory-consent", response_model=MemoryListResponse)
    async def memory_consent(
        payload: MemoryConsentRequest, request: Request
    ) -> MemoryListResponse:
        await enforce_rate_limit(request)
        store, token = memory_access(request)
        try:
            enabled = await store.set_consent(token, payload.enabled)
            _, records = await store.list(token)
        except InvalidMemorySession as error:
            raise HTTPException(status_code=400, detail="Invalid memory session.") from error
        return MemoryListResponse(
            enabled=enabled,
            memories=memory_items(records),
        )

    @application.get("/api/fuugi/memories", response_model=MemoryListResponse)
    async def list_memories(request: Request) -> MemoryListResponse:
        await enforce_rate_limit(request)
        store, token = memory_access(request)
        try:
            enabled, records = await store.list(token)
        except InvalidMemorySession as error:
            raise HTTPException(status_code=400, detail="Invalid memory session.") from error
        return MemoryListResponse(
            enabled=enabled,
            memories=memory_items(records),
        )

    @application.delete("/api/fuugi/memories", response_model=MemoryListResponse)
    async def clear_memories(request: Request) -> MemoryListResponse:
        await enforce_rate_limit(request)
        store, token = memory_access(request)
        try:
            await store.clear(token)
            enabled, _ = await store.list(token)
        except InvalidMemorySession as error:
            raise HTTPException(status_code=400, detail="Invalid memory session.") from error
        return MemoryListResponse(enabled=enabled, memories=[])

    @application.delete("/api/fuugi/memories/{memory_id}", status_code=204)
    async def delete_memory(memory_id: str, request: Request) -> Response:
        await enforce_rate_limit(request)
        store, token = memory_access(request)
        try:
            deleted = await store.delete(token, memory_id)
        except InvalidMemorySession as error:
            raise HTTPException(status_code=400, detail="Invalid memory reference.") from error
        if not deleted:
            raise HTTPException(status_code=404, detail="Memory not found.")
        return Response(status_code=204)

    @application.post(
        "/api/fuugi", response_model=FuugiResponse, response_model_exclude_none=True
    )
    async def fuugi_chat(payload: FuugiRequest, request: Request) -> FuugiResponse:
        await enforce_rate_limit(request)
        store: MemoryStore | None = request.app.state.memory
        token = request.headers.get("x-fuugi-session", "").strip()
        explicit_memory = extract_explicit_memory(payload.message)

        if explicit_memory is not None:
            if store is None:
                return FuugiResponse(
                    content="Der Lernspeicher ist auf diesem Server nicht aktiviert.",
                    source="memory",
                    model="local-memory-v1",
                    memory_event=MemoryEvent(status="unavailable"),
                )
            if not token:
                return FuugiResponse(
                    content=(
                        "Aktiviere zuerst den Lernspeicher im Chat, damit ich das speichern darf."
                    ),
                    source="memory",
                    model="local-memory-v1",
                    memory_event=MemoryEvent(status="consent_required"),
                )
            try:
                record = await store.add(token, explicit_memory)
            except MemoryConsentRequired:
                return FuugiResponse(
                    content=(
                        "Aktiviere zuerst den Lernspeicher im Chat, damit ich das speichern darf."
                    ),
                    source="memory",
                    model="local-memory-v1",
                    memory_event=MemoryEvent(status="consent_required"),
                )
            except (MemoryLimitReached, SensitiveMemoryRejected):
                return FuugiResponse(
                    content=(
                        "Das speichere ich nicht. Nutze kurze, nicht sensible Angaben und keine "
                        "Passwörter, Schlüssel oder Zahlungsdaten."
                    ),
                    source="memory",
                    model="local-memory-v1",
                    memory_event=MemoryEvent(status="rejected"),
                )
            except InvalidMemorySession as error:
                raise HTTPException(status_code=400, detail="Invalid memory session.") from error
            return FuugiResponse(
                content="Gemerkt. Du kannst diese Erinnerung jederzeit ansehen oder löschen.",
                source="memory",
                model="local-memory-v1",
                memory_event=MemoryEvent(status="saved", id=record.id),
            )

        memories: list[str] = []
        if store is not None and token:
            try:
                memories = await store.context(token)
            except InvalidMemorySession as error:
                raise HTTPException(status_code=400, detail="Invalid memory session.") from error
        try:
            messages = request_messages(payload, active_settings, system_prompt(), memories)
        except ValueError as error:
            raise HTTPException(status_code=400, detail="Invalid request.") from error

        worker: WorkerClient = request.app.state.worker
        completion = None
        source = "openrouter"
        if worker.configured:
            try:
                completion = await worker.complete(messages)
                source = "fuugi-worker"
            except WorkerNotConfigured:
                completion = None
            except WorkerUnavailable:
                logger.warning("Fuugi worker unavailable; trying OpenRouter fallback.")
        if completion is None:
            client: OpenRouterClient = request.app.state.openrouter
            try:
                completion = await client.complete(messages)
                source = "openrouter"
            except OpenRouterNotConfigured as error:
                raise HTTPException(
                    status_code=503, detail="Service temporarily unavailable."
                ) from error
            except OpenRouterUnavailable as error:
                raise HTTPException(
                    status_code=502, detail="Upstream service temporarily unavailable."
                ) from error

        research: ResearchStore | None = request.app.state.research
        if research is not None:
            research_text = payload.message
            if not research_text:
                research_text = next(
                    (
                        item.content
                        for item in reversed(payload.messages)
                        if item.role == "user"
                    ),
                    "",
                )
            if research_text:
                topic, intent = classify_research(research_text)
                try:
                    await research.increment(topic, intent)
                except ResearchWriteError:
                    logger.warning("Anonymous research counter update failed.")

        return FuugiResponse(
            content=completion.content,
            source=source,
            model=completion.model,
        )

    return application


app = create_app()