Skip to content

fix(agents): use console language when creating agent and fallback unsupported langs#2498

Open
Alneys wants to merge 1 commit intoagentscope-ai:mainfrom
Alneys:dev
Open

fix(agents): use console language when creating agent and fallback unsupported langs#2498
Alneys wants to merge 1 commit intoagentscope-ai:mainfrom
Alneys:dev

Conversation

@Alneys
Copy link
Copy Markdown

@Alneys Alneys commented Mar 29, 2026

Description

Newly created agents always choose English (en) as agent language and always copy Chinese (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:

  1. Console Frontend - Agent Management (console/src/pages/Settings/Agents/index.tsx):

    • Read language from localStorage when creating a new agent
    • Pass language parameter to the createAgent API call
  2. Console Frontend - Configuration (console/src/pages/Agent/Config/useAgentConfig.tsx):

    • Add logic to refresh agent config when selectedAgent changes
    • Fix config not updating when switching between agents
  3. Backend (src/copaw/app/routers/agent.py, src/copaw/app/routers/agents.py):

    • Validate language parameter during agent creation
    • Auto-fallback to English (en) for unsupported languages

Supported languages now: en, zh, ru
Fallback behavior: Unsupported languages (e.g., ja, fr) automatically fall back to English

Why: Ensures new agents inherit the user's console language preference while preventing invalid language configurations that could cause Agent behavior issues.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Refactoring

Component(s) Affected

  • Core / Backend (app, agents, config, providers, utils, local_models)
  • Console (frontend web UI)
  • Channels (DingTalk, Feishu, QQ, Discord, iMessage, etc.)
  • Skills
  • CLI
  • Documentation (website)
  • Tests
  • CI/CD
  • Scripts / Deploy

Checklist

  • I ran pre-commit run --all-files locally and it passes
  • If pre-commit auto-fixed files, I committed those changes and reran checks
  • I ran tests locally (pytest or as relevant) and they pass
  • Documentation updated (if needed)
  • Ready for review

Testing

  1. Test Console Language Inheritance:

    • Open Console in different languages (Settings → Language)
    • Create a new agent via Settings → Agents → Create
    • Verify the agent's language matches the console language
  2. Test Unsupported Language Fallback:

    • Use a language that has no persona files, i.e. unsupported (e.g., ja)
    • Create a new agent
    • Verify the agent falls back to English (en)
  3. Test Agent Config Refresh:

    • Switch between different agents in the Console - Config
    • Verify the page refreshes correctly for each agent
  4. Verify Existing Agents Unaffected:

    • Check existing agents retain their original language settings
    • No regression in agent functionality

@github-actions
Copy link
Copy Markdown

Welcome to CoPaw! 🐾

Hi @Alneys, this is your 2nd Pull Request.

🙌 Join Developer Community

Thanks 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:
https://copaw.agentscope.io/docs/community

We truly appreciate your enthusiasm—and look forward to your future contributions! 😊

We'll review your PR soon.


Tip

⭐ If you find CoPaw useful, please give us a Star!

Star CoPaw

Staying ahead

Star CoPaw on GitHub and be instantly notified of new releases.

Your star helps more developers discover this project! 🐾

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +41 to 57
// 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]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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];

Comment on lines +75 to +90
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
language = agent_config.language or "en"
language = agent_config.language

@Alneys Alneys temporarily deployed to maintainer-approved March 29, 2026 15:32 — with GitHub Actions Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant