feat(i18n): persist language preference and thread through chat, memoir, TTS
- Add users.language_preference (Alembic 0018, default zh); capture at signup/SMS only; expose on auth and profile APIs - Lite English prompts for chat and memoir; localized stage labels and agent names (Life Echo / 岁月知己) - Tencent TTS: language-aware synthesis, ModelType=1 for 501004, English chunking - WebSocket pipeline: emit all AGENT_RESPONSE segments when TTS cancels; INFO logs for tts_this_turn and TTS decisions; on-demand TTS logging - Expo: device language on auth, i18n tiers/agent name, [SPLIT] streaming UX fixes - Tests for migration, prompts, pipeline, router tts_this_turn, reply segments Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -13,7 +13,7 @@ from app.agents.memoir.prompts import (
|
||||
get_narrative_merge_json_prompt,
|
||||
)
|
||||
from app.agents.memoir.schemas import MemoirTitleOutput
|
||||
from app.agents.stage_constants import CHAPTER_CATEGORIES
|
||||
from app.agents.stage_constants import CHAPTER_CATEGORIES, chapter_category_display
|
||||
from app.core.config import settings
|
||||
from app.core.langchain_llm import invoke_json_object
|
||||
from app.core.llm_call import llm_json_call
|
||||
@@ -22,6 +22,13 @@ from app.core.logging import get_logger
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
def _default_title_for(stage: str, language: str) -> str:
|
||||
if language == "en":
|
||||
cat = chapter_category_display(stage, language="en") or stage
|
||||
return f"{cat} Memory"
|
||||
return f"{CHAPTER_CATEGORIES.get(stage, stage)} 回忆"
|
||||
|
||||
|
||||
class NarrativeAgent:
|
||||
"""生成章节标题和叙事正文"""
|
||||
|
||||
@@ -33,10 +40,11 @@ class NarrativeAgent:
|
||||
user_profile: str = "",
|
||||
birth_year: Optional[int] = None,
|
||||
llm: Any = None,
|
||||
language: str = "zh",
|
||||
) -> str:
|
||||
"""生成创意标题。若无 LLM 则返回默认标题"""
|
||||
if not llm:
|
||||
return f"{CHAPTER_CATEGORIES.get(stage, stage)} 回忆"
|
||||
return _default_title_for(stage, language)
|
||||
try:
|
||||
prompt = get_creative_title_json_prompt(
|
||||
stage=stage,
|
||||
@@ -44,8 +52,9 @@ class NarrativeAgent:
|
||||
slots=slots,
|
||||
user_profile=user_profile,
|
||||
birth_year=birth_year,
|
||||
language=language,
|
||||
)
|
||||
default_title = f"{CHAPTER_CATEGORIES.get(stage, stage)} 回忆"
|
||||
default_title = _default_title_for(stage, language)
|
||||
|
||||
def _title_fallback() -> MemoirTitleOutput:
|
||||
return MemoirTitleOutput(title=default_title)
|
||||
@@ -64,7 +73,7 @@ class NarrativeAgent:
|
||||
return default_title
|
||||
except Exception as e:
|
||||
logger.warning("NarrativeAgent 生成标题失败: {}", e)
|
||||
return f"{CHAPTER_CATEGORIES.get(stage, stage)} 回忆"
|
||||
return _default_title_for(stage, language)
|
||||
|
||||
def generate_narrative(
|
||||
self,
|
||||
@@ -79,6 +88,7 @@ class NarrativeAgent:
|
||||
occupation: str = "",
|
||||
*,
|
||||
fallback_plain_oral: str = "",
|
||||
language: str = "zh",
|
||||
) -> str:
|
||||
"""将新对话改写为叙述。若无 LLM 则直接拼接。
|
||||
|
||||
@@ -106,6 +116,7 @@ class NarrativeAgent:
|
||||
birth_year=birth_year,
|
||||
background_voice=background_voice,
|
||||
occupation=occupation,
|
||||
language=language,
|
||||
)
|
||||
max_tokens = int(settings.memoir_narrative_merge_max_tokens)
|
||||
agent_name = "NarrativeAgent.generate_narrative_merge"
|
||||
@@ -119,6 +130,7 @@ class NarrativeAgent:
|
||||
birth_year=birth_year,
|
||||
background_voice=background_voice,
|
||||
occupation=occupation,
|
||||
language=language,
|
||||
)
|
||||
max_tokens = int(settings.memoir_narrative_max_tokens)
|
||||
agent_name = "NarrativeAgent.generate_narrative"
|
||||
|
||||
Reference in New Issue
Block a user