"""API: Save model routing configuration.""" from helpers.api import ApiHandler, Request, Response import yaml import os class ModelRoutingSave(ApiHandler): async def process(self, input: dict, request: Request) -> dict | Response: role = input.get("role", "") preset = input.get("preset", "") project_name = input.get("project_name", "") agent_profile = input.get("agent_profile", "") if not role or not preset: return {"ok": False, "error": "role and preset are required"} # Update default_config.yaml or per-project config plugin_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) config_path = os.path.join(plugin_dir, "default_config.yaml") try: with open(config_path, "r") as f: config = yaml.safe_load(f) or {} except Exception: config = {} routing = config.get("model_routing", {}) roles = routing.get("roles", {}) if role not in roles: roles[role] = {} roles[role]["preset"] = preset routing["roles"] = roles config["model_routing"] = routing try: with open(config_path, "w") as f: yaml.dump(config, f, default_flow_style=False, allow_unicode=True) return {"ok": True, "role": role, "preset": preset} except Exception as e: return {"ok": False, "error": str(e)}