2026-04-04 16:29:37 +00:00
|
|
|
from typing import Any, Dict, Optional
|
2026-03-13 06:40:54 +00:00
|
|
|
|
2026-04-04 16:29:37 +00:00
|
|
|
from fastapi import APIRouter, Depends
|
2026-03-13 06:40:54 +00:00
|
|
|
from pydantic import BaseModel
|
2026-04-04 16:29:37 +00:00
|
|
|
from sqlmodel import Session
|
2026-03-13 06:40:54 +00:00
|
|
|
|
|
|
|
|
from core.database import get_session
|
|
|
|
|
from services.topic_service import (
|
2026-04-04 16:29:37 +00:00
|
|
|
create_topic,
|
|
|
|
|
delete_topic,
|
|
|
|
|
delete_topic_item,
|
|
|
|
|
get_topic_item_stats,
|
|
|
|
|
list_topic_items,
|
|
|
|
|
list_topics,
|
|
|
|
|
mark_topic_item_read,
|
|
|
|
|
update_topic,
|
2026-03-13 06:40:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TopicCreateRequest(BaseModel):
|
|
|
|
|
topic_key: str
|
|
|
|
|
name: Optional[str] = None
|
|
|
|
|
description: Optional[str] = None
|
|
|
|
|
is_active: bool = True
|
|
|
|
|
routing: Optional[Dict[str, Any]] = None
|
|
|
|
|
view_schema: Optional[Dict[str, Any]] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TopicUpdateRequest(BaseModel):
|
|
|
|
|
name: Optional[str] = None
|
|
|
|
|
description: Optional[str] = None
|
|
|
|
|
is_active: Optional[bool] = None
|
|
|
|
|
routing: Optional[Dict[str, Any]] = None
|
|
|
|
|
view_schema: Optional[Dict[str, Any]] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/api/bots/{bot_id}/topics")
|
|
|
|
|
def list_bot_topics(bot_id: str, session: Session = Depends(get_session)):
|
2026-04-04 16:29:37 +00:00
|
|
|
return list_topics(session, bot_id)
|
2026-03-13 06:40:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/api/bots/{bot_id}/topics")
|
|
|
|
|
def create_bot_topic(bot_id: str, payload: TopicCreateRequest, session: Session = Depends(get_session)):
|
2026-04-04 16:29:37 +00:00
|
|
|
return create_topic(
|
|
|
|
|
session,
|
2026-03-13 06:40:54 +00:00
|
|
|
bot_id=bot_id,
|
2026-04-04 16:29:37 +00:00
|
|
|
topic_key=payload.topic_key,
|
|
|
|
|
name=payload.name,
|
|
|
|
|
description=payload.description,
|
|
|
|
|
is_active=payload.is_active,
|
|
|
|
|
routing=payload.routing,
|
|
|
|
|
view_schema=payload.view_schema,
|
2026-03-13 06:40:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.put("/api/bots/{bot_id}/topics/{topic_key}")
|
|
|
|
|
def update_bot_topic(bot_id: str, topic_key: str, payload: TopicUpdateRequest, session: Session = Depends(get_session)):
|
2026-04-04 16:29:37 +00:00
|
|
|
return update_topic(session, bot_id=bot_id, topic_key=topic_key, updates=payload.model_dump(exclude_unset=True))
|
2026-03-13 06:40:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/api/bots/{bot_id}/topics/{topic_key}")
|
|
|
|
|
def delete_bot_topic(bot_id: str, topic_key: str, session: Session = Depends(get_session)):
|
2026-04-04 16:29:37 +00:00
|
|
|
return delete_topic(session, bot_id=bot_id, topic_key=topic_key)
|
2026-03-13 06:40:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/api/bots/{bot_id}/topic-items")
|
|
|
|
|
def list_bot_topic_items(
|
|
|
|
|
bot_id: str,
|
|
|
|
|
topic_key: Optional[str] = None,
|
|
|
|
|
cursor: Optional[int] = None,
|
|
|
|
|
limit: int = 50,
|
|
|
|
|
session: Session = Depends(get_session),
|
|
|
|
|
):
|
2026-04-04 16:29:37 +00:00
|
|
|
return list_topic_items(session, bot_id=bot_id, topic_key=topic_key, cursor=cursor, limit=limit)
|
2026-03-13 06:40:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/api/bots/{bot_id}/topic-items/stats")
|
|
|
|
|
def get_bot_topic_item_stats(bot_id: str, session: Session = Depends(get_session)):
|
2026-04-04 16:29:37 +00:00
|
|
|
return get_topic_item_stats(session, bot_id=bot_id)
|
2026-03-13 06:40:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/api/bots/{bot_id}/topic-items/{item_id}/read")
|
|
|
|
|
def mark_bot_topic_item_read(bot_id: str, item_id: int, session: Session = Depends(get_session)):
|
2026-04-04 16:29:37 +00:00
|
|
|
return mark_topic_item_read(session, bot_id=bot_id, item_id=item_id)
|
2026-03-15 07:14:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/api/bots/{bot_id}/topic-items/{item_id}")
|
|
|
|
|
def delete_bot_topic_item(bot_id: str, item_id: int, session: Session = Depends(get_session)):
|
2026-04-04 16:29:37 +00:00
|
|
|
return delete_topic_item(session, bot_id=bot_id, item_id=item_id)
|