fix(ops): English replies from English prompts and ROLE_SYSTEM.en

Detect language from user message before UI hint, load ops ROLE_SYSTEM.en.md when en, and wire user_text through WS/HTTP/worker paths.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-05-19 22:41:46 +08:00
parent f1626195f7
commit 0de5f7bc98
9 changed files with 151 additions and 19 deletions

View file

@ -9,7 +9,7 @@ from svc.config.paths import PROJECT_ROOT
_ROLE_CONTEXT_CACHE_LOCK = threading.Lock()
_ROLE_CONTEXT_CACHE: dict[tuple[str, tuple[Any, ...], tuple[tuple[str, str], ...]], str] = {}
_ROLE_DOCS: tuple[str, ...] = ("SOUL.md", "ROLE_SYSTEM.md")
_ROLE_DOCS: tuple[str, ...] = ("SOUL.md", "SOUL.en.md", "ROLE_SYSTEM.md", "ROLE_SYSTEM.en.md")
_TEMPLATE_VAR_RE = re.compile(r"\{\{\s*([A-Za-z0-9_]+)\s*\}\}")
@ -75,12 +75,29 @@ def _render_template_vars(text: str, vars_tuple: tuple[tuple[str, str], ...]) ->
return _TEMPLATE_VAR_RE.sub(_replace, text)
def build_role_system_context(role: str, template_vars: dict[str, Any] | None = None) -> str:
def _read_localized_doc(root: Path, base: str, *, lang: str) -> str:
"""Prefer ``ROLE_SYSTEM.en.md`` when lang is en; fall back to ``ROLE_SYSTEM.md``."""
lang_en = str(lang or "").strip().lower().startswith("en")
if lang_en and base.endswith(".md"):
localized = base[:-3] + ".en.md"
text = _read_text(root / localized)
if text:
return text
return _read_text(root / base)
def build_role_system_context(
role: str,
template_vars: dict[str, Any] | None = None,
*,
lang: str = "zh",
) -> str:
"""Build role context from runtime/workspaces/<role>."""
role_id = _workspace_for_role(role)
lang_key = "en" if str(lang or "").strip().lower().startswith("en") else "zh"
sig = _role_workspace_signature(role_id)
vars_tuple = _normalize_template_vars(template_vars)
cache_key = (role_id, sig, vars_tuple)
cache_key = (role_id, lang_key, sig, vars_tuple)
with _ROLE_CONTEXT_CACHE_LOCK:
cached = _ROLE_CONTEXT_CACHE.get(cache_key)
if isinstance(cached, str) and cached.strip():
@ -94,18 +111,25 @@ def build_role_system_context(role: str, template_vars: dict[str, Any] | None =
for name in ("SOUL.md",):
t = ""
for root in roots:
t = _render_template_vars(_read_text(root / name), vars_tuple)
t = _read_localized_doc(root, name, lang=lang_key)
if t:
t = _render_template_vars(t, vars_tuple)
if t:
break
if t:
parts.append(f"# SOUL\n{t}")
role_system = ""
for root in roots:
role_system = _render_template_vars(_read_text(root / "ROLE_SYSTEM.md"), vars_tuple)
role_system = _read_localized_doc(root, "ROLE_SYSTEM.md", lang=lang_key)
if role_system:
role_system = _render_template_vars(role_system, vars_tuple)
break
if not role_system:
role_system = "你是专业助手。先给可验证结论,再给依据与下一步。"
role_system = (
"You are a professional assistant. Lead with verifiable conclusions, then evidence and next steps."
if lang_key == "en"
else "你是专业助手。先给可验证结论,再给依据与下一步。"
)
parts.append(f"# ROLE_SYSTEM\n{role_system}")
out = "\n\n".join([p for p in parts if p.strip()]).strip()
with _ROLE_CONTEXT_CACHE_LOCK: