2026-03-18 17:18:23 +08:00
|
|
|
|
"""Memoir service — 回忆录编排(章节生成、状态流转);通过 MemoryService 获取 evidence。"""
|
|
|
|
|
|
|
2026-03-20 15:15:35 +08:00
|
|
|
|
import asyncio
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from typing import List, Optional
|
|
|
|
|
|
|
|
|
|
|
|
from fastapi import HTTPException
|
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
from sqlalchemy.orm import joinedload
|
|
|
|
|
|
|
2026-03-20 15:15:35 +08:00
|
|
|
|
from app.core.logging import get_logger
|
|
|
|
|
|
from app.core.storage_purge import delete_object_storage_keys_best_effort
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from app.features.memoir import repo
|
2026-03-20 10:30:07 +08:00
|
|
|
|
from app.features.memoir.asset_resolver import (
|
|
|
|
|
|
collect_asset_ids_for_chapter,
|
|
|
|
|
|
collect_asset_ids_for_chapters,
|
2026-03-22 16:45:57 +08:00
|
|
|
|
strip_image_placeholders,
|
2026-03-20 10:30:07 +08:00
|
|
|
|
)
|
|
|
|
|
|
from app.features.memoir.asset_urls import signed_urls_for_asset_ids
|
2026-03-20 15:15:35 +08:00
|
|
|
|
from app.features.memoir.chapter_markdown_compose import (
|
|
|
|
|
|
materialize_chapter_markdown_from_loaded_chapter,
|
|
|
|
|
|
)
|
|
|
|
|
|
from app.features.memoir.cover_eligibility import primary_chapter_memoir_image
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from app.features.memoir.helpers import (
|
|
|
|
|
|
chapter_to_dict,
|
2026-03-20 10:30:07 +08:00
|
|
|
|
chapter_to_list_dict,
|
2026-03-18 17:18:23 +08:00
|
|
|
|
is_image_permanently_unavailable,
|
|
|
|
|
|
)
|
2026-03-19 14:14:13 +08:00
|
|
|
|
from app.features.memoir.memoir_images.settings import MemoirImageSettings
|
2026-03-20 16:36:42 +08:00
|
|
|
|
from app.features.memoir.models import Book, Chapter, ChapterStoryLink
|
2026-03-26 16:28:33 +08:00
|
|
|
|
from app.features.memoir.reading_segment_materialize import (
|
|
|
|
|
|
chapter_meets_minimum_display,
|
|
|
|
|
|
)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from app.features.memory.service import MemoryService
|
2026-03-20 15:15:35 +08:00
|
|
|
|
from app.ports.storage import ObjectStorage
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_or_create_book(user_id: str, db: AsyncSession):
|
|
|
|
|
|
"""Get the user's current book or return None."""
|
|
|
|
|
|
return await repo.get_current_book(user_id, db)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MemoirService:
|
|
|
|
|
|
def __init__(
|
|
|
|
|
|
self,
|
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
|
memory_service: Optional[MemoryService] = None,
|
2026-03-20 15:15:35 +08:00
|
|
|
|
*,
|
|
|
|
|
|
object_storage: ObjectStorage | None = None,
|
2026-03-18 17:18:23 +08:00
|
|
|
|
):
|
|
|
|
|
|
self._db = db
|
|
|
|
|
|
self._memory = memory_service
|
2026-03-20 15:15:35 +08:00
|
|
|
|
self._object_storage = object_storage
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
|
|
|
|
|
async def get_evidence(self, user_id: str, query: str, *, top_k: int = 10) -> dict:
|
2026-03-26 12:13:36 +08:00
|
|
|
|
"""通过 MemoryService → HybridRetriever 获取证据(含向量时与 Celery 的 FTS-only 路径不同)。"""
|
2026-03-18 17:18:23 +08:00
|
|
|
|
if self._memory is None:
|
|
|
|
|
|
return {
|
|
|
|
|
|
"relevant_chunks": [],
|
|
|
|
|
|
"relevant_summaries": [],
|
|
|
|
|
|
"relevant_facts": [],
|
|
|
|
|
|
"timeline_hints": [],
|
2026-03-27 16:01:28 +08:00
|
|
|
|
"relevant_stories": [],
|
2026-03-18 17:18:23 +08:00
|
|
|
|
}
|
2026-03-27 16:01:28 +08:00
|
|
|
|
bundle = await self._memory.retrieve(user_id, query, top_k=top_k)
|
|
|
|
|
|
return bundle.model_dump()
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
|
|
|
|
|
async def _cleanup_unavailable_images(self, ch: Chapter) -> None:
|
|
|
|
|
|
cleaned = False
|
2026-03-20 15:15:35 +08:00
|
|
|
|
for rec in getattr(ch, "images", None) or []:
|
2026-03-18 17:18:23 +08:00
|
|
|
|
if rec and is_image_permanently_unavailable(rec):
|
2026-03-26 12:13:36 +08:00
|
|
|
|
logger.info("清理不可用配图: chapter={}, image={}", ch.id, rec.id)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
await self._db.delete(rec)
|
|
|
|
|
|
cleaned = True
|
|
|
|
|
|
if cleaned:
|
|
|
|
|
|
await self._db.commit()
|
|
|
|
|
|
await self._db.refresh(ch)
|
|
|
|
|
|
|
2026-03-22 16:45:57 +08:00
|
|
|
|
async def _ensure_chapter_materialized(self, chapter: Chapter) -> Chapter:
|
|
|
|
|
|
has_story_links = bool(getattr(chapter, "story_links", None))
|
|
|
|
|
|
has_snapshot = chapter.reading_segments_json is not None
|
|
|
|
|
|
if not has_story_links or (has_snapshot and not chapter.markdown_compose_dirty):
|
|
|
|
|
|
return chapter
|
|
|
|
|
|
|
|
|
|
|
|
markdown = materialize_chapter_markdown_from_loaded_chapter(chapter)
|
|
|
|
|
|
await repo.append_chapter_compose_version_async(self._db, chapter, markdown)
|
|
|
|
|
|
await self._db.commit()
|
|
|
|
|
|
refreshed = await repo.get_chapter_by_id(chapter.id, self._db)
|
|
|
|
|
|
return refreshed or chapter
|
|
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
async def get_current_book(self, user_id: str) -> dict:
|
|
|
|
|
|
book = await repo.get_current_book(user_id, self._db)
|
|
|
|
|
|
if not book:
|
|
|
|
|
|
return {"message": "No book found"}
|
|
|
|
|
|
return {
|
|
|
|
|
|
"id": book.id,
|
|
|
|
|
|
"title": book.title,
|
|
|
|
|
|
"total_pages": book.total_pages,
|
|
|
|
|
|
"total_words": book.total_words,
|
|
|
|
|
|
"cover_image_url": book.cover_image_url,
|
|
|
|
|
|
"has_update": book.has_update,
|
|
|
|
|
|
"last_update_chapter_id": book.last_update_chapter_id,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def clear_book_update(self, user_id: str) -> dict:
|
|
|
|
|
|
book = await repo.get_current_book(user_id, self._db)
|
|
|
|
|
|
if not book:
|
|
|
|
|
|
return {"status": "ok", "message": "No book found"}
|
|
|
|
|
|
book.has_update = False
|
|
|
|
|
|
await self._db.commit()
|
|
|
|
|
|
return {"status": "ok"}
|
|
|
|
|
|
|
|
|
|
|
|
async def update_book(self, book_id: str, user_id: str, title: str) -> dict:
|
|
|
|
|
|
book = await self._db.get(Book, book_id)
|
|
|
|
|
|
if not book:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Book not found")
|
|
|
|
|
|
if book.user_id != user_id:
|
|
|
|
|
|
raise HTTPException(status_code=403, detail="无权更新此回忆录")
|
|
|
|
|
|
book.title = title
|
|
|
|
|
|
await self._db.commit()
|
|
|
|
|
|
await self._db.refresh(book)
|
|
|
|
|
|
return {
|
|
|
|
|
|
"id": book.id,
|
|
|
|
|
|
"title": book.title,
|
|
|
|
|
|
"total_pages": book.total_pages,
|
|
|
|
|
|
"total_words": book.total_words,
|
|
|
|
|
|
"cover_image_url": book.cover_image_url,
|
|
|
|
|
|
"has_update": book.has_update,
|
|
|
|
|
|
"last_update_chapter_id": book.last_update_chapter_id,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def export_pdf(self, user_id: str, book_id: str) -> dict:
|
|
|
|
|
|
from app.features.memoir.pdf_service import pdf_service
|
|
|
|
|
|
|
|
|
|
|
|
book = await self._db.get(Book, book_id)
|
|
|
|
|
|
if not book:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Book not found")
|
|
|
|
|
|
if book.user_id != user_id:
|
|
|
|
|
|
raise HTTPException(status_code=403, detail="无权导出此回忆录")
|
|
|
|
|
|
stmt = (
|
|
|
|
|
|
select(Chapter)
|
|
|
|
|
|
.where(Chapter.user_id == user_id, Chapter.is_active == True)
|
2026-03-20 10:30:07 +08:00
|
|
|
|
.options(
|
|
|
|
|
|
joinedload(Chapter.images),
|
2026-03-20 16:36:42 +08:00
|
|
|
|
joinedload(Chapter.story_links).joinedload(ChapterStoryLink.story),
|
2026-03-20 10:30:07 +08:00
|
|
|
|
)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
.order_by(Chapter.order_index)
|
|
|
|
|
|
)
|
|
|
|
|
|
result = await self._db.execute(stmt)
|
2026-03-26 16:28:33 +08:00
|
|
|
|
chapters_raw = list(result.unique().scalars().all())
|
|
|
|
|
|
chapters: List[Chapter] = []
|
|
|
|
|
|
for ch in chapters_raw:
|
|
|
|
|
|
ch2 = await self._ensure_chapter_materialized(ch)
|
|
|
|
|
|
if chapter_meets_minimum_display(ch2):
|
|
|
|
|
|
chapters.append(ch2)
|
2026-03-20 10:30:07 +08:00
|
|
|
|
asset_ids = collect_asset_ids_for_chapters(chapters)
|
|
|
|
|
|
asset_map = await signed_urls_for_asset_ids(self._db, asset_ids)
|
|
|
|
|
|
pdf_bytes = await pdf_service.generate_pdf(
|
|
|
|
|
|
book, chapters, asset_url_map=asset_map
|
|
|
|
|
|
)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
return {
|
|
|
|
|
|
"pdf_base64": pdf_bytes.decode("latin1"),
|
|
|
|
|
|
"filename": f"{book.title}.pdf",
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def get_chapters(
|
|
|
|
|
|
self, user_id: str, is_new: bool | None = None
|
|
|
|
|
|
) -> List[dict]:
|
2026-03-22 16:45:57 +08:00
|
|
|
|
chapters = await repo.get_chapters_for_memoir_list(
|
2026-03-18 17:18:23 +08:00
|
|
|
|
user_id, self._db, is_new_only=is_new
|
|
|
|
|
|
)
|
2026-03-22 16:45:57 +08:00
|
|
|
|
if not chapters:
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
2026-03-20 10:30:07 +08:00
|
|
|
|
asset_ids: set[str] = set()
|
|
|
|
|
|
for ch in chapters:
|
|
|
|
|
|
asset_ids |= collect_asset_ids_for_chapter(ch)
|
|
|
|
|
|
asset_map = await signed_urls_for_asset_ids(self._db, asset_ids)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
all_chapters: List[dict] = []
|
2026-03-22 16:45:57 +08:00
|
|
|
|
for ch in chapters:
|
2026-03-26 16:28:33 +08:00
|
|
|
|
ch = await self._ensure_chapter_materialized(ch)
|
|
|
|
|
|
if not chapter_meets_minimum_display(ch):
|
|
|
|
|
|
continue
|
2026-03-18 17:18:23 +08:00
|
|
|
|
await self._cleanup_unavailable_images(ch)
|
2026-03-20 10:30:07 +08:00
|
|
|
|
all_chapters.append(chapter_to_list_dict(ch, asset_url_map=asset_map))
|
2026-03-18 17:18:23 +08:00
|
|
|
|
return all_chapters
|
|
|
|
|
|
|
|
|
|
|
|
async def get_chapter(self, chapter_id: str, user_id: str) -> dict:
|
|
|
|
|
|
chapter = await repo.get_chapter_by_id(chapter_id, self._db)
|
|
|
|
|
|
if not chapter:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Chapter not found")
|
|
|
|
|
|
if chapter.user_id != user_id:
|
|
|
|
|
|
raise HTTPException(status_code=403, detail="无权访问此章节")
|
2026-03-19 10:44:35 +08:00
|
|
|
|
if not chapter.is_active:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Chapter not found")
|
2026-03-22 16:45:57 +08:00
|
|
|
|
chapter = await self._ensure_chapter_materialized(chapter)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
await self._cleanup_unavailable_images(chapter)
|
2026-03-26 16:28:33 +08:00
|
|
|
|
if not chapter_meets_minimum_display(chapter):
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Chapter not found")
|
2026-03-20 10:30:07 +08:00
|
|
|
|
asset_map = await signed_urls_for_asset_ids(
|
|
|
|
|
|
self._db, collect_asset_ids_for_chapter(chapter)
|
|
|
|
|
|
)
|
|
|
|
|
|
return chapter_to_dict(chapter, asset_url_map=asset_map)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
|
|
|
|
|
async def disable_chapter(self, chapter_id: str, user_id: str) -> dict:
|
2026-03-20 15:15:35 +08:00
|
|
|
|
chapter = await repo.get_chapter_by_id(chapter_id, self._db)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
if not chapter:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Chapter not found")
|
|
|
|
|
|
if chapter.user_id != user_id:
|
|
|
|
|
|
raise HTTPException(status_code=403, detail="无权操作此章节")
|
2026-03-20 15:15:35 +08:00
|
|
|
|
cos_keys = await repo.collect_cos_storage_keys_for_chapter(self._db, chapter)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
chapter.is_active = False
|
|
|
|
|
|
await self._db.commit()
|
2026-03-20 15:15:35 +08:00
|
|
|
|
delete_object_storage_keys_best_effort(
|
|
|
|
|
|
self._object_storage,
|
|
|
|
|
|
cos_keys,
|
|
|
|
|
|
log_prefix=f"chapter_soft_delete id={chapter_id}",
|
|
|
|
|
|
)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
return {"status": "ok", "message": "章节已清除"}
|
|
|
|
|
|
|
2026-03-20 15:15:35 +08:00
|
|
|
|
async def set_chapter_story_order(
|
|
|
|
|
|
self, chapter_id: str, user_id: str, story_ids: list[str]
|
|
|
|
|
|
) -> dict:
|
|
|
|
|
|
chapter = await self._db.get(Chapter, chapter_id)
|
|
|
|
|
|
if not chapter:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Chapter not found")
|
|
|
|
|
|
if chapter.user_id != user_id:
|
|
|
|
|
|
raise HTTPException(status_code=403, detail="无权操作此章节")
|
|
|
|
|
|
if not chapter.is_active:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Chapter not found")
|
|
|
|
|
|
try:
|
|
|
|
|
|
await repo.replace_chapter_story_links_async(
|
|
|
|
|
|
self._db,
|
|
|
|
|
|
chapter_id=chapter_id,
|
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
|
story_ids=story_ids,
|
|
|
|
|
|
)
|
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
|
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
|
|
|
|
|
|
|
|
|
|
ch = await repo.get_chapter_with_story_links_for_compose(chapter_id, self._db)
|
|
|
|
|
|
if not ch:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Chapter not found")
|
|
|
|
|
|
if not ch.story_links:
|
|
|
|
|
|
md = ""
|
|
|
|
|
|
else:
|
|
|
|
|
|
md = materialize_chapter_markdown_from_loaded_chapter(ch)
|
|
|
|
|
|
await repo.append_chapter_compose_version_async(self._db, ch, md)
|
|
|
|
|
|
await self._db.commit()
|
|
|
|
|
|
return {"status": "ok", "chapter_id": chapter_id, "story_count": len(story_ids)}
|
|
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
async def get_memoir_state(self, user_id: str) -> dict:
|
|
|
|
|
|
from app.features.memoir.state_service import get_or_create_state
|
|
|
|
|
|
|
|
|
|
|
|
state = await get_or_create_state(user_id, self._db)
|
|
|
|
|
|
return state.model_dump()
|
|
|
|
|
|
|
|
|
|
|
|
async def get_next_question_context(self, user_id: str) -> dict:
|
|
|
|
|
|
from app.features.memoir.state_service import get_or_create_state
|
|
|
|
|
|
|
|
|
|
|
|
state = await get_or_create_state(user_id, self._db)
|
|
|
|
|
|
return {
|
|
|
|
|
|
"current_stage": state.current_stage,
|
|
|
|
|
|
"empty_slots": state.empty_slots_for_current_stage(),
|
|
|
|
|
|
"covered_stages": state.covered_stages,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-19 14:14:13 +08:00
|
|
|
|
async def check_and_trigger_cover_generation(self, user_id: str) -> dict:
|
|
|
|
|
|
"""
|
2026-03-22 16:45:57 +08:00
|
|
|
|
有正文、尚无 cover_asset、且封面 MemoirImage 未 completed 时,
|
2026-03-20 10:30:07 +08:00
|
|
|
|
派发 generate_chapter_cover(由 intent/asset 闭环完成)。
|
2026-03-19 14:14:13 +08:00
|
|
|
|
"""
|
2026-03-20 15:15:35 +08:00
|
|
|
|
from app.tasks.chapter_cover_enqueue import try_enqueue_generate_chapter_cover
|
2026-03-20 10:30:07 +08:00
|
|
|
|
|
|
|
|
|
|
img_settings = MemoirImageSettings.from_env()
|
|
|
|
|
|
if not img_settings.enabled:
|
|
|
|
|
|
return {"triggered": []}
|
2026-03-19 14:14:13 +08:00
|
|
|
|
|
2026-03-22 16:45:57 +08:00
|
|
|
|
chapters = await repo.get_chapters_for_memoir_list(
|
2026-03-19 14:14:13 +08:00
|
|
|
|
user_id, self._db, active_only=True, is_new_only=None
|
|
|
|
|
|
)
|
|
|
|
|
|
triggered: List[str] = []
|
|
|
|
|
|
for ch in chapters:
|
2026-03-22 16:45:57 +08:00
|
|
|
|
if not ch.category or not getattr(ch, "story_links", None):
|
2026-03-19 14:14:13 +08:00
|
|
|
|
continue
|
2026-03-20 10:30:07 +08:00
|
|
|
|
if getattr(ch, "cover_asset_id", None):
|
|
|
|
|
|
continue
|
|
|
|
|
|
md = (ch.canonical_markdown or "").strip()
|
2026-03-22 16:45:57 +08:00
|
|
|
|
body = strip_image_placeholders(md).strip() if md else ""
|
2026-03-20 10:30:07 +08:00
|
|
|
|
if not body:
|
|
|
|
|
|
continue
|
2026-03-20 15:15:35 +08:00
|
|
|
|
cover_rec = primary_chapter_memoir_image(ch)
|
|
|
|
|
|
if cover_rec and (cover_rec.status or "").strip() == "completed":
|
2026-03-19 14:14:13 +08:00
|
|
|
|
continue
|
2026-03-20 15:15:35 +08:00
|
|
|
|
enqueued = await asyncio.to_thread(
|
|
|
|
|
|
try_enqueue_generate_chapter_cover, ch.id, "http"
|
|
|
|
|
|
)
|
|
|
|
|
|
if enqueued:
|
2026-03-19 14:14:13 +08:00
|
|
|
|
triggered.append(ch.id)
|
2026-03-26 12:13:36 +08:00
|
|
|
|
logger.info("触发生成章节封面(asset): chapter={}", ch.id)
|
2026-03-19 14:14:13 +08:00
|
|
|
|
return {"triggered": triggered}
|
|
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
async def mark_memoir_read(self, user_id: str) -> dict:
|
2026-03-19 14:36:14 +08:00
|
|
|
|
stmt = select(Chapter).where(Chapter.user_id == user_id, Chapter.is_new == True)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
result = await self._db.execute(stmt)
|
|
|
|
|
|
for chapter in result.scalars().all():
|
|
|
|
|
|
chapter.is_new = False
|
|
|
|
|
|
stmt_book = (
|
2026-03-19 14:36:14 +08:00
|
|
|
|
select(Book).where(Book.user_id == user_id).order_by(Book.updated_at.desc())
|
2026-03-18 17:18:23 +08:00
|
|
|
|
)
|
|
|
|
|
|
result_book = await self._db.execute(stmt_book)
|
|
|
|
|
|
book = result_book.scalar_one_or_none()
|
|
|
|
|
|
if book:
|
|
|
|
|
|
book.has_update = False
|
|
|
|
|
|
await self._db.commit()
|
|
|
|
|
|
return {"status": "ok"}
|