2026-01-07 11:56:40 +08:00
|
|
|
|
"""
|
|
|
|
|
|
章节相关 API 路由
|
|
|
|
|
|
"""
|
2026-03-11 10:06:12 +08:00
|
|
|
|
import logging
|
|
|
|
|
|
import os
|
2026-01-18 15:57:51 +08:00
|
|
|
|
from typing import List, Optional
|
2026-01-07 11:56:40 +08:00
|
|
|
|
|
2026-01-18 15:57:51 +08:00
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
2026-01-07 11:56:40 +08:00
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
|
|
from database import get_async_db
|
|
|
|
|
|
from database.models import Chapter as ChapterModel
|
2026-01-18 15:57:51 +08:00
|
|
|
|
from database.models import User as UserModel
|
|
|
|
|
|
from middleware.auth import get_current_user
|
2026-03-01 10:50:58 +01:00
|
|
|
|
from agents.prompts.memory_prompts import CHAPTER_CATEGORIES, CHAPTER_ORDER, STAGE_TO_ORDER
|
2026-03-11 10:06:12 +08:00
|
|
|
|
from services.memoir_images.storage import (
|
|
|
|
|
|
TencentCosStorageService,
|
|
|
|
|
|
normalize_cos_url,
|
|
|
|
|
|
resolve_image_storage_key,
|
|
|
|
|
|
)
|
2026-01-07 11:56:40 +08:00
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api/chapters", tags=["chapters"])
|
2026-03-11 10:06:12 +08:00
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _normalize_image_assets(images: list[dict] | None) -> list[dict]:
|
|
|
|
|
|
bucket = os.getenv("TENCENT_COS_BUCKET", "")
|
|
|
|
|
|
region = os.getenv("TENCENT_COS_REGION", "")
|
|
|
|
|
|
base_url = os.getenv("TENCENT_COS_BASE_URL", "")
|
|
|
|
|
|
storage = TencentCosStorageService.from_env()
|
|
|
|
|
|
normalized_assets: list[dict] = []
|
|
|
|
|
|
|
|
|
|
|
|
for item in (images or []):
|
|
|
|
|
|
asset = dict(item)
|
|
|
|
|
|
normalized_url = normalize_cos_url(
|
|
|
|
|
|
asset.get("url"),
|
|
|
|
|
|
bucket=bucket,
|
|
|
|
|
|
region=region,
|
|
|
|
|
|
base_url=base_url,
|
|
|
|
|
|
)
|
|
|
|
|
|
storage_key = resolve_image_storage_key(asset)
|
|
|
|
|
|
if asset.get("status") == "completed" and storage_key:
|
|
|
|
|
|
try:
|
|
|
|
|
|
asset["url"] = storage.get_download_url(storage_key)
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
logger.warning("章节图片签名失败: key=%s, error=%s", storage_key, exc)
|
|
|
|
|
|
asset["url"] = normalized_url
|
|
|
|
|
|
else:
|
|
|
|
|
|
asset["url"] = normalized_url
|
|
|
|
|
|
asset.pop("storage_key", None)
|
|
|
|
|
|
normalized_assets.append(asset)
|
|
|
|
|
|
|
|
|
|
|
|
return normalized_assets
|
2026-01-07 11:56:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-03-01 10:50:58 +01:00
|
|
|
|
def _chapter_to_dict(ch: ChapterModel) -> dict:
|
2026-03-11 10:06:12 +08:00
|
|
|
|
normalized_images = _normalize_image_assets(ch.images)
|
2026-03-01 10:50:58 +01:00
|
|
|
|
return {
|
|
|
|
|
|
"id": ch.id,
|
|
|
|
|
|
"title": ch.title,
|
|
|
|
|
|
"content": ch.content,
|
|
|
|
|
|
"order_index": ch.order_index,
|
|
|
|
|
|
"status": ch.status,
|
|
|
|
|
|
"category": ch.category,
|
2026-03-11 10:06:12 +08:00
|
|
|
|
"images": normalized_images,
|
2026-03-01 10:50:58 +01:00
|
|
|
|
"updated_at": ch.updated_at.isoformat() if ch.updated_at else None,
|
|
|
|
|
|
"is_new": ch.is_new,
|
|
|
|
|
|
"source_segments": ch.source_segments or [],
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-07 11:56:40 +08:00
|
|
|
|
@router.get("", response_model=List[dict])
|
|
|
|
|
|
async def get_chapters(
|
2026-01-18 15:57:51 +08:00
|
|
|
|
current_user: UserModel = Depends(get_current_user),
|
2026-01-21 22:31:03 +01:00
|
|
|
|
is_new: Optional[bool] = Query(None, description="仅返回未读章节"),
|
2026-01-07 11:56:40 +08:00
|
|
|
|
db: AsyncSession = Depends(get_async_db)
|
|
|
|
|
|
):
|
2026-03-01 10:50:58 +01:00
|
|
|
|
"""
|
|
|
|
|
|
获取用户所有章节(需要认证,仅返回 active 章节)。
|
|
|
|
|
|
始终返回全部 8 个预定义类别,没有内容的类别用占位符返回。
|
|
|
|
|
|
"""
|
2026-02-14 10:57:51 +01:00
|
|
|
|
stmt = select(ChapterModel).where(
|
|
|
|
|
|
ChapterModel.user_id == current_user.id,
|
|
|
|
|
|
ChapterModel.is_active == True
|
|
|
|
|
|
)
|
2026-01-21 22:31:03 +01:00
|
|
|
|
if is_new is True:
|
|
|
|
|
|
stmt = stmt.where(ChapterModel.is_new == True)
|
|
|
|
|
|
stmt = stmt.order_by(ChapterModel.order_index)
|
2026-01-07 11:56:40 +08:00
|
|
|
|
result = await db.execute(stmt)
|
|
|
|
|
|
chapters = result.scalars().all()
|
2026-03-01 10:50:58 +01:00
|
|
|
|
|
|
|
|
|
|
chapter_by_category: dict[str, ChapterModel] = {}
|
|
|
|
|
|
for ch in chapters:
|
|
|
|
|
|
if ch.category and ch.category not in chapter_by_category:
|
|
|
|
|
|
chapter_by_category[ch.category] = ch
|
|
|
|
|
|
|
|
|
|
|
|
all_chapters: List[dict] = []
|
|
|
|
|
|
for category in CHAPTER_ORDER:
|
|
|
|
|
|
ch = chapter_by_category.pop(category, None)
|
|
|
|
|
|
if ch:
|
|
|
|
|
|
all_chapters.append(_chapter_to_dict(ch))
|
|
|
|
|
|
else:
|
|
|
|
|
|
if is_new is True:
|
|
|
|
|
|
continue
|
|
|
|
|
|
all_chapters.append({
|
|
|
|
|
|
"id": f"placeholder_{category}",
|
|
|
|
|
|
"title": CHAPTER_CATEGORIES[category],
|
|
|
|
|
|
"content": "",
|
|
|
|
|
|
"order_index": STAGE_TO_ORDER.get(category, 999),
|
|
|
|
|
|
"status": "empty",
|
|
|
|
|
|
"category": category,
|
|
|
|
|
|
"images": [],
|
|
|
|
|
|
"updated_at": None,
|
|
|
|
|
|
"is_new": False,
|
|
|
|
|
|
"source_segments": [],
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
for ch in chapter_by_category.values():
|
|
|
|
|
|
all_chapters.append(_chapter_to_dict(ch))
|
|
|
|
|
|
|
|
|
|
|
|
return all_chapters
|
2026-01-07 11:56:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/{chapter_id}", response_model=dict)
|
|
|
|
|
|
async def get_chapter(
|
|
|
|
|
|
chapter_id: str,
|
2026-01-18 15:57:51 +08:00
|
|
|
|
current_user: UserModel = Depends(get_current_user),
|
2026-01-07 11:56:40 +08:00
|
|
|
|
db: AsyncSession = Depends(get_async_db)
|
|
|
|
|
|
):
|
2026-01-18 15:57:51 +08:00
|
|
|
|
"""获取章节详情(需要认证,只能访问自己的章节)"""
|
2026-01-07 11:56:40 +08:00
|
|
|
|
chapter = await db.get(ChapterModel, chapter_id)
|
|
|
|
|
|
if not chapter:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Chapter not found")
|
|
|
|
|
|
|
2026-01-18 15:57:51 +08:00
|
|
|
|
# 验证用户权限
|
|
|
|
|
|
if chapter.user_id != current_user.id:
|
|
|
|
|
|
raise HTTPException(status_code=403, detail="无权访问此章节")
|
|
|
|
|
|
|
2026-01-07 11:56:40 +08:00
|
|
|
|
return {
|
|
|
|
|
|
"id": chapter.id,
|
|
|
|
|
|
"title": chapter.title,
|
|
|
|
|
|
"content": chapter.content,
|
|
|
|
|
|
"order_index": chapter.order_index,
|
|
|
|
|
|
"status": chapter.status,
|
|
|
|
|
|
"category": chapter.category,
|
2026-03-11 10:06:12 +08:00
|
|
|
|
"images": _normalize_image_assets(chapter.images),
|
2026-01-21 22:31:03 +01:00
|
|
|
|
"updated_at": chapter.updated_at.isoformat() if chapter.updated_at else None,
|
|
|
|
|
|
"is_new": chapter.is_new,
|
|
|
|
|
|
"source_segments": chapter.source_segments or [],
|
2026-01-07 11:56:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-02-14 10:57:51 +01:00
|
|
|
|
@router.delete("/{chapter_id}")
|
|
|
|
|
|
async def disable_chapter(
|
|
|
|
|
|
chapter_id: str,
|
|
|
|
|
|
current_user: UserModel = Depends(get_current_user),
|
|
|
|
|
|
db: AsyncSession = Depends(get_async_db)
|
|
|
|
|
|
):
|
|
|
|
|
|
"""清除章节(将章节标记为 disabled,需要认证,只能操作自己的章节)"""
|
|
|
|
|
|
chapter = await db.get(ChapterModel, chapter_id)
|
|
|
|
|
|
if not chapter:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Chapter not found")
|
|
|
|
|
|
|
|
|
|
|
|
# 验证用户权限
|
|
|
|
|
|
if chapter.user_id != current_user.id:
|
|
|
|
|
|
raise HTTPException(status_code=403, detail="无权操作此章节")
|
|
|
|
|
|
|
|
|
|
|
|
# 将章节标记为 disabled(不物理删除)
|
|
|
|
|
|
chapter.is_active = False
|
|
|
|
|
|
await db.commit()
|
|
|
|
|
|
|
|
|
|
|
|
return {"status": "ok", "message": "章节已清除"}
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-07 11:56:40 +08:00
|
|
|
|
@router.post("/{chapter_id}/regenerate")
|
|
|
|
|
|
async def regenerate_chapter(
|
|
|
|
|
|
chapter_id: str,
|
2026-01-18 15:57:51 +08:00
|
|
|
|
current_user: UserModel = Depends(get_current_user),
|
2026-01-07 11:56:40 +08:00
|
|
|
|
db: AsyncSession = Depends(get_async_db)
|
|
|
|
|
|
):
|
2026-01-18 15:57:51 +08:00
|
|
|
|
"""重新整理章节(需要认证,只能操作自己的章节)"""
|
|
|
|
|
|
chapter = await db.get(ChapterModel, chapter_id)
|
|
|
|
|
|
if not chapter:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Chapter not found")
|
|
|
|
|
|
|
|
|
|
|
|
# 验证用户权限
|
|
|
|
|
|
if chapter.user_id != current_user.id:
|
|
|
|
|
|
raise HTTPException(status_code=403, detail="无权操作此章节")
|
|
|
|
|
|
|
2026-01-07 11:56:40 +08:00
|
|
|
|
# TODO: 实现重新整理逻辑
|
|
|
|
|
|
return {"status": "ok", "message": "Chapter regeneration triggered"}
|