feat: chatbot
This commit is contained in:
60
backend/routers/ai_chat.py
Normal file
60
backend/routers/ai_chat.py
Normal file
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
Free-form, read-only chat with GPT-4o about the current cockpit state.
|
||||
No function-calling — this endpoint can never trigger an action.
|
||||
"""
|
||||
from typing import List, Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from services.ai_chat_context import CONTEXT_BLOCKS
|
||||
|
||||
router = APIRouter(prefix="/api/ai-chat", tags=["ai-chat"])
|
||||
|
||||
|
||||
class ChatMessageBody(BaseModel):
|
||||
session_id: str
|
||||
message: str
|
||||
enabled_blocks: Optional[List[str]] = None
|
||||
refresh_context: bool = False
|
||||
|
||||
|
||||
class ClearBody(BaseModel):
|
||||
session_id: str
|
||||
|
||||
|
||||
@router.get("/blocks")
|
||||
def list_context_blocks():
|
||||
"""Static list of context block keys the frontend can offer as toggles."""
|
||||
return {"blocks": CONTEXT_BLOCKS}
|
||||
|
||||
|
||||
@router.post("/")
|
||||
def send_message(body: ChatMessageBody):
|
||||
from services.ai_chat import send_chat_message
|
||||
if not body.message.strip():
|
||||
raise HTTPException(400, "Message vide.")
|
||||
try:
|
||||
return send_chat_message(
|
||||
session_id=body.session_id,
|
||||
message=body.message.strip(),
|
||||
enabled_blocks=body.enabled_blocks,
|
||||
refresh_context=body.refresh_context,
|
||||
)
|
||||
except RuntimeError as exc:
|
||||
raise HTTPException(400, str(exc)) from exc
|
||||
|
||||
|
||||
@router.get("/history")
|
||||
def get_history(session_id: str):
|
||||
from services.database import get_chat_messages
|
||||
return {"messages": get_chat_messages(session_id)}
|
||||
|
||||
|
||||
@router.post("/clear")
|
||||
def clear_session(body: ClearBody):
|
||||
from services.database import clear_chat_session
|
||||
from services.ai_chat_context import clear_context_cache
|
||||
clear_chat_session(body.session_id)
|
||||
clear_context_cache(body.session_id)
|
||||
return {"cleared": body.session_id}
|
||||
Reference in New Issue
Block a user