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:
@@ -1,12 +1,10 @@
|
||||
"""聊天模块:AI 回复用户(ProfileAgent + InterviewAgent + ChatOrchestrator)"""
|
||||
|
||||
from app.agents.chat.conversation_agent import ConversationAgent
|
||||
from app.agents.chat.interview_agent import InterviewAgent
|
||||
from app.agents.chat.orchestrator import ChatOrchestrator
|
||||
from app.agents.chat.profile_agent import ProfileAgent
|
||||
from app.agents.chat.interview_agent import InterviewAgent
|
||||
|
||||
__all__ = [
|
||||
"ConversationAgent",
|
||||
"ChatOrchestrator",
|
||||
"ProfileAgent",
|
||||
"InterviewAgent",
|
||||
|
||||
@@ -1,147 +0,0 @@
|
||||
"""
|
||||
对话 Agent:Facade,内部委托 ChatOrchestrator + ProfileAgent + InterviewAgent
|
||||
保留原有对外 API,供 router 等调用方兼容使用
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from app.agents.chat.agent_turn import AgentChatTurn
|
||||
from app.agents.chat.orchestrator import ChatOrchestrator
|
||||
from app.agents.chat.prompts_conversation import ConversationStage
|
||||
from app.agents.state_schema import MemoirStateSchema
|
||||
from app.core.redis import redis_service
|
||||
|
||||
|
||||
class ConversationAgent:
|
||||
"""对话 Agent Facade,委托 ChatOrchestrator 实现多 Agent 协同"""
|
||||
|
||||
def __init__(self):
|
||||
self._orchestrator = ChatOrchestrator()
|
||||
|
||||
async def extract_profile_from_message(
|
||||
self,
|
||||
user_message: str,
|
||||
missing_fields: List[str],
|
||||
conversation_id: Optional[str] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""委托 ChatOrchestrator/ProfileAgent 提取资料"""
|
||||
return await self._orchestrator.extract_profile_from_message(
|
||||
user_message, missing_fields, conversation_id=conversation_id
|
||||
)
|
||||
|
||||
async def generate_profile_followup(
|
||||
self,
|
||||
conversation_id: str,
|
||||
user_message: str,
|
||||
missing_fields: List[str],
|
||||
filled_fields: Dict[str, str],
|
||||
nickname: str = "",
|
||||
is_from_voice: bool = False,
|
||||
voice_session_id: str | None = None,
|
||||
user_message_timestamp: datetime | None = None,
|
||||
audio_duration_seconds: int | None = None,
|
||||
) -> List[str]:
|
||||
"""委托 ChatOrchestrator/ProfileAgent 生成资料追问"""
|
||||
return await self._orchestrator.generate_profile_followup(
|
||||
conversation_id=conversation_id,
|
||||
user_message=user_message,
|
||||
missing_fields=missing_fields,
|
||||
filled_fields=filled_fields,
|
||||
nickname=nickname,
|
||||
is_from_voice=is_from_voice,
|
||||
voice_session_id=voice_session_id,
|
||||
user_message_timestamp=user_message_timestamp,
|
||||
audio_duration_seconds=audio_duration_seconds,
|
||||
)
|
||||
|
||||
async def generate_profile_greeting(
|
||||
self,
|
||||
conversation_id: str,
|
||||
missing_fields: List[str],
|
||||
nickname: str = "",
|
||||
) -> List[str]:
|
||||
"""委托 ChatOrchestrator/ProfileAgent 生成资料收集开场白"""
|
||||
return await self._orchestrator.generate_profile_greeting(
|
||||
conversation_id=conversation_id,
|
||||
missing_fields=missing_fields,
|
||||
nickname=nickname,
|
||||
)
|
||||
|
||||
async def generate_response_with_state(
|
||||
self,
|
||||
conversation_id: str,
|
||||
user_message: str,
|
||||
memoir_state: MemoirStateSchema,
|
||||
user_profile_context: str = "",
|
||||
is_from_voice: bool = False,
|
||||
voice_session_id: str | None = None,
|
||||
user_message_timestamp: datetime | None = None,
|
||||
audio_duration_seconds: int | None = None,
|
||||
) -> AgentChatTurn:
|
||||
"""委托 ChatOrchestrator/InterviewAgent 生成访谈回复"""
|
||||
return await self._orchestrator.generate_response_with_state(
|
||||
conversation_id=conversation_id,
|
||||
user_message=user_message,
|
||||
memoir_state=memoir_state,
|
||||
user_profile_context=user_profile_context,
|
||||
is_from_voice=is_from_voice,
|
||||
voice_session_id=voice_session_id,
|
||||
user_message_timestamp=user_message_timestamp,
|
||||
audio_duration_seconds=audio_duration_seconds,
|
||||
)
|
||||
|
||||
async def generate_opening_message(
|
||||
self,
|
||||
conversation_id: str,
|
||||
memoir_state: MemoirStateSchema,
|
||||
user_profile_context: str = "",
|
||||
) -> List[str]:
|
||||
"""委托 ChatOrchestrator/InterviewAgent 生成开场白"""
|
||||
return await self._orchestrator.generate_opening_message(
|
||||
conversation_id=conversation_id,
|
||||
memoir_state=memoir_state,
|
||||
user_profile_context=user_profile_context,
|
||||
)
|
||||
|
||||
async def generate_response(
|
||||
self,
|
||||
conversation_id: str,
|
||||
user_message: str,
|
||||
current_stage: Optional[ConversationStage] = None,
|
||||
covered_topics: Optional[List[str]] = None,
|
||||
) -> str:
|
||||
"""兼容旧 API:生成简单回复(无状态感知),委托 InterviewAgent 的等价逻辑"""
|
||||
from app.agents.state_schema import default_state
|
||||
|
||||
state = default_state()
|
||||
state.current_stage = (current_stage or ConversationStage.CHILDHOOD).value
|
||||
state.covered_stages = covered_topics or []
|
||||
turn = await self._orchestrator.generate_response_with_state(
|
||||
conversation_id=conversation_id,
|
||||
user_message=user_message,
|
||||
memoir_state=state,
|
||||
user_profile_context="",
|
||||
)
|
||||
return turn.messages[0] if turn.messages else ""
|
||||
|
||||
def detect_stage(
|
||||
self, conversation_id: str, user_message: str
|
||||
) -> ConversationStage:
|
||||
"""根据关键词检测用户阶段(兼容 API)"""
|
||||
detected = self._orchestrator.detect_user_stage(user_message)
|
||||
if detected == "childhood":
|
||||
return ConversationStage.CHILDHOOD
|
||||
if detected == "education":
|
||||
return ConversationStage.EDUCATION
|
||||
if detected == "career":
|
||||
return ConversationStage.CAREER
|
||||
if detected == "family":
|
||||
return ConversationStage.FAMILY
|
||||
if detected == "belief":
|
||||
return ConversationStage.BELIEFS
|
||||
return ConversationStage.CHILDHOOD
|
||||
|
||||
async def clear_memory(self, conversation_id: str) -> None:
|
||||
"""清除 Redis 中的对话历史"""
|
||||
await redis_service.clear_conversation_history(conversation_id)
|
||||
@@ -1,6 +1,6 @@
|
||||
"""
|
||||
ChatOrchestrator:AI 回复用户模块的编排层
|
||||
负责路由(Profile vs Interview)、调用 Specialist Agent、统一 Redis 持久化与错误处理
|
||||
负责路由(Profile vs Interview)、调用 Specialist Agent;持久化由 feature 层 ConversationHistoryStore 完成。
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
@@ -9,7 +9,6 @@ from typing import TYPE_CHECKING, List, Optional
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.agents.chat.agent_turn import AgentChatTurn
|
||||
from app.agents.chat.helpers import save_message
|
||||
from app.agents.chat.interview_agent import InterviewAgent
|
||||
from app.agents.chat.profile_agent import ProfileAgent
|
||||
from app.agents.state_schema import MemoirStateSchema
|
||||
@@ -28,8 +27,8 @@ _UNAUTH_TURN = AgentChatTurn(
|
||||
|
||||
class ChatOrchestrator:
|
||||
"""
|
||||
聊天编排器:根据用户资料完成度路由到 ProfileAgent 或 InterviewAgent,
|
||||
统一管理 Redis 写入。
|
||||
聊天编排器:根据用户资料完成度路由到 ProfileAgent 或 InterviewAgent。
|
||||
不直接写入 Redis/DB;由 WS pipeline / ConversationHistoryStore 落库并同步缓存。
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
@@ -53,8 +52,7 @@ class ChatOrchestrator:
|
||||
) -> AgentChatTurn:
|
||||
"""
|
||||
处理用户消息,返回 AI 回复(分段 + 是否跳过 TTS)。
|
||||
根据 missing_fields 路由到 ProfileAgent 或 InterviewAgent,
|
||||
统一写入 Redis。
|
||||
根据 missing_fields 路由到 ProfileAgent 或 InterviewAgent。
|
||||
"""
|
||||
|
||||
# --- 资料收集模式 ---
|
||||
@@ -77,15 +75,6 @@ class ChatOrchestrator:
|
||||
filled_fields=filled,
|
||||
nickname=user.nickname or "",
|
||||
)
|
||||
await self._save_messages(
|
||||
conversation_id=conversation_id,
|
||||
user_message=user_message,
|
||||
response_text="\n\n".join(responses),
|
||||
is_from_voice=is_from_voice,
|
||||
voice_session_id=voice_session_id,
|
||||
user_message_timestamp=user_message_timestamp,
|
||||
audio_duration_seconds=audio_duration_seconds,
|
||||
)
|
||||
return AgentChatTurn(messages=responses, skip_tts=False)
|
||||
except Exception as e:
|
||||
logger.error(f"资料收集处理失败: {e}", exc_info=True)
|
||||
@@ -111,52 +100,12 @@ class ChatOrchestrator:
|
||||
occupation=user.occupation,
|
||||
)
|
||||
|
||||
turn = await self.interview_agent.generate_response_with_state(
|
||||
return await self.interview_agent.generate_response_with_state(
|
||||
conversation_id=conversation_id,
|
||||
user_message=user_message,
|
||||
memoir_state=state,
|
||||
user_profile_context=user_profile_context,
|
||||
)
|
||||
await self._save_messages(
|
||||
conversation_id=conversation_id,
|
||||
user_message=user_message,
|
||||
response_text="\n\n".join(turn.messages),
|
||||
is_from_voice=is_from_voice,
|
||||
voice_session_id=voice_session_id,
|
||||
user_message_timestamp=user_message_timestamp,
|
||||
audio_duration_seconds=audio_duration_seconds,
|
||||
)
|
||||
return turn
|
||||
|
||||
async def _save_messages(
|
||||
self,
|
||||
conversation_id: str,
|
||||
user_message: str,
|
||||
response_text: str,
|
||||
is_from_voice: bool = False,
|
||||
voice_session_id: Optional[str] = None,
|
||||
user_message_timestamp: Optional[datetime] = None,
|
||||
audio_duration_seconds: Optional[int] = None,
|
||||
) -> None:
|
||||
"""统一写入 Human + AI 消息到 Redis"""
|
||||
human_msg_type = "audio" if is_from_voice else "text"
|
||||
human_duration = (
|
||||
audio_duration_seconds
|
||||
if is_from_voice
|
||||
and audio_duration_seconds is not None
|
||||
and audio_duration_seconds > 0
|
||||
else None
|
||||
)
|
||||
await save_message(
|
||||
conversation_id,
|
||||
"human",
|
||||
user_message,
|
||||
message_type=human_msg_type,
|
||||
voice_session_id=voice_session_id,
|
||||
timestamp=user_message_timestamp,
|
||||
audio_duration_seconds=human_duration,
|
||||
)
|
||||
await save_message(conversation_id, "ai", response_text)
|
||||
|
||||
async def extract_profile_from_message(
|
||||
self,
|
||||
@@ -181,25 +130,14 @@ class ChatOrchestrator:
|
||||
user_message_timestamp: datetime | None = None,
|
||||
audio_duration_seconds: int | None = None,
|
||||
) -> List[str]:
|
||||
"""委托 ProfileAgent 生成资料追问,并写入 Redis"""
|
||||
responses = await self.profile_agent.generate_profile_followup(
|
||||
"""委托 ProfileAgent 生成资料追问(持久化由调用方负责)。"""
|
||||
return await self.profile_agent.generate_profile_followup(
|
||||
conversation_id=conversation_id,
|
||||
user_message=user_message,
|
||||
missing_fields=missing_fields,
|
||||
filled_fields=filled_fields,
|
||||
nickname=nickname,
|
||||
)
|
||||
response_text = "\n\n".join(responses)
|
||||
await self._save_messages(
|
||||
conversation_id=conversation_id,
|
||||
user_message=user_message,
|
||||
response_text=response_text,
|
||||
is_from_voice=is_from_voice,
|
||||
voice_session_id=voice_session_id,
|
||||
user_message_timestamp=user_message_timestamp,
|
||||
audio_duration_seconds=audio_duration_seconds,
|
||||
)
|
||||
return responses
|
||||
|
||||
async def generate_profile_greeting(
|
||||
self,
|
||||
@@ -207,15 +145,12 @@ class ChatOrchestrator:
|
||||
missing_fields: List[str],
|
||||
nickname: str = "",
|
||||
) -> List[str]:
|
||||
"""委托 ProfileAgent 生成资料收集开场白,并写入 Redis"""
|
||||
responses = await self.profile_agent.generate_profile_greeting(
|
||||
"""委托 ProfileAgent 生成资料收集开场白(持久化由调用方负责)。"""
|
||||
return await self.profile_agent.generate_profile_greeting(
|
||||
conversation_id=conversation_id,
|
||||
missing_fields=missing_fields,
|
||||
nickname=nickname,
|
||||
)
|
||||
response_text = "\n\n".join(responses)
|
||||
await save_message(conversation_id, "ai", response_text)
|
||||
return responses
|
||||
|
||||
async def generate_response_with_state(
|
||||
self,
|
||||
@@ -228,24 +163,13 @@ class ChatOrchestrator:
|
||||
user_message_timestamp: datetime | None = None,
|
||||
audio_duration_seconds: int | None = None,
|
||||
) -> AgentChatTurn:
|
||||
"""委托 InterviewAgent 生成访谈回复,并写入 Redis"""
|
||||
turn = await self.interview_agent.generate_response_with_state(
|
||||
"""委托 InterviewAgent 生成访谈回复(持久化由调用方负责)。"""
|
||||
return await self.interview_agent.generate_response_with_state(
|
||||
conversation_id=conversation_id,
|
||||
user_message=user_message,
|
||||
memoir_state=memoir_state,
|
||||
user_profile_context=user_profile_context,
|
||||
)
|
||||
response_text = "\n\n".join(turn.messages)
|
||||
await self._save_messages(
|
||||
conversation_id=conversation_id,
|
||||
user_message=user_message,
|
||||
response_text=response_text,
|
||||
is_from_voice=is_from_voice,
|
||||
voice_session_id=voice_session_id,
|
||||
user_message_timestamp=user_message_timestamp,
|
||||
audio_duration_seconds=audio_duration_seconds,
|
||||
)
|
||||
return turn
|
||||
|
||||
def detect_user_stage(self, user_message: str) -> str:
|
||||
"""委托 InterviewAgent 检测用户阶段"""
|
||||
@@ -258,16 +182,10 @@ class ChatOrchestrator:
|
||||
user_profile_context: str = "",
|
||||
) -> List[str]:
|
||||
"""
|
||||
委托 InterviewAgent 生成访谈开场白,并写入 Redis。
|
||||
|
||||
调用方(如 WS)须在「空会话」分支前通过 ConversationService 从 DB 回填 Redis,
|
||||
避免与多 Agent 契约混淆:本编排器不读取 segments,只假定 Redis 已反映是否已有轮次。
|
||||
委托 InterviewAgent 生成访谈开场白(持久化由调用方 ConversationHistoryStore 负责)。
|
||||
"""
|
||||
responses = await self.interview_agent.generate_opening_message(
|
||||
return await self.interview_agent.generate_opening_message(
|
||||
conversation_id=conversation_id,
|
||||
memoir_state=memoir_state,
|
||||
user_profile_context=user_profile_context,
|
||||
)
|
||||
response_text = "\n\n".join(responses)
|
||||
await save_message(conversation_id, "ai", response_text)
|
||||
return responses
|
||||
|
||||
@@ -17,7 +17,6 @@ from app.agents.chat.prompts_conversation import (
|
||||
ConversationStage,
|
||||
INTERVIEW_QUESTIONS,
|
||||
SLOT_NAME_MAP,
|
||||
get_conversation_prompt,
|
||||
get_guided_conversation_prompt,
|
||||
get_opening_prompt,
|
||||
get_questions_for_stage,
|
||||
@@ -34,7 +33,6 @@ __all__ = [
|
||||
"ConversationStage",
|
||||
"INTERVIEW_QUESTIONS",
|
||||
"SLOT_NAME_MAP",
|
||||
"get_conversation_prompt",
|
||||
"get_guided_conversation_prompt",
|
||||
"get_opening_prompt",
|
||||
"get_questions_for_stage",
|
||||
|
||||
@@ -465,12 +465,3 @@ def get_guided_conversation_prompt(
|
||||
直接输出你要说的话(多条消息用 [SPLIT] 分隔):"""
|
||||
|
||||
return prompt
|
||||
|
||||
|
||||
def get_conversation_prompt(
|
||||
current_stage: ConversationStage,
|
||||
covered_topics: List[str],
|
||||
user_latest_response: str,
|
||||
) -> str:
|
||||
"""向后兼容的函数"""
|
||||
return get_system_prompt(current_stage, covered_topics, user_latest_response)
|
||||
|
||||
Reference in New Issue
Block a user