Skip to content

feat: add provider adapter i18n registration support#6917

Open
Li-shi-ling wants to merge 4 commits intoAstrBotDevs:masterfrom
Li-shi-ling:feature/i18n-for-providers
Open

feat: add provider adapter i18n registration support#6917
Li-shi-ling wants to merge 4 commits intoAstrBotDevs:masterfrom
Li-shi-ling:feature/i18n-for-providers

Conversation

@Li-shi-ling
Copy link
Contributor

@Li-shi-ling Li-shi-ling commented Mar 25, 2026

此项更改为 Provider 适配器增加了动态 i18n 注册支持,与现有的平台适配器机制保持一致。
在此更改之前,register_provider_adapter 无法声明 i18n_resources 或 config_metadata,因此 Provider 适配器缺少完整的后端到前端 i18n 注册路径。这导致自定义 Provider 配置字段无法参与到平台适配器所使用的同一套已翻译的 WebUI 表单流程中。

Modifications / 改动点

astrbot/core/provider/entities.py
在 ProviderMetaData 中添加了 i18n_resources 和 config_metadata,使 Provider 适配器注册能够通过后端注册层携带自定义的 i18n 负载和配置元数据。

astrbot/core/provider/register.py
扩展了 register_provider_adapter(...) 以接受 i18n_resources 和 config_metadata,并将其持久化到 ProviderMetaData 中。这保持了旧注册的兼容性,同时启用了 Provider 端的 i18n 注册功能。

astrbot/dashboard/routes/config.py
添加了 Provider 元数据注入和动态 i18n 响应支持。
为 description / hint / labels / name 实现了递归的元数据 i18n 键重写,包括嵌套的 items 和 template_schema。
添加了使用 provider_group.provider.{provider.type}.* 的 Provider 端键路径生成。
扩展了 /api/config/get 和 /api/config/provider/template 两个接口,使其返回 provider_i18n_translations。
将 Provider 适配器的 config_metadata 注入到 Provider 架构生成中。

dashboard/src/composables/useProviderSources.ts
通过 mergeDynamicTranslations('features.config-metadata', ...) 添加了动态 Provider i18n 合并逻辑。
将 Provider 翻译加载路径集中到共享的组合式函数中,使 Provider 页面和 Provider 配置对话框都能受益于相同的动态 i18n 行为。
添加了语言切换重新加载处理,以便在切换语言时刷新 Provider 适配器的翻译。

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

image image image image

测试插件代码(节选)

from astrbot.api.provider import register_provider_adapter
from astrbot.api.provider import ProviderType
from astrbot.api.provider import EmbeddingProvider

DEFAULT_MODEL_NAME = "paraphrase-multilingual-MiniLM-L12-v2"
STEMBEDDING_PROVIDER_TEMPLATE = {
    "id": "STEmbedding",
    "type": "STEmbedding",
    "provider": "Local",
    "STEmbedding_path": DEFAULT_MODEL_NAME,
    "STEmbedding_torch_load_weights_only": "auto",
    "provider_type": "embedding",
    "enable": True,
    "embedding_dimensions": 384,
}
STEMBEDDING_PROVIDER_CONFIG_METADATA = {
    "STEmbedding_path": {
        "type": "string",
        "description": "",
        "hint": "",
    },
    "STEmbedding_torch_load_weights_only": {
        "type": "string",
        "description": "",
        "hint": "",
    },
}
STEMBEDDING_PROVIDER_I18N_RESOURCES = {
    "zh-CN": {
        "name": "STEmbedding",
        "STEmbedding_path": {
            "description": "Sentence Transformers 模型路径",
            "hint": "支持相对路径和绝对路径。相对路径会基于 AstrBot 的数据目录解析。",
        },
        "STEmbedding_torch_load_weights_only": {
            "description": "PyTorch torch.load 的 weights_only 行为",
            "hint": "可选 auto、true、false。PyTorch 2.6+ 默认更严格,旧模型遇到加载失败时可尝试 false,但只建议用于可信模型。",
        },
    },
    "en-US": {
        "name": "STEmbedding",
        "STEmbedding_path": {
            "description": "Sentence Transformers model path",
            "hint": "Supports both relative and absolute paths. Relative paths are resolved from the AstrBot data directory.",
        },
        "STEmbedding_torch_load_weights_only": {
            "description": "PyTorch torch.load weights_only behavior",
            "hint": "Available values: auto, true, false. PyTorch 2.6+ is stricter by default. If an older model fails to load, you can try false, but only for trusted models.",
        },
    },
    "ru-RU": {
        "name": "STEmbedding",
        "STEmbedding_path": {
            "description": "Путь к модели Sentence Transformers",
            "hint": "Поддерживаются относительные и абсолютные пути. Относительные пути вычисляются относительно каталога данных AstrBot.",
        },
        "STEmbedding_torch_load_weights_only": {
            "description": "Поведение параметра weights_only в PyTorch torch.load",
            "hint": "Допустимые значения: auto, true, false. В PyTorch 2.6+ загрузка по умолчанию стала строже. Если старая модель не загружается, можно попробовать false, но только для доверенных моделей.",
        },
    },
}

