feat: add api and mcp

This commit is contained in:
hh
2026-05-30 01:32:35 +02:00
parent 6a5cde6ae4
commit c40e720163
30 changed files with 2354 additions and 31 deletions
+68
View File
@@ -0,0 +1,68 @@
from typing import Annotated
import asyncpg
from dishka.integrations.fastapi import DishkaRoute, FromDishka
from fastapi import APIRouter, Query
from utils.read import chats
from utils.read.models import (
DEFAULT_LIMIT,
ChatListItem,
MessageVersionView,
MessageView,
Page,
)
router = APIRouter(prefix="/api", tags=["chats"], route_class=DishkaRoute)
AccountId = Annotated[int, Query()]
Limit = Annotated[int, Query()]
Offset = Annotated[int, Query()]
@router.get("/chats")
async def list_chats(
pool: FromDishka[asyncpg.Pool],
account_id: AccountId,
limit: Limit = DEFAULT_LIMIT,
offset: Offset = 0,
) -> list[ChatListItem]:
return await chats.list_chats(pool, account_id, Page(limit=limit, offset=offset))
@router.get("/chats/{chat_id}/messages")
async def chat_history(
pool: FromDishka[asyncpg.Pool],
chat_id: int,
account_id: AccountId,
limit: Limit = DEFAULT_LIMIT,
offset: Offset = 0,
include_deleted: Annotated[bool, Query()] = True,
) -> list[MessageView]:
return await chats.get_chat_history(
pool,
account_id,
chat_id,
Page(limit=limit, offset=offset),
include_deleted=include_deleted,
)
@router.get("/chats/{chat_id}/messages/{message_id}/versions")
async def message_versions(
pool: FromDishka[asyncpg.Pool], chat_id: int, message_id: int, account_id: AccountId
) -> list[MessageVersionView]:
return await chats.get_message_versions(pool, account_id, chat_id, message_id)
@router.get("/deleted")
async def deleted_messages(
pool: FromDishka[asyncpg.Pool],
account_id: AccountId,
chat_id: Annotated[int | None, Query()] = None,
limit: Limit = DEFAULT_LIMIT,
offset: Offset = 0,
) -> list[MessageView]:
return await chats.get_deleted_messages(
pool, account_id, Page(limit=limit, offset=offset), chat_id=chat_id
)