2026-03-22 16:45:57 +08:00
|
|
|
|
"""ConversationMessage -> Redis conversation history 的纯映射(无 I/O)。"""
|
2026-03-20 15:15:35 +08:00
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
|
2026-03-22 16:45:57 +08:00
|
|
|
|
from app.features.conversation.models import ConversationMessage
|
2026-03-20 15:15:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-03-22 16:45:57 +08:00
|
|
|
|
def conversation_messages_to_redis_history(
|
|
|
|
|
|
rows: List[ConversationMessage],
|
|
|
|
|
|
) -> List[Dict[str, Any]]:
|
|
|
|
|
|
"""ConversationMessage 行 -> Redis conversation history 项。"""
|
2026-03-20 15:15:35 +08:00
|
|
|
|
history: List[Dict[str, Any]] = []
|
2026-03-22 16:45:57 +08:00
|
|
|
|
for row in rows:
|
|
|
|
|
|
item: Dict[str, Any] = {
|
|
|
|
|
|
"role": row.role,
|
|
|
|
|
|
"content": row.content,
|
|
|
|
|
|
"messageType": row.message_type,
|
|
|
|
|
|
"timestamp": row.created_at.isoformat() if row.created_at else None,
|
2026-03-20 15:15:35 +08:00
|
|
|
|
}
|
2026-03-22 16:45:57 +08:00
|
|
|
|
if row.voice_session_id:
|
|
|
|
|
|
item["voiceSessionId"] = row.voice_session_id
|
|
|
|
|
|
if row.duration_seconds:
|
|
|
|
|
|
item["durationSeconds"] = row.duration_seconds
|
|
|
|
|
|
if row.tts_audio_urls:
|
|
|
|
|
|
item["ttsAudioUrls"] = row.tts_audio_urls
|
|
|
|
|
|
history.append(item)
|
2026-03-20 15:15:35 +08:00
|
|
|
|
return history
|