init
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api.routes import chat, health, pomodoro
|
||||
|
||||
api_router = APIRouter(prefix="/api/v1")
|
||||
api_router.include_router(health.router, tags=["health"])
|
||||
api_router.include_router(chat.router, prefix="/chat", tags=["chat"])
|
||||
api_router.include_router(pomodoro.router, prefix="/pomodoro", tags=["pomodoro"])
|
||||
@@ -0,0 +1,55 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.schemas import MessageCreate, MessageOut, SessionCreate, SessionDetailOut, SessionOut
|
||||
from app.chat.service import ChatService
|
||||
from app.db.base import get_db
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/sessions", response_model=SessionOut)
|
||||
def create_session(payload: SessionCreate, db: Session = Depends(get_db)) -> SessionOut:
|
||||
service = ChatService(db)
|
||||
return service.create_session(title=payload.title)
|
||||
|
||||
|
||||
@router.get("/sessions", response_model=list[SessionOut])
|
||||
def list_sessions(db: Session = Depends(get_db)) -> list[SessionOut]:
|
||||
service = ChatService(db)
|
||||
return service.list_sessions()
|
||||
|
||||
|
||||
@router.get("/sessions/{session_id}", response_model=SessionDetailOut)
|
||||
def get_session(session_id: int, db: Session = Depends(get_db)) -> SessionDetailOut:
|
||||
service = ChatService(db)
|
||||
session = service.get_session(session_id)
|
||||
if not session:
|
||||
raise HTTPException(status_code=404, detail="Session not found")
|
||||
return session
|
||||
|
||||
|
||||
@router.delete("/sessions/{session_id}")
|
||||
def delete_session(session_id: int, db: Session = Depends(get_db)) -> dict[str, bool]:
|
||||
service = ChatService(db)
|
||||
if not service.delete_session(session_id):
|
||||
raise HTTPException(status_code=404, detail="Session not found")
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@router.post("/sessions/{session_id}/messages")
|
||||
async def send_message(
|
||||
session_id: int,
|
||||
payload: MessageCreate,
|
||||
db: Session = Depends(get_db),
|
||||
) -> StreamingResponse:
|
||||
service = ChatService(db)
|
||||
if not service.get_session(session_id):
|
||||
raise HTTPException(status_code=404, detail="Session not found")
|
||||
|
||||
async def event_stream():
|
||||
async for chunk in service.stream_response(session_id, payload.content):
|
||||
yield chunk
|
||||
|
||||
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
||||
@@ -0,0 +1,8 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/health")
|
||||
def health() -> dict[str, str]:
|
||||
return {"status": "ok"}
|
||||
@@ -0,0 +1,60 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.schemas import PomodoroStart, PomodoroStop
|
||||
from app.db.base import get_db
|
||||
from app.pomodoro.service import PomodoroService
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def _handle_value_error(exc: ValueError) -> HTTPException:
|
||||
return HTTPException(status_code=400, detail=str(exc))
|
||||
|
||||
|
||||
@router.get("/status")
|
||||
def get_status(db: Session = Depends(get_db)) -> dict:
|
||||
return PomodoroService(db).get_status()
|
||||
|
||||
|
||||
@router.post("/start")
|
||||
def start_pomodoro(payload: PomodoroStart, db: Session = Depends(get_db)) -> dict:
|
||||
try:
|
||||
return PomodoroService(db).start(
|
||||
duration_min=payload.duration_min,
|
||||
task_note=payload.task_note,
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise _handle_value_error(exc) from exc
|
||||
|
||||
|
||||
@router.post("/pause")
|
||||
def pause_pomodoro(db: Session = Depends(get_db)) -> dict:
|
||||
try:
|
||||
return PomodoroService(db).pause()
|
||||
except ValueError as exc:
|
||||
raise _handle_value_error(exc) from exc
|
||||
|
||||
|
||||
@router.post("/resume")
|
||||
def resume_pomodoro(db: Session = Depends(get_db)) -> dict:
|
||||
try:
|
||||
return PomodoroService(db).resume()
|
||||
except ValueError as exc:
|
||||
raise _handle_value_error(exc) from exc
|
||||
|
||||
|
||||
@router.post("/stop")
|
||||
def stop_pomodoro(payload: PomodoroStop, db: Session = Depends(get_db)) -> dict:
|
||||
try:
|
||||
return PomodoroService(db).stop(
|
||||
result=payload.result,
|
||||
completed=payload.completed,
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise _handle_value_error(exc) from exc
|
||||
|
||||
|
||||
@router.get("/history")
|
||||
def get_history(limit: int = 20, db: Session = Depends(get_db)) -> list[dict]:
|
||||
return PomodoroService(db).history(limit=limit)
|
||||
@@ -0,0 +1,43 @@
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class SessionCreate(BaseModel):
|
||||
title: str = "Новый чат"
|
||||
|
||||
|
||||
class SessionOut(BaseModel):
|
||||
id: int
|
||||
title: str
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class MessageOut(BaseModel):
|
||||
id: int
|
||||
role: str
|
||||
content: str
|
||||
created_at: datetime
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class SessionDetailOut(SessionOut):
|
||||
messages: list[MessageOut]
|
||||
|
||||
|
||||
class MessageCreate(BaseModel):
|
||||
content: str = Field(min_length=1)
|
||||
|
||||
|
||||
class PomodoroStart(BaseModel):
|
||||
duration_min: int = Field(default=25, ge=1, le=180)
|
||||
task_note: str = ""
|
||||
|
||||
|
||||
class PomodoroStop(BaseModel):
|
||||
result: str = ""
|
||||
completed: bool = False
|
||||
Reference in New Issue
Block a user