fix(agents): use console language when creating agent and fallback unsupported langs#2498
fix(agents): use console language when creating agent and fallback unsupported langs#2498Alneys wants to merge 1 commit intoagentscope-ai:mainfrom
Conversation
|
Hi @Alneys, this is your 2nd Pull Request. 🙌 Join Developer CommunityThanks so much for your contribution! We'd love to invite you to join the official CoPaw developer group! You can find the Discord and DingTalk group links under the "Developer Community" section on our docs page: We truly appreciate your enthusiasm—and look forward to your future contributions! 😊 We'll review your PR soon. |
There was a problem hiding this comment.
Code Review
This pull request introduces language support and synchronization for agents. Key changes include updating the UI to refresh agent configurations when the selected agent changes, passing the current language during agent creation, and implementing a backend fallback mechanism to English for unsupported languages. Review feedback suggests simplifying the React hooks used for configuration fetching, utilizing the i18n library instead of direct local storage access for language detection, and streamlining Python validation logic and redundant default value assignments.
| // Refresh config when selectedAgent changes | ||
| const prevSelectedAgentRef = useRef(selectedAgent); | ||
| useEffect(() => { | ||
| // Only refresh if selectedAgent actually changed (not initial mount) | ||
| if ( | ||
| prevSelectedAgentRef.current !== selectedAgent && | ||
| prevSelectedAgentRef.current !== undefined | ||
| ) { | ||
| fetchConfig(); | ||
| } | ||
| prevSelectedAgentRef.current = selectedAgent; | ||
| }, [selectedAgent, fetchConfig]); | ||
|
|
||
| // Fetch config on mount | ||
| useEffect(() => { | ||
| fetchConfig(); | ||
| }, [fetchConfig]); |
There was a problem hiding this comment.
The logic for fetching the configuration on component mount and when the selected agent changes can be simplified. The current implementation uses two useEffect hooks and a useRef to track the previous agent, which is more complex than necessary.
A single useEffect hook with selectedAgent in its dependency array can handle both the initial fetch and subsequent updates, leading to cleaner and more maintainable code.
// Fetch config on mount and when selectedAgent changes
useEffect(() => {
fetchConfig();
}, [selectedAgent, fetchConfig]);
| : workspaceRaw; | ||
| const payload = { ...values, workspace_dir }; | ||
|
|
||
| const currentLanguage = localStorage.getItem("language") || "en"; |
There was a problem hiding this comment.
Instead of directly accessing localStorage, it's better practice to use the i18next instance to get the current language. This decouples your component from the storage implementation details of the i18n library and makes the code more robust.
You can get the i18n object from the useTranslation hook and then derive the language from it. Remember to handle cases where the language code includes a region (e.g., en-US).
Example:
// At the top of your component
const { t, i18n } = useTranslation();
// In your handleSubmit function
const lang = i18n.language || 'en';
const currentLanguage = lang.split('-')[0];| def validate_language(cls, value: str | None) -> str: | ||
| """Validate language and fallback to default if not supported. | ||
|
|
||
| Agent supports: en, zh, ru (not ja) | ||
| If an unsupported language is provided (e.g., ja from UI), | ||
| fallback to English (en). | ||
| """ | ||
| if value is None: | ||
| return DEFAULT_AGENT_LANGUAGE | ||
| if isinstance(value, str): | ||
| language = value.strip().lower() | ||
| if language in VALID_AGENT_LANGUAGES: | ||
| return language | ||
| # Fallback to English for unsupported languages (e.g., ja) | ||
| return DEFAULT_AGENT_LANGUAGE | ||
| return DEFAULT_AGENT_LANGUAGE |
There was a problem hiding this comment.
The validate_language method can be simplified to be more concise and readable. The current implementation has multiple return paths that can be consolidated.
def validate_language(cls, value: str | None) -> str:
"""Validate language and fallback to default if not supported.
Agent supports: en, zh, ru (not ja)
If an unsupported language is provided (e.g., ja from UI),
fallback to English (en).
"""
if isinstance(value, str):
language = value.strip().lower()
if language in VALID_AGENT_LANGUAGES:
return language
return DEFAULT_AGENT_LANGUAGE| config = load_global_config() | ||
| language = config.agents.language or "zh" | ||
| # Use language from agent config | ||
| language = agent_config.language or "en" |
There was a problem hiding this comment.
The agent_config.language field is a non-optional string with a default value in the AgentProfileConfig model. Therefore, it will never be a falsy value, and the or "en" fallback is unreachable. This can be simplified to directly use the value.
| language = agent_config.language or "en" | |
| language = agent_config.language |

Description
Newly created agents always choose
English (en)as agent language and always copyChinese (zh)persona MD files regardless of the user's UI language, this PR fixes the issue by reading language from localStorage during creation and adding server-side validation with automatic English fallback:Fix & Changes:
Console Frontend - Agent Management (
console/src/pages/Settings/Agents/index.tsx):localStoragewhen creating a new agentlanguageparameter to thecreateAgentAPI callConsole Frontend - Configuration (
console/src/pages/Agent/Config/useAgentConfig.tsx):selectedAgentchangesBackend (
src/copaw/app/routers/agent.py,src/copaw/app/routers/agents.py):en) for unsupported languagesSupported languages now:
en,zh,ruFallback behavior: Unsupported languages (e.g.,
ja,fr) automatically fall back to EnglishWhy: Ensures new agents inherit the user's console language preference while preventing invalid language configurations that could cause Agent behavior issues.
Type of Change
Component(s) Affected
Checklist
pre-commit run --all-fileslocally and it passespytestor as relevant) and they passTesting
Test Console Language Inheritance:
Test Unsupported Language Fallback:
ja)en)Test Agent Config Refresh:
Verify Existing Agents Unaffected: