Files
life-echo/api/app/features/conversation/service.py
Kevin 69a673e6c6 feat(api): 访谈人格/回复长度策略、口述归一、背景语气与输入净稿全链路
Chat 访谈
- 新增 persona 系统(default / warm_listener / curious_guide)与 background_voice 语气层
- 回复长度由 compute_reply_plan 统一决策(brief / standard / expanded),融合信息密度启发式
- 输入净稿(input_normalize):编排层可选 rules/llm 归一用户口语后再喂模型与记忆检索
- 记忆证据注入:按用户话检索 memory evidence 并注入 prompt

Memoir 回忆录
- 口述归一(oral_normalize):segment 原文保留,story 管线取派生净稿作叙事输入
- segment 入队批次门闸:累计字数 + 最长等待秒数,减少零碎提交
- fidelity_check / prompts / narrative_agent 微调
- Alembic 0005:清理跨章节 story 外键

Infra
- Dockerfile 加入 ffmpeg
- pyproject.toml 新增依赖并同步 uv.lock
- .env.example / .env.production 补全新配置项

Tests
- 新增 test_background_voice、test_chat_input_normalize、test_experience_regressions
- 扩展 test_interview_prompts、test_interview_reply_length、test_story_route_oral_invariant

Made-with: Cursor
2026-03-31 23:55:26 +08:00

