- 添加 prompt_locale 参数支持简体中文、繁体中文和英文提示词本地化 - 移除内置 agents 配置以简化系统架构 - 更新 ContextBuilder 使用动态提示词模板而非硬编码内容 - 在 AgentLoop、Web 接口和 AgentService 中传递 locale 参数 - 添加输出语言指令确保用户界面内容按指定语言生成 - 扩展前端 LanguageSwitcher 组件支持三种语言选项 - 优化 Header 和侧边栏组件的响应式布局和文本截断处理 - 更新测试用例验证不同语言环境下的提示词正确性
56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
"""Locale-aware main agent prompt loading."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
|
|
DEFAULT_MAIN_AGENT_PROMPT_LOCALE = "zh-Hans"
|
|
|
|
_PROMPT_FILES = {
|
|
"zh-Hans": "zh-Hans.md",
|
|
"zh-Hant": "zh-Hant.md",
|
|
"en": "en.md",
|
|
}
|
|
|
|
_LOCALE_ALIASES = {
|
|
"zh": "zh-Hans",
|
|
"zh-cn": "zh-Hans",
|
|
"zh-hans": "zh-Hans",
|
|
"zh-sg": "zh-Hans",
|
|
"zh-hant": "zh-Hant",
|
|
"zh-tw": "zh-Hant",
|
|
"zh-hk": "zh-Hant",
|
|
"zh-mo": "zh-Hant",
|
|
"en": "en",
|
|
"en-us": "en",
|
|
"en-gb": "en",
|
|
}
|
|
|
|
|
|
def get_main_agent_prompt(locale: str | None = None) -> str:
|
|
"""Return the main-agent identity prompt for a prompt locale."""
|
|
|
|
prompt_locale = normalize_main_agent_prompt_locale(locale)
|
|
return _load_main_agent_prompt(prompt_locale)
|
|
|
|
|
|
def normalize_main_agent_prompt_locale(locale: str | None = None) -> str:
|
|
cleaned = (locale or DEFAULT_MAIN_AGENT_PROMPT_LOCALE).strip()
|
|
if not cleaned:
|
|
return DEFAULT_MAIN_AGENT_PROMPT_LOCALE
|
|
normalized = _LOCALE_ALIASES.get(cleaned.lower())
|
|
if normalized:
|
|
return normalized
|
|
return cleaned if cleaned in _PROMPT_FILES else DEFAULT_MAIN_AGENT_PROMPT_LOCALE
|
|
|
|
|
|
@lru_cache(maxsize=len(_PROMPT_FILES))
|
|
def _load_main_agent_prompt(locale: str) -> str:
|
|
filename = _PROMPT_FILES.get(locale, _PROMPT_FILES[DEFAULT_MAIN_AGENT_PROMPT_LOCALE])
|
|
path = Path(__file__).with_name("main_agent") / filename
|
|
if not path.exists():
|
|
fallback_path = Path(__file__).with_name("main_agent") / _PROMPT_FILES[DEFAULT_MAIN_AGENT_PROMPT_LOCALE]
|
|
return fallback_path.read_text(encoding="utf-8").strip()
|
|
return path.read_text(encoding="utf-8").strip()
|