|
| 1 | +from openai._exceptions import NotFoundError |
| 2 | + |
| 3 | +from ..register import register_provider_adapter |
| 4 | +from .openai_source import ProviderOpenAIOfficial |
| 5 | +from .request_retry import retry_provider_request |
| 6 | + |
| 7 | + |
| 8 | +@register_provider_adapter( |
| 9 | + "ssycloud_chat_completion", |
| 10 | + "SSYCloud Chat Completion Provider Adapter", |
| 11 | +) |
| 12 | +class ProviderSSYCloud(ProviderOpenAIOfficial): |
| 13 | + """SSYCloud provider using its OpenAI-compatible Chat Completions API.""" |
| 14 | + |
| 15 | + def __init__(self, provider_config: dict, provider_settings: dict) -> None: |
| 16 | + """Initialize the SSYCloud client with provider defaults. |
| 17 | +
|
| 18 | + Args: |
| 19 | + provider_config: AstrBot provider source configuration. |
| 20 | + provider_settings: Global provider settings. |
| 21 | + """ |
| 22 | + if not provider_config.get("api_base"): |
| 23 | + provider_config["api_base"] = "https://router.shengsuanyun.com/api/v1" |
| 24 | + custom_headers = provider_config.get("custom_headers") |
| 25 | + if not isinstance(custom_headers, dict): |
| 26 | + custom_headers = {} |
| 27 | + provider_config["custom_headers"] = custom_headers |
| 28 | + custom_headers.setdefault("X-Title", "AstrBot") |
| 29 | + super().__init__(provider_config, provider_settings) |
| 30 | + |
| 31 | + async def get_models(self) -> list[str]: |
| 32 | + """Return models compatible with the Chat Completions API. |
| 33 | +
|
| 34 | + Returns: |
| 35 | + Sorted model IDs. Models without ``support_apis`` metadata are kept |
| 36 | + for compatibility with older SSYCloud responses. |
| 37 | +
|
| 38 | + Raises: |
| 39 | + Exception: If the SSYCloud model catalog endpoint is unavailable. |
| 40 | + """ |
| 41 | + try: |
| 42 | + response = await retry_provider_request( |
| 43 | + "SSYCloud", |
| 44 | + lambda: self.client.models.list(), |
| 45 | + ) |
| 46 | + model_ids: list[str] = [] |
| 47 | + for model in response.data: |
| 48 | + support_apis = getattr(model, "support_apis", None) |
| 49 | + if support_apis is None: |
| 50 | + model_extra = getattr(model, "model_extra", None) |
| 51 | + if isinstance(model_extra, dict): |
| 52 | + support_apis = model_extra.get("support_apis") |
| 53 | + if not isinstance(support_apis, list) or ( |
| 54 | + "/v1/chat/completions" in support_apis |
| 55 | + ): |
| 56 | + model_ids.append(model.id) |
| 57 | + return sorted(model_ids) |
| 58 | + except NotFoundError as exc: |
| 59 | + raise Exception(f"Failed to fetch SSYCloud model list: {exc}") from exc |
0 commit comments