2026-03-18 17:18:23 +08:00
|
|
|
|
"""Conversation service — 对话编排(列表、创建、结束、删除、消息、整理)。"""
|
|
|
|
|
|
|
2026-04-02 16:37:14 +08:00
|
|
|
|
import asyncio
|
2026-03-18 17:18:23 +08:00
|
|
|
|
import uuid
|
|
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
|
|
|
|
|
|
|
|
from fastapi import HTTPException
|
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
2026-03-20 16:36:42 +08:00
|
|
|
|
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,
|
|
|
|
|
|
)
|
2026-03-20 15:15:35 +08:00
|
|
|
|
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
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from app.features.conversation import repo
|
2026-04-01 11:49:33 +08:00
|
|
|
|
from app.features.conversation.models import Conversation, Segment
|
2026-03-22 16:45:57 +08:00
|
|
|
|
from app.features.conversation.session_history import (
|
|
|
|
|
|
conversation_messages_to_redis_history,
|
|
|
|
|
|
)
|
2026-03-26 15:51:24 +08:00
|
|
|
|
from app.features.conversation.tts_delivery import apply_presigned_tts_urls_to_messages
|
2026-03-20 15:15:35 +08:00
|
|
|
|
from app.features.memory import repo as memory_repo
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from app.features.quota.service import QuotaService
|
2026-03-20 15:15:35 +08:00
|
|
|
|
from app.ports.storage import ObjectStorage
|
2026-04-02 16:37:14 +08:00
|
|
|
|
from app.tasks.memoir_tasks import (
|
|
|
|
|
|
dispatch_pending_memoir_phase2_for_user,
|
|
|
|
|
|
process_memoir_phase1,
|
|
|
|
|
|
)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
|
|
|
|
|
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(
|
2026-03-19 14:36:14 +08:00
|
|
|
|
datetime.fromisoformat(raw_timestamp.replace("Z", "+00:00")).timestamp()
|
|
|
|
|
|
* 1000
|
2026-03-18 17:18:23 +08:00
|
|
|
|
)
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
pass
|
|
|
|
|
|
return _datetime_to_timestamp_ms(fallback)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _latest_message_time_ms(conversation: Conversation, history: list[dict]) -> int:
|
2026-03-31 23:55:26 +08:00
|
|
|
|
last_at = getattr(conversation, "last_message_at", None)
|
|
|
|
|
|
if last_at:
|
|
|
|
|
|
return _datetime_to_timestamp_ms(last_at)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
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)
|
2026-03-20 16:36:42 +08:00
|
|
|
|
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)]
|
2026-05-08 17:28:31 +08:00
|
|
|
|
dm = msg.get("durableMessageId")
|
|
|
|
|
|
if isinstance(dm, str) and dm:
|
|
|
|
|
|
item["durableMessageId"] = dm
|
2026-03-20 16:36:42 +08:00
|
|
|
|
messages.append(item)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
return messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConversationService:
|
2026-03-20 15:15:35 +08:00
|
|
|
|
def __init__(
|
|
|
|
|
|
self,
|
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
|
quota_service: QuotaService,
|
|
|
|
|
|
*,
|
|
|
|
|
|
object_storage: ObjectStorage | None = None,
|
|
|
|
|
|
):
|
2026-03-18 17:18:23 +08:00
|
|
|
|
self._db = db
|
|
|
|
|
|
self._quota = quota_service
|
2026-03-20 15:15:35 +08:00
|
|
|
|
self._object_storage = object_storage
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
2026-04-01 11:49:33 +08:00
|
|
|
|
async def ensure_ws_connection(
|
|
|
|
|
|
self, conversation_id: str, user_id: str
|
|
|
|
|
|
) -> tuple[Conversation | None, str]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
WebSocket:加载或创建对话。返回 (conversation, err)。
|
|
|
|
|
|
err 为空表示成功;否则为 forbidden | deleted。
|
|
|
|
|
|
"""
|
|
|
|
|
|
conv = await self._db.get(Conversation, conversation_id)
|
|
|
|
|
|
if not conv:
|
|
|
|
|
|
conv = Conversation(
|
|
|
|
|
|
id=conversation_id,
|
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
|
started_at=datetime.now(timezone.utc),
|
|
|
|
|
|
status="active",
|
|
|
|
|
|
)
|
|
|
|
|
|
self._db.add(conv)
|
|
|
|
|
|
await self._db.commit()
|
|
|
|
|
|
await self._db.refresh(conv)
|
|
|
|
|
|
return conv, ""
|
|
|
|
|
|
if conv.user_id != user_id:
|
|
|
|
|
|
return None, "forbidden"
|
|
|
|
|
|
if conv.deleted_at is not None:
|
|
|
|
|
|
return None, "deleted"
|
|
|
|
|
|
return conv, ""
|
|
|
|
|
|
|
|
|
|
|
|
async def create_user_segment(
|
|
|
|
|
|
self,
|
|
|
|
|
|
conversation: Conversation,
|
|
|
|
|
|
user_id: str,
|
|
|
|
|
|
text: str,
|
|
|
|
|
|
*,
|
|
|
|
|
|
audio_url: str | None = None,
|
|
|
|
|
|
audio_duration_seconds: int | None = None,
|
|
|
|
|
|
) -> Segment:
|
|
|
|
|
|
if conversation.user_id != user_id:
|
|
|
|
|
|
raise ValueError("conversation ownership mismatch")
|
|
|
|
|
|
segment = Segment(
|
|
|
|
|
|
id=str(uuid.uuid4()),
|
|
|
|
|
|
conversation_id=conversation.id,
|
|
|
|
|
|
user_input_text=text,
|
|
|
|
|
|
audio_url=audio_url,
|
|
|
|
|
|
audio_duration_seconds=audio_duration_seconds,
|
|
|
|
|
|
processed=False,
|
|
|
|
|
|
)
|
|
|
|
|
|
self._db.add(segment)
|
|
|
|
|
|
conversation.last_message_at = datetime.now(timezone.utc)
|
|
|
|
|
|
await self._db.commit()
|
|
|
|
|
|
await self._db.refresh(segment)
|
|
|
|
|
|
return segment
|
|
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
async def _clear_history(self, conversation_id: str) -> None:
|
|
|
|
|
|
try:
|
|
|
|
|
|
await redis_service.clear_conversation_history(conversation_id)
|
2026-04-01 11:49:33 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.debug("清空会话历史失败: {}", e)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
|
2026-03-22 16:45:57 +08:00
|
|
|
|
async def ensure_redis_history_from_db(self, conversation_id: str) -> list[dict]:
|
2026-03-20 15:15:35 +08:00
|
|
|
|
"""
|
2026-03-22 16:45:57 +08:00
|
|
|
|
供 WS 与 get_messages 使用:优先 Redis;若为空则用 DB conversation_messages 重建并写回。
|
2026-03-20 15:15:35 +08:00
|
|
|
|
"""
|
2026-03-22 16:45:57 +08:00
|
|
|
|
try:
|
|
|
|
|
|
history = await redis_service.get_conversation_history(conversation_id)
|
|
|
|
|
|
except Exception as exc:
|
2026-03-26 12:13:36 +08:00
|
|
|
|
logger.warning("conversation history cache read skipped: {}", exc)
|
2026-03-22 16:45:57 +08:00
|
|
|
|
history = []
|
2026-03-20 15:15:35 +08:00
|
|
|
|
if history:
|
|
|
|
|
|
return history
|
2026-03-22 16:45:57 +08:00
|
|
|
|
|
|
|
|
|
|
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:
|
2026-03-26 12:13:36 +08:00
|
|
|
|
logger.warning("conversation history cache write skipped: {}", exc)
|
2026-03-22 16:45:57 +08:00
|
|
|
|
return rebuilt
|
|
|
|
|
|
|
|
|
|
|
|
return []
|
2026-03-20 15:15:35 +08:00
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
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:
|
2026-03-22 16:45:57 +08:00
|
|
|
|
history = await self.ensure_redis_history_from_db(conv.id)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
except Exception:
|
|
|
|
|
|
pass
|
|
|
|
|
|
latest_message = history[-1].get("content", "")[:50] if history else None
|
2026-03-26 16:28:33 +08:00
|
|
|
|
has_user_message = any((msg.get("role") == "human") for msg in history)
|
2026-03-19 14:36:14 +08:00
|
|
|
|
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),
|
2026-03-31 23:55:26 +08:00
|
|
|
|
# 对话「初次创建」时间(ms),供客户端按日历日区分「打个招呼 / 继续对话」
|
|
|
|
|
|
"startedAt": _datetime_to_timestamp_ms(conv.started_at),
|
2026-03-19 14:36:14 +08:00
|
|
|
|
"unreadCount": 0,
|
|
|
|
|
|
"isDefaultAssistant": conv.summary is None,
|
2026-03-26 16:28:33 +08:00
|
|
|
|
"hasUserMessage": has_user_message,
|
2026-03-19 14:36:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
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)
|
2026-03-20 15:15:35 +08:00
|
|
|
|
if not conv or conv.user_id != user_id or conv.deleted_at is not None:
|
2026-03-18 17:18:23 +08:00
|
|
|
|
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)
|
2026-03-20 15:15:35 +08:00
|
|
|
|
cos_keys: set[str] = set(
|
|
|
|
|
|
await memory_repo.list_storage_keys_for_conversation(
|
|
|
|
|
|
self._db, conversation_id
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2026-03-20 16:36:42 +08:00
|
|
|
|
try:
|
|
|
|
|
|
hist = await redis_service.get_conversation_history(conversation_id)
|
|
|
|
|
|
cos_keys |= collect_cos_keys_from_conversation_history(hist)
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
pass
|
2026-03-20 15:15:35 +08:00
|
|
|
|
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)
|
2026-03-20 16:36:42 +08:00
|
|
|
|
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)]
|
|
|
|
|
|
)
|
2026-03-20 15:15:35 +08:00
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
await self._clear_history(conversation_id)
|
2026-03-20 15:15:35 +08:00
|
|
|
|
conv.deleted_at = datetime.now(timezone.utc)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
await self._db.commit()
|
|
|
|
|
|
|
2026-03-20 15:15:35 +08:00
|
|
|
|
delete_object_storage_keys_best_effort(
|
|
|
|
|
|
self._object_storage,
|
|
|
|
|
|
sorted(cos_keys),
|
|
|
|
|
|
log_prefix=f"conversation_soft_delete id={conversation_id}",
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
async def get_messages(self, conversation_id: str, user_id: str) -> list[dict]:
|
|
|
|
|
|
conv = await self.get_or_404(conversation_id, user_id)
|
|
|
|
|
|
try:
|
2026-03-22 16:45:57 +08:00
|
|
|
|
history = await self.ensure_redis_history_from_db(conversation_id)
|
2026-03-26 15:51:24 +08:00
|
|
|
|
messages = _build_messages_from_history(
|
2026-03-18 17:18:23 +08:00
|
|
|
|
conversation_id=conversation_id,
|
|
|
|
|
|
history=history,
|
|
|
|
|
|
fallback_timestamp=conv.started_at,
|
|
|
|
|
|
)
|
2026-03-26 15:51:24 +08:00
|
|
|
|
apply_presigned_tts_urls_to_messages(messages, self._object_storage)
|
|
|
|
|
|
return messages
|
2026-03-18 17:18:23 +08:00
|
|
|
|
except Exception:
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
2026-03-19 14:36:14 +08:00
|
|
|
|
async def organize(
|
|
|
|
|
|
self, conversation_id: str, user_id: str, subscription_type: str
|
|
|
|
|
|
) -> dict:
|
2026-03-18 17:18:23 +08:00
|
|
|
|
conv = await self.get_or_404(conversation_id, user_id)
|
2026-04-02 16:37:14 +08:00
|
|
|
|
pending_p1 = await repo.get_segments_pending_phase1(conversation_id, self._db)
|
|
|
|
|
|
has_p2 = await repo.conversation_has_pending_phase2(conversation_id, self._db)
|
|
|
|
|
|
if not pending_p1 and not has_p2:
|
2026-03-18 17:18:23 +08:00
|
|
|
|
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)
|
2026-04-02 16:37:14 +08:00
|
|
|
|
if pending_p1:
|
|
|
|
|
|
segment_ids = [s.id for s in pending_p1]
|
|
|
|
|
|
process_memoir_phase1.delay(conv.user_id, segment_ids)
|
|
|
|
|
|
logger.info(
|
|
|
|
|
|
"手动触发 Phase1: conversation_id={}, segments={}",
|
|
|
|
|
|
conversation_id,
|
|
|
|
|
|
len(segment_ids),
|
|
|
|
|
|
)
|
|
|
|
|
|
await asyncio.to_thread(dispatch_pending_memoir_phase2_for_user, conv.user_id)
|
2026-03-18 17:18:23 +08:00
|
|
|
|
return {
|
|
|
|
|
|
"message": "对话整理任务已提交",
|
|
|
|
|
|
"conversation_id": conversation_id,
|
2026-04-02 16:37:14 +08:00
|
|
|
|
"segments_count": len(pending_p1),
|
2026-03-18 17:18:23 +08:00
|
|
|
|
}
|