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:
Kevin
2026-05-11 16:16:49 +08:00
parent 5ce29aad64
commit ccdc4e4277
64 changed files with 3233 additions and 208 deletions

View File

@@ -41,13 +41,16 @@ def _polish_story_title(
llm,
*,
chapter_category: str,
language: str = "zh",
) -> bool:
"""Re-generate title if current title is a placeholder. Returns True if updated."""
from app.features.memoir.story_pipeline_sync import _placeholder_title
current = (story.title or "").strip()
placeholder = _placeholder_title(chapter_category)
if current and current != placeholder:
placeholder_zh = _placeholder_title(chapter_category, language="zh")
placeholder_en = _placeholder_title(chapter_category, language="en")
placeholder = _placeholder_title(chapter_category, language=language)
if current and current not in (placeholder_zh, placeholder_en):
return False
body = (story.canonical_markdown or "").strip()
@@ -63,9 +66,10 @@ def _polish_story_title(
user_profile="",
birth_year=None,
llm=llm,
language=language,
)
new_title = (new_title or "").strip()
if not new_title or new_title == placeholder:
if not new_title or new_title in (placeholder_zh, placeholder_en, placeholder):
return False
story.title = new_title
@@ -138,6 +142,16 @@ def memoir_quality_pass(
chapters_dirtied: set[str] = set()
with get_sync_db() as db:
from app.features.user.models import User
user_obj = db.get(User, user_id)
user_language = (
"en"
if user_obj is not None
and str(getattr(user_obj, "language_preference", "zh") or "zh").lower()
== "en"
else "zh"
)
for sid in story_ids:
story = db.get(Story, sid)
if not story or story.user_id != user_id:
@@ -145,7 +159,11 @@ def memoir_quality_pass(
chapter_category = story.stage or "summary"
if _polish_story_title(
db, story, llm, chapter_category=chapter_category
db,
story,
llm,
chapter_category=chapter_category,
language=user_language,
):
titles_polished += 1
stmt = select(Chapter.id).where(

View File

@@ -666,13 +666,21 @@ def process_memoir_phase2(
user_birth_year = None
background_voice = "default"
user_occupation = ""
user_language = "zh"
if user_obj:
user_birth_year = user_obj.birth_year
user_language = (
"en"
if str(getattr(user_obj, "language_preference", "zh") or "zh").lower()
== "en"
else "zh"
)
user_profile = format_user_profile_context(
birth_year=user_obj.birth_year,
birth_place=user_obj.birth_place,
grew_up_place=user_obj.grew_up_place,
occupation=user_obj.occupation,
language=user_language,
)
background_voice = infer_background_voice(user_obj.occupation)
user_occupation = user_obj.occupation or ""
@@ -752,6 +760,7 @@ def process_memoir_phase2(
memoir_correlation_id=cid,
llm_fast=llm_fast,
memory_evidence=memory_evidence,
language=user_language,
)
pipeline_elapsed = time.perf_counter() - pipeline_t0
@@ -931,6 +940,14 @@ def process_memoir_phase1(self, user_id: str, segment_ids: List[str]):
try:
with get_sync_db() as db:
user_obj_for_lang = db.get(User, user_id)
user_language = (
"en"
if user_obj_for_lang is not None
and str(getattr(user_obj_for_lang, "language_preference", "zh") or "zh").lower()
== "en"
else "zh"
)
stmt = (
select(Segment)
.where(Segment.id.in_(segment_ids))
@@ -1056,6 +1073,7 @@ def process_memoir_phase1(self, user_id: str, segment_ids: List[str]):
memoir_batch=True,
),
on_phase1_chunk=_phase1_chunk_cb,
language=user_language,
)
prep_elapsed = time.perf_counter() - prep_t0
merge_pipeline_run(
@@ -1273,13 +1291,21 @@ def generate_chapter_content(self, user_id: str, stage: str, new_content: str):
user_birth_year = None
background_voice = "default"
user_occupation = ""
user_language = "zh"
if user_obj:
user_birth_year = user_obj.birth_year
user_language = (
"en"
if str(getattr(user_obj, "language_preference", "zh") or "zh").lower()
== "en"
else "zh"
)
user_profile = format_user_profile_context(
birth_year=user_obj.birth_year,
birth_place=user_obj.birth_place,
grew_up_place=user_obj.grew_up_place,
occupation=user_obj.occupation,
language=user_language,
)
background_voice = infer_background_voice(user_obj.occupation)
user_occupation = user_obj.occupation or ""
@@ -1303,6 +1329,7 @@ def generate_chapter_content(self, user_id: str, stage: str, new_content: str):
occupation=user_occupation,
memoir_correlation_id=cid,
llm_fast=llm_fast,
language=user_language,
)
db.flush()
if chapter is None:

View File

@@ -51,8 +51,19 @@ def generate_story_title_after_create(
ms,
)
return {"status": "skip_not_found"}
expected_ph = _placeholder_title(chapter_category)
if (st.title or "").strip() and (st.title or "").strip() != expected_ph:
user_obj_pre = db.get(User, user_id)
user_language = (
"en"
if user_obj_pre is not None
and str(getattr(user_obj_pre, "language_preference", "zh") or "zh").lower()
== "en"
else "zh"
)
expected_ph_zh = _placeholder_title(chapter_category, language="zh")
expected_ph_en = _placeholder_title(chapter_category, language="en")
expected_ph = _placeholder_title(chapter_category, language=user_language)
current = (st.title or "").strip()
if current and current not in (expected_ph_zh, expected_ph_en):
ms = (time.perf_counter() - t0) * 1000
logger.info(
"event=story_title_task_skip story_id={} reason=user_modified duration_ms={:.1f} "
@@ -73,7 +84,7 @@ def generate_story_title_after_create(
)
return {"status": "skip_no_llm"}
user_obj = db.get(User, user_id)
user_obj = user_obj_pre
user_profile = ""
birth_year = None
if user_obj:
@@ -83,6 +94,7 @@ def generate_story_title_after_create(
birth_place=user_obj.birth_place,
grew_up_place=user_obj.grew_up_place,
occupation=user_obj.occupation,
language=user_language,
)
state = get_or_create_state_sync(user_id, db)
@@ -101,8 +113,13 @@ def generate_story_title_after_create(
user_birth_year=birth_year,
llm=llm,
oral_scope=oral_scope or "",
language=user_language,
)
if not new_title.strip() or new_title.strip() == expected_ph:
if not new_title.strip() or new_title.strip() in (
expected_ph_zh,
expected_ph_en,
expected_ph,
):
ms = (time.perf_counter() - t0) * 1000
logger.info(
"event=story_title_task_skip story_id={} reason=placeholder duration_ms={:.1f} "