def register_STEmbeddingProvider():
    try:
        register_provider_adapter(
            "STEmbedding",
            "Sentence Transformers Embedding Provider",
            provider_type=ProviderType.EMBEDDING,
            provider_display_name="STEmbedding",
            default_config_tmpl=STEMBEDDING_PROVIDER_TEMPLATE.copy(),
            config_metadata={
                key: value.copy()
                for key, value in STEMBEDDING_PROVIDER_CONFIG_METADATA.items()
            },
            i18n_resources=STEMBEDDING_PROVIDER_I18N_RESOURCES,
        )(STEmbeddingProvider)
        logger.info("[STEmbedding] Provider 已注册")
    except ValueError:
        logger.info("[STEmbedding] Provider 已存在,跳过注册")

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • [ x ] 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

有进行测试

  • [ x ] 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

没有引入新库

  • [ x ] 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

没有引入

Summary by Sourcery

Add dynamic i18n registration and metadata support for provider adapters and expose provider adapter types and registration utilities through the public API.

New Features:

  • Allow provider adapters to register i18n resources and configuration metadata via the ProviderMetaData model and register_provider_adapter decorator.
  • Return provider-specific i18n translations alongside configuration schemas from backend config endpoints for use in the Web UI.
  • Enable the frontend provider configuration UI to merge and reload dynamic provider i18n translations when templates are fetched or the locale changes.
  • Expose additional provider adapter base classes and the provider adapter registration helper through the astrbot.api.provider package.

Enhancements:

  • Refactor platform metadata i18n key rewriting into a reusable helper and reuse it for provider metadata to support nested schemas.

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Mar 25, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在为 AstrBot 的 Provider 适配器引入全面的国际化 (i18n) 注册支持。通过允许 Provider 适配器声明其自身的 i18n 资源和配置元数据,它解决了自定义 Provider 配置字段无法参与到 WebUI 翻译流程的问题。这一改进确保了所有 Provider 配置都能在用户界面中正确地进行本地化显示,从而提升了用户体验并保持了与平台适配器机制的一致性。

Highlights

  • Provider 适配器 i18n 注册支持: 为 Provider 适配器增加了动态 i18n 注册支持,使其与现有平台适配器机制保持一致,解决了自定义 Provider 配置字段无法参与 WebUI 翻译流程的问题。
  • ProviderMetaData 扩展: 在 ProviderMetaData 中新增了 i18n_resourcesconfig_metadata 字段,以支持 Provider 适配器携带自定义的 i18n 负载和配置元数据。
  • 注册函数更新: 扩展了 register_provider_adapter 函数,使其能够接受并持久化 i18n_resourcesconfig_metadata,同时保持了旧注册的兼容性。
  • 后端 API 增强: 更新了后端 API (/api/config/get/api/config/provider/template),以注入 Provider 元数据并返回动态 i18n 翻译,包括递归的元数据 i18n 键重写。
  • 前端国际化处理: 前端 (useProviderSources.ts) 增加了动态 Provider i18n 合并逻辑,并实现了语言切换时刷新 Provider 适配器翻译的功能,确保了 WebUI 的本地化显示。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • The new _rewrite_metadata_i18n_keys now also rewrites name fields (and is reused for platforms and providers), which is a behavior change for platform metadata; consider confirming this won’t break existing consumers that expect literal name values or gating name rewriting behind an option.
  • The docstring for register_provider_adapter in astrbot/core/provider/register.py still says it is for registering platform adapters (用于注册平台适配器); updating this to refer to provider adapters would avoid confusion.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `_rewrite_metadata_i18n_keys` now also rewrites `name` fields (and is reused for platforms and providers), which is a behavior change for platform metadata; consider confirming this won’t break existing consumers that expect literal `name` values or gating `name` rewriting behind an option.
- The docstring for `register_provider_adapter` in `astrbot/core/provider/register.py` still says it is for registering platform adapters (用于注册平台适配器); updating this to refer to provider adapters would avoid confusion.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dosubot dosubot bot added area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. area:webui The bug / feature is about webui(dashboard) of astrbot. labels Mar 25, 2026
Copy link
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 new provider types (Embedding, Rerank, TTS) and significantly enhances the dashboard's configuration and internationalization (i18n) capabilities for providers. It allows providers to define their own i18n resources and config metadata, which are then dynamically loaded and applied in the UI. A high-severity issue was identified where provider.default_config_tmpl was directly assigned, potentially leading to unintended side effects due to mutable shared global state. This should be corrected by using copy.deepcopy() to ensure isolated copies of the template.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f8771b5038

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. area:webui The bug / feature is about webui(dashboard) of astrbot. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant