Agent Zero AI framework
feat(providers): Allow plugins to add and override model providers
linuztx342a9584cc0b
diff --git a/helpers/plugins.py b/helpers/plugins.pyindex fe67e6fe6..2daf21682 100644--- a/helpers/plugins.py+++ b/helpers/plugins.py@@ -79,6 +79,8 @@ class PluginListItem(BaseModel):def after_plugin_change(plugin_names: list[str] | None = None):clear_plugin_cache()+ from helpers.providers import reload_providers+ reload_providers()send_frontend_reload_notification(plugin_names)diff --git a/helpers/providers.py b/helpers/providers.pyindex ab74d6d9b..1aae73eb2 100644--- a/helpers/providers.py+++ b/helpers/providers.py@@ -20,39 +20,65 @@ class ProviderManager:cls._instance = cls()return cls._instance+ @classmethod+ def reload(cls):+ """Force reload of all provider configs (call after plugin changes)."""+ inst = cls.get_instance()+ inst._load_providers()+def __init__(self):if self._raw is None or self._options is None:self._load_providers()- def _load_providers(self):- """Loads provider configurations from the YAML file and normalises them."""+ @staticmethod+ def _load_yaml(path: str) -> dict:try:- config_path = files.get_abs_path("conf/model_providers.yaml")- with open(config_path, "r", encoding="utf-8") as f:- raw_yaml = yaml.safe_load(f) or {}+ with open(path, "r", encoding="utf-8") as f:+ return yaml.safe_load(f) or {}except (FileNotFoundError, yaml.YAMLError):- raw_yaml = {}-- # ------------------------------------------------------------- # Normalise the YAML so that internally we always work with a- # list-of-dicts [{id, name, ...}] for each provider type. This- # keeps existing callers unchanged while allowing the new nested- # mapping format in the YAML (id -> { ... }).- # ------------------------------------------------------------- normalised: Dict[str, List[Dict[str, str]]] = {}+ return {}+ @staticmethod+ def _normalise_yaml(raw_yaml: dict) -> Dict[str, Dict[str, Dict[str, str]]]:+ """Normalise YAML into {type: {id: config}} mapping format."""+ result: Dict[str, Dict[str, Dict[str, str]]] = {}for p_type, providers in (raw_yaml or {}).items():- items: List[Dict[str, str]] = []-+ entries: Dict[str, Dict[str, str]] = {}if isinstance(providers, dict):- # New format: mapping of id -> configfor pid, cfg in providers.items():- entry = {"id": pid, **(cfg or {})}- items.append(entry)+ entries[pid] = cfg or {}elif isinstance(providers, list):- # Legacy list format – use as-is- items.extend(providers or [])+ for p in (providers or []):+ pid = (p.get("id") or p.get("value") or "").lower()+ if pid:+ entries[pid] = {k: v for k, v in p.items() if k not in ("id", "value")}+ result[p_type] = entries+ return result+ def _load_providers(self):+ """Loads provider configs from main YAML and enabled plugins, then merges."""+ # Load base config+ base_path = files.get_abs_path("conf/model_providers.yaml")+ merged = self._normalise_yaml(self._load_yaml(base_path))++ # Merge plugin provider configs (enabled plugins only)+ from helpers.plugins import get_enabled_plugin_paths+ plugin_yamls = get_enabled_plugin_paths(None, "conf", "model_providers.yaml")+ for plugin_yaml_path in plugin_yamls:+ plugin_data = self._normalise_yaml(self._load_yaml(plugin_yaml_path))+ for p_type, providers in plugin_data.items():+ if p_type not in merged:+ merged[p_type] = {}+ # Overwrite matching keys, append new ones+ merged[p_type].update(providers)++ # Convert merged {type: {id: config}} to normalised list format+ normalised: Dict[str, List[Dict[str, str]]] = {}+ for p_type, providers in merged.items():+ items: List[Dict[str, str]] = []+ for pid, cfg in providers.items():+ entry = {"id": pid, **cfg}+ items.append(entry)normalised[p_type] = items# Save raw@@ -98,4 +124,9 @@ def get_raw_providers(provider_type: ModelType) -> List[Dict[str, str]]:def get_provider_config(provider_type: ModelType, provider_id: str) -> Optional[Dict[str, str]]:"""Return metadata for a single provider (None if not found)."""- return ProviderManager.get_instance().get_provider_config(provider_type, provider_id)\ No newline at end of file+ return ProviderManager.get_instance().get_provider_config(provider_type, provider_id)+++def reload_providers():+ """Re-merge base + plugin provider configs. Call after plugin changes."""+ ProviderManager.reload()\ No newline at end of file