from __future__ import annotations

from niannian.llm.types import ProviderSpec


def parse_provider_model(value: str | None) -> ProviderSpec | None:
    """解析 `provider:model` 配置值。"""
    if value is None or value == "null":
        return None
    text = str(value).strip()
    if not text:
        return None
    if ":" not in text:
        raise ValueError(f"无效的 provider:model 格式: {text!r}")
    provider, model = text.split(":", 1)
    provider = provider.strip()
    model = model.strip()
    if not provider or not model:
        raise ValueError(f"无效的 provider:model 格式: {text!r}")
    return ProviderSpec(provider=provider, model=model, slot="")