291 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Conversation service — 对话编排(列表、创建、结束、删除、消息、整理)。"""
import uuid
from datetime import datetime, timezone
from fastapi import HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.cos_url_keys import (
collect_cos_keys_from_conversation_history,
collect_cos_keys_from_tts_url_list,
extract_cos_object_key_if_owned,
)
from app.core.logging import get_logger
from app.core.redis import redis_service
from app.core.storage_purge import delete_object_storage_keys_best_effort
from app.features.conversation import repo
from app.features.conversation.models import Conversation
from app.features.conversation.session_history import (
conversation_messages_to_redis_history,
)
from app.features.conversation.tts_delivery import apply_presigned_tts_urls_to_messages
from app.features.memory import repo as memory_repo
from app.features.quota.service import QuotaService
from app.ports.storage import ObjectStorage
from app.tasks.memoir_tasks import process_memoir_segments
logger = get_logger(__name__)
def _datetime_to_timestamp_ms(value: datetime | None) -> int:
if value is None:
return int(datetime.now(timezone.utc).timestamp() * 1000)
if value.tzinfo is None:
value = value.replace(tzinfo=timezone.utc)
return int(value.timestamp() * 1000)
def _message_timestamp_ms(msg: dict, fallback: datetime | None) -> int:
raw_timestamp = msg.get("timestamp")
if isinstance(raw_timestamp, (int, float)):
return int(raw_timestamp)
if isinstance(raw_timestamp, str):
try:
return int(
datetime.fromisoformat(raw_timestamp.replace("Z", "+00:00")).timestamp()
* 1000
)
except ValueError:
pass
return _datetime_to_timestamp_ms(fallback)
def _latest_message_time_ms(conversation: Conversation, history: list[dict]) -> int:
last_at = getattr(conversation, "last_message_at", None)
if last_at:
return _datetime_to_timestamp_ms(last_at)
if history:
return _message_timestamp_ms(history[-1], conversation.started_at)
return _datetime_to_timestamp_ms(conversation.started_at)
def _build_messages_from_history(
conversation_id: str,
history: list[dict],
fallback_timestamp: datetime | None,
) -> list[dict]:
messages: list[dict] = []
seen_audio_sessions: set[str] = set()
for idx, msg in enumerate(history):
role = msg.get("role")
message_type = msg.get("messageType", "text")
voice_session_id = msg.get("voiceSessionId")
if role == "human" and message_type == "audio" and voice_session_id:
if voice_session_id in seen_audio_sessions:
continue
seen_audio_sessions.add(voice_session_id)
item: dict = {
"id": f"{conversation_id}_msg_{idx}",
"conversationId": conversation_id,
"content": msg.get("content", ""),
"senderType": "user" if role == "human" else "assistant",
"timestamp": _message_timestamp_ms(msg, fallback_timestamp),
"messageType": message_type,
}
if voice_session_id and role == "human":
item["voiceSessionId"] = voice_session_id
ds = msg.get("durationSeconds")
if isinstance(ds, (int, float)) and ds > 0:
item["durationSeconds"] = int(ds)
if role == "ai":
tts = msg.get("ttsAudioUrls")
if isinstance(tts, list) and tts:
item["ttsAudioUrls"] = [x for x in tts if isinstance(x, str)]
messages.append(item)
return messages
class ConversationService:
def __init__(
self,
db: AsyncSession,
quota_service: QuotaService,
*,
object_storage: ObjectStorage | None = None,
):
self._db = db
self._quota = quota_service
self._object_storage = object_storage
async def _clear_history(self, conversation_id: str) -> None:
try:
await redis_service.clear_conversation_history(conversation_id)
except Exception:
pass
async def ensure_redis_history_from_db(self, conversation_id: str) -> list[dict]:
"""
供 WS 与 get_messages 使用:优先 Redis若为空则用 DB conversation_messages 重建并写回。
"""
try:
history = await redis_service.get_conversation_history(conversation_id)
except Exception as exc:
logger.warning("conversation history cache read skipped: {}", exc)
history = []
if history:
return history
rows = await repo.get_conversation_messages(conversation_id, self._db)
if rows:
rebuilt = conversation_messages_to_redis_history(rows)
try:
await redis_service.set_conversation_history(conversation_id, rebuilt)
except Exception as exc:
logger.warning("conversation history cache write skipped: {}", exc)
return rebuilt
return []
async def list_for_user(self, user_id: str) -> list[dict]:
conversations = await repo.get_user_conversations(user_id, self._db)
result = []
for conv in conversations:
history: list[dict] = []
try:
history = await self.ensure_redis_history_from_db(conv.id)
except Exception:
pass
latest_message = history[-1].get("content", "")[:50] if history else None
has_user_message = any((msg.get("role") == "human") for msg in history)
result.append(
{
"id": conv.id,
"title": (conv.summary or "")[:30] or "岁月知己",
"avatarUrl": None,
"latestMessagePreview": latest_message or conv.summary,
"latestMessageTime": _latest_message_time_ms(conv, history),
# 对话「初次创建」时间ms供客户端按日历日区分「打个招呼 / 继续对话」
"startedAt": _datetime_to_timestamp_ms(conv.started_at),
"unreadCount": 0,
"isDefaultAssistant": conv.summary is None,
"hasUserMessage": has_user_message,
}
)
return result
async def create(self, user_id: str) -> dict:
conv = Conversation(
id=str(uuid.uuid4()),
user_id=user_id,
started_at=datetime.now(timezone.utc),
status="active",
)
repo.add_conversation(conv, self._db)
await self._db.commit()
await self._db.refresh(conv)
return {
"id": conv.id,
"user_id": conv.user_id,
"started_at": conv.started_at.isoformat(),
"status": conv.status,
}
async def get_or_404(self, conversation_id: str, user_id: str) -> Conversation:
conv = await repo.get_conversation(conversation_id, self._db)
if not conv or conv.user_id != user_id or conv.deleted_at is not None:
raise HTTPException(status_code=404, detail="Conversation not found")
return conv
async def get_one(self, conversation_id: str, user_id: str) -> dict:
conv = await self.get_or_404(conversation_id, user_id)
return {
"id": conv.id,
"user_id": conv.user_id,
"started_at": conv.started_at.isoformat(),
"ended_at": conv.ended_at.isoformat() if conv.ended_at else None,
"duration_seconds": conv.duration_seconds,
"summary": conv.summary,
"status": conv.status,
"current_topic": conv.current_topic,
"conversation_stage": conv.conversation_stage,
}
async def end(self, conversation_id: str, user_id: str) -> dict:
conv = await self.get_or_404(conversation_id, user_id)
conv.status = "ended"
conv.ended_at = datetime.now(timezone.utc)
if conv.started_at:
conv.duration_seconds = int(
(conv.ended_at - conv.started_at).total_seconds()
)
await self._db.commit()
return {
"id": conv.id,
"status": conv.status,
"ended_at": conv.ended_at.isoformat(),
"duration_seconds": conv.duration_seconds,
}
async def delete(self, conversation_id: str, user_id: str) -> None:
conv = await self.get_or_404(conversation_id, user_id)
cos_keys: set[str] = set(
await memory_repo.list_storage_keys_for_conversation(
self._db, conversation_id
)
)
try:
hist = await redis_service.get_conversation_history(conversation_id)
cos_keys |= collect_cos_keys_from_conversation_history(hist)
except Exception:
pass
segments = await repo.get_segments_for_conversation(conversation_id, self._db)
for seg in segments:
k = extract_cos_object_key_if_owned(seg.audio_url)
if k:
cos_keys.add(k)
raw_tts = getattr(seg, "tts_audio_urls", None)
if isinstance(raw_tts, list):
cos_keys |= collect_cos_keys_from_tts_url_list(
[str(x) for x in raw_tts if isinstance(x, str)]
)
await self._clear_history(conversation_id)
conv.deleted_at = datetime.now(timezone.utc)
await self._db.commit()
delete_object_storage_keys_best_effort(
self._object_storage,
sorted(cos_keys),
log_prefix=f"conversation_soft_delete id={conversation_id}",
)
async def get_messages(self, conversation_id: str, user_id: str) -> list[dict]:
conv = await self.get_or_404(conversation_id, user_id)
try:
history = await self.ensure_redis_history_from_db(conversation_id)
messages = _build_messages_from_history(
conversation_id=conversation_id,
history=history,
fallback_timestamp=conv.started_at,
)
apply_presigned_tts_urls_to_messages(messages, self._object_storage)
return messages
except Exception:
return []
async def organize(
self, conversation_id: str, user_id: str, subscription_type: str
) -> dict:
conv = await self.get_or_404(conversation_id, user_id)
segments = await repo.get_segments_for_organize(conversation_id, self._db)
if not segments:
raise HTTPException(status_code=400, detail="该对话没有可整理的内容")
can_submit, quota_message = await self._quota.check_can_submit_organize(
user_id, subscription_type
)
if not can_submit:
raise HTTPException(status_code=403, detail=quota_message)
segment_ids = [s.id for s in segments]
process_memoir_segments.delay(conv.user_id, segment_ids)
logger.info(
"手动触发对话整理: conversation_id={}, segments={}",
conversation_id,
len(segment_ids),
)
return {
"message": "对话整理任务已提交",
"conversation_id": conversation_id,
"segments_count": len(segment_ids),
}