Coverage for web_search/provider_registry.py: 87%

1 from typing import Dict, List, Optional, Type

2 from qrclaw.web_search.types import WebSearchProvider

3 from qrclaw.web_search.providers.tavily import TavilySearchProvider

4 from qrclaw.web_search.providers.duckduckgo import DuckDuckGoSearchProvider

5 from qrclaw.web_search.providers.google import GoogleSearchProvider

6

7 _providers: Dict[str, Type[WebSearchProvider]] = {}

8

9 def register_provider(provider_cls: Type[WebSearchProvider]):

10 """注册一个新的 Search Provider"""

11 _providers[provider_cls().id] = provider_cls

12

13 def list_providers() -> List[Type[WebSearchProvider]]:

14 """列出所有已注册的 Provider"""

15 return list(_providers.values())

16

17 def get_provider(provider_id: str) -> Optional[Type[WebSearchProvider]]:

18 """根据 ID 获取 Provider"""

19 return _providers.get(provider_id)

20

21# 注册所有 Provider

22# 注意：Runtime 的自动选择逻辑会按照这里的注册顺序或者显式优先级来挑

23 register_provider(TavilySearchProvider)

24 register_provider(GoogleSearchProvider)

25 register_provider(DuckDuckGoSearchProvider)