Skip to content

新增 Metaso(秘塔搜索)作为 Web 搜索后端#8241

Open
mrluanma wants to merge 1 commit into
AstrBotDevs:masterfrom
meta-sota:feat/add-metaso-web-search
Open

新增 Metaso(秘塔搜索)作为 Web 搜索后端#8241
mrluanma wants to merge 1 commit into
AstrBotDevs:masterfrom
meta-sota:feat/add-metaso-web-search

Conversation

@mrluanma
Copy link
Copy Markdown

@mrluanma mrluanma commented May 19, 2026

动机

新增 Metaso(秘塔搜索)作为 Web 搜索后端。Metaso 提供开箱即用的免费 API(每日 100 次查询),用户无需额外配置即可使用网页搜索功能。同时支持通过 websearch_metaso_key 配置自定义 API Key 以获取更高配额,并支持多 Key 轮询。

改动点

  • astrbot/core/tools/web_search_tools.py — 新增 _metaso_search() 异步函数,调用 https://metaso.cn/api/v1/search;新增 MetasoWebSearchTool 工具类,支持 size 参数(范围 1–100);内置免费 API Key _METASO_DEFAULT_API_KEY;新增 _METASO_KEY_ROTATOR 支持多 Key 轮询;映射 HTTP 401/403/429 和 API 错误码 3003(每日限额)/2005(Key 无效)为描述性异常;在 WEB_SEARCH_TOOL_NAMES 中注册 "web_search_metaso",并更新 __all__ 导出。
  • astrbot/core/astr_main_agent.py — 导入 MetasoWebSearchTool 并在搜索提供者分发逻辑中注册(provider == "metaso")。
  • astrbot/core/config/default.py — 在提供者下拉列表中新增 "metaso" 选项;新增 websearch_metaso_key 默认配置([])及条件 schema 定义。
  • dashboard/src/i18n/locales/{en-US,ru-RU,zh-CN}/features/config-metadata.json — 三语言均新增 websearch_metaso_key 的 i18n 条目(描述 + 提示语)。
  • tests/unit/test_web_search_tools.py — 新增 16 个单元测试,覆盖:结果映射、默认 Key 回退、自定义 Key 使用、默认载荷、size 边界值(过低/过高)、空结果、HTTP 401/403/429 错误、API 错误码 3003/2005/通用非零码、空 webpages 响应。

运行截图或测试结果

$ python -m pytest tests/unit/test_web_search_tools.py -v
======================== 26 passed in 0.84s =========================

全部 26 个测试通过(10 个 Firecrawl + 16 个 Metaso)。

检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
  • 👀 我的更改经过了良好的测试,并已在上方提供了"验证步骤"和"运行截图"
  • 🤓 我确保没有引入新依赖库。
  • 😮 我的更改没有引入恶意代码。
  • 这不是一个破坏性变更。

Summary by Sourcery

Add Metaso as a new configurable web search backend and integrate it into the agent pipeline and configuration system.

New Features:

  • Introduce Metaso-based web search tool with configurable result size and support for default and user-provided API keys.
  • Expose Metaso as a selectable web search provider in core configuration and agent web search tool dispatch.

Enhancements:

  • Normalize legacy web search configuration to handle Metaso API key lists alongside existing providers.
  • Simplify shell command quoting in the shipyard search utility using shlex.join.
  • Tighten the pipeline stage process method type hint for its async generator return type.

Documentation:

  • Add i18n configuration metadata entries for Metaso API key settings in the dashboard across supported locales.

Tests:

  • Add unit tests covering Metaso search result mapping, payload defaults and size bounds, API key selection and rotation, empty results handling, HTTP error statuses, API error codes, and empty response bodies.

@auto-assign auto-assign Bot requested review from LIghtJUNction and Raven95676 May 19, 2026 12:36
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels May 19, 2026
Copy link
Copy Markdown
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 found 1 issue, and left some high level feedback:

  • In _metaso_search all error paths raise a bare Exception; consider using a more specific exception type (or a shared custom web-search error) so callers can distinguish Metaso failures from other unexpected errors more reliably.
  • The type annotation for Stage.process was changed from AsyncGenerator[None, None] to AsyncGenerator[None], which is not the canonical two-parameter form of AsyncGenerator; it would be clearer and more accurate to keep the full generic parameters or use a higher-level AsyncIterator / AsyncIterable if the send type is not needed.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `_metaso_search` all error paths raise a bare `Exception`; consider using a more specific exception type (or a shared custom web-search error) so callers can distinguish Metaso failures from other unexpected errors more reliably.
- The type annotation for `Stage.process` was changed from `AsyncGenerator[None, None]` to `AsyncGenerator[None]`, which is not the canonical two-parameter form of `AsyncGenerator`; it would be clearer and more accurate to keep the full generic parameters or use a higher-level `AsyncIterator` / `AsyncIterable` if the send type is not needed.

## Individual Comments

### Comment 1
<location path="astrbot/core/pipeline/stage.py" line_range="36" />
<code_context>
         self,
         event: AstrMessageEvent,
-    ) -> None | AsyncGenerator[None, None]:
+    ) -> None | AsyncGenerator[None]:
         """处理事件

</code_context>
<issue_to_address>
**issue (bug_risk):** Using `AsyncGenerator` with a single type argument is invalid for current Python typing and will raise at runtime.

This should remain `AsyncGenerator[None, None]` (or be wrapped in a helper alias) to match the current `AsyncGenerator[T_co, T_contra]` signature in Python ≤3.11. Changing it to a single-argument form assumes future typing changes and will break on currently supported versions with a `TypeError` at import time.
</issue_to_address>

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.

Comment thread astrbot/core/pipeline/stage.py Outdated
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 the Metaso Web Search tool, integrating it into the main agent, configuration defaults, and dashboard localized metadata. The implementation includes the MetasoWebSearchTool class, API interaction logic with support for key rotation, and a suite of unit tests. Additionally, the PR includes minor refactors such as adopting shlex.join for command construction and simplifying AsyncGenerator type hints. Review feedback recommends simplifying conditional checks for HTTP status codes using the in operator.

Comment thread astrbot/core/tools/web_search_tools.py Outdated
Add MetasoWebSearchTool as a new web search backend using the Metaso
Search API (https://metaso.cn/api/v1/search). Includes a built-in
free-tier key (100 queries/day) and supports custom API key rotation
via websearch_metaso_key config.

- Add Metaso provider config and i18n metadata (en/ru/zh)
- Register MetasoWebSearchTool in astr_main_agent
- Map Metaso API error codes (401/403/429, code 3003/2005) to
  descriptive exceptions
- Add 17 unit tests covering HTTP errors, API codes, key selection,
  size clamping, and empty results
@mrluanma mrluanma force-pushed the feat/add-metaso-web-search branch from 4d0f7b5 to 91d68a3 Compare May 19, 2026 13:04
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. 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