refactor(api,expo): 多智能体与会话收敛、回忆录兼容层移除、后端测试集大幅删减

- 对齐「多智能体收敛」与「回忆录 stories-first / markdown-first」方向:收紧运行时契约、
  删除过渡兼容路径与双轨逻辑,并同步更新客户端与文档。

- Chat:以 ChatOrchestrator 为实时编排入口;删除独立 conversation_agent,精简 prompts。
- Memoir:删除 memory_agent;MemoirOrchestrator、classification / story_route 与 prompts 收敛到
  prepare_batches + run_story_pipeline_for_category_batch 主链路。
- 将 agents 侧 processor 迁入 feature 层为 background_runner,并移除 features 下重复/过时
  processor 封装。

- 新增 history_store,强化「conversation_messages 为 DB 真源、Redis 为缓存」模型。
- 调整 models、repo、service、session_history;精简 WS message_types,重构 pipeline 与 router。

- 移除章节占位、整章再生等旧路径;章节列表与封面逻辑要求 story 关联;收紧 cover 资格与
  enqueue。
- helpers、repo、service、router、reading_segment_materialize、story_pipeline_sync、pdf_service
  等按 canonical markdown / cover_asset_id 收缩;删除 memoir_images/provider 等冗余。
- tasks:memoir_tasks、chapter_cover_tasks 等大幅瘦身;story_image_tasks 等与当前图片任务对齐。

- core:config、logging、redis、task_tracker 小幅调整。
- auth / user / payment / quota:路由或服务侧删减过时接口或逻辑(如 payment router 行数减少)。

- pyproject.toml、development.sh、.env.example / .env.production、README 等同步说明或变量。

- Alembic 0001_initial_schema 微调(与当前 schema 叙事一致的小改动)。

- 回忆录:types / mappers / api、章节页与 memoir 页与后端契约对齐;markdown-renderer 调整。
- 语音:删除 voice/player,voice-segment-store 相应精简。

- api/tests:删除 conftest 及绝大部分既有测试文件(websocket_baseline、conversation、memoir
  图片、PDF、SMS 等),属有意收缩/待按 backend-test-system 重建的信号。
- docs:新增多智能体收敛与移除兼容层计划摘要;更新 story-first 设计、backend-test-system、
  multi-agent-refactor-plan、实施总结等。

BREAKING CHANGE: 后端对外契约、回忆录章节字段与若干路由/任务行为已变更;大量 API 测试被移除,
  CI 若依赖这些用例需按新策略补测或调整流水线。
This commit is contained in:
Kevin
2026-03-22 16:45:57 +08:00
parent 70070216c4
commit 786ebf8ae6
122 changed files with 2802 additions and 7941 deletions

View File

@@ -12,9 +12,6 @@ from app.features.memoir.asset_resolver import collect_asset_ids_for_chapter
from app.features.memoir.chapter_markdown_compose import (
materialize_chapter_markdown_from_loaded_chapter,
)
from app.features.memoir.reading_segment_materialize import (
build_reading_segments_snapshot,
)
from app.features.memoir.models import (
Book,
Chapter,
@@ -23,6 +20,9 @@ from app.features.memoir.models import (
ChapterVersion,
MemoirState,
)
from app.features.memoir.reading_segment_materialize import (
build_reading_segments_snapshot,
)
from app.features.story.models import Story
@@ -64,19 +64,6 @@ async def get_chapters_for_memoir_list(
return list(result.unique().scalars().all())
async def get_chapters_with_sections(
user_id: str,
db: AsyncSession,
*,
active_only: bool = True,
is_new_only: bool | None = None,
) -> list[Chapter]:
"""兼容旧名:与 get_chapters_for_memoir_list 相同。"""
return await get_chapters_for_memoir_list(
user_id, db, active_only=active_only, is_new_only=is_new_only
)
async def get_chapter_by_id(chapter_id: str, db: AsyncSession) -> Chapter | None:
stmt = (
select(Chapter)
@@ -98,138 +85,6 @@ async def get_memoir_state(user_id: str, db: AsyncSession) -> MemoirState | None
return result.scalar_one_or_none()
def get_archived_chapter_summaries_sync(
session: Session, user_id: str, category: str
) -> list[tuple[str, str]]:
"""获取已删除is_active=False的同类别章节的标题与内容摘要供 AI 参考。"""
stmt = (
select(Chapter)
.where(
Chapter.user_id == user_id,
Chapter.category == category,
Chapter.is_active == False, # noqa: E712
)
.order_by(Chapter.updated_at.desc())
)
result = session.execute(stmt)
chapters = list(result.unique().scalars().all())
summaries: list[tuple[str, str]] = []
for ch in chapters:
combined = (getattr(ch, "canonical_markdown", None) or "").strip()
preview = (combined[:200] + "...") if len(combined) > 200 else combined
if preview.strip():
summaries.append((ch.title or "", preview))
return summaries
def ensure_chapter_markdown_and_version_sync(
session: Session,
chapter: Chapter,
markdown: str,
) -> None:
"""
为已有 chapter 设置 canonical_markdown 并创建 chapter_version。
供非 story 物化路径(如 save_chapter_markdown_sync使用。
"""
from sqlalchemy import func
count_stmt = select(func.count(ChapterVersion.id)).where(
ChapterVersion.chapter_id == chapter.id
)
version_no = (session.execute(count_stmt).scalar() or 0) + 1
version = ChapterVersion(
id=str(uuid.uuid4()),
chapter_id=chapter.id,
version_no=version_no,
markdown_snapshot=markdown,
actor_type="ai",
source_type="generate",
)
session.add(version)
session.flush()
chapter.canonical_markdown = markdown
chapter.current_version_id = version.id
def save_chapter_markdown_sync(
session: Session,
*,
user_id: str,
chapter_id: str | None,
title: str,
category: str,
order_index: int,
markdown: str,
source_segments: list[str] | None = None,
) -> Chapter:
"""
将 markdown 写入 chapter.canonical_markdown 和 chapter_versions。
Agent 不直接调用,由 service/task 调用。
若 chapter_id 为 None 则新建章节。
"""
if chapter_id:
chapter = session.get(Chapter, chapter_id)
if not chapter or chapter.user_id != user_id:
raise ValueError(f"Chapter {chapter_id} not found or access denied")
else:
chapter = Chapter(
id=str(uuid.uuid4()),
user_id=user_id,
title=title,
category=category,
order_index=order_index,
status="completed",
is_new=True,
is_active=True,
source_segments=source_segments or [],
)
session.add(chapter)
session.flush()
# 创建 chapter_version
from sqlalchemy import func
count_stmt = select(func.count(ChapterVersion.id)).where(
ChapterVersion.chapter_id == chapter.id
)
version_no = (session.execute(count_stmt).scalar() or 0) + 1
version = ChapterVersion(
id=str(uuid.uuid4()),
chapter_id=chapter.id,
version_no=version_no,
markdown_snapshot=markdown,
actor_type="ai",
source_type="generate",
)
session.add(version)
session.flush()
chapter.canonical_markdown = markdown
chapter.current_version_id = version.id
chapter.title = title
chapter.is_new = True
if source_segments:
chapter.source_segments = list(
set((chapter.source_segments or []) + source_segments)
)
session.flush()
session.refresh(chapter)
return chapter
async def count_chapter_story_links(db: AsyncSession, chapter_id: str) -> int:
stmt = (
select(func.count())
.select_from(ChapterStoryLink)
.where(ChapterStoryLink.chapter_id == chapter_id)
)
n = await db.scalar(stmt)
return int(n or 0)
async def get_chapter_ids_linked_to_story(db: AsyncSession, story_id: str) -> list[str]:
stmt = select(ChapterStoryLink.chapter_id).where(
ChapterStoryLink.story_id == story_id