| 1 | from helpers.api import ApiHandler, Request, Response |
| 2 | from helpers.providers import get_raw_providers |
| 3 | from plugins._model_config.helpers import model_config |
| 4 | from agent import AgentContext |
| 5 | import models |
| 6 | |
| 7 | |
| 8 | class ModelConfigGet(ApiHandler): |
| 9 | async def process(self, input: dict, request: Request) -> dict | Response: |
| 10 | project_name = input.get("project_name", "") |
| 11 | agent_profile = input.get("agent_profile", "") |
| 12 | context_id = str(input.get("context_id") or "").strip() |
| 13 | context = AgentContext.get(context_id) if context_id else None |
| 14 | agent = context.agent0 if context else None |
| 15 | |
| 16 | if agent: |
| 17 | config = model_config.get_effective_config(agent) |
| 18 | configured_preset = model_config.get_configured_preset_name(agent=agent) |
| 19 | selected_preset = model_config.get_effective_preset_name(agent) |
| 20 | else: |
| 21 | config = model_config.get_config( |
| 22 | project_name=project_name or None, |
| 23 | agent_profile=agent_profile or None, |
| 24 | ) |
| 25 | configured_preset = model_config.get_configured_preset_name( |
| 26 | project_name=project_name or None, |
| 27 | agent_profile=agent_profile or None, |
| 28 | ) |
| 29 | selected_preset = str( |
| 30 | config.get(model_config.MODEL_PRESET_CONFIG_KEY) |
| 31 | or model_config.DEFAULT_PRESET_NAME |
| 32 | ) |
| 33 | |
| 34 | # Add provider lists for UI dropdowns |
| 35 | chat_providers = model_config.get_chat_providers() |
| 36 | embedding_providers = model_config.get_embedding_providers() |
| 37 | chat_provider_details = get_raw_providers("chat") |
| 38 | embedding_provider_details = get_raw_providers("embedding") |
| 39 | |
| 40 | # Mask API keys - show status only |
| 41 | api_key_status = {} |
| 42 | all_providers = chat_providers + embedding_providers |
| 43 | seen = set() |
| 44 | for p in all_providers: |
| 45 | pid = p.get("value", "") |
| 46 | if pid and pid not in seen: |
| 47 | seen.add(pid) |
| 48 | key = models.get_api_key(pid) |
| 49 | api_key_status[pid] = bool(key and key.strip() and key != "None") |
| 50 | |
| 51 | chat_model = config.get("chat_model", {}) if isinstance(config, dict) else {} |
| 52 | chat_provider = str(chat_model.get("provider") or "").strip() |
| 53 | chat_name = str(chat_model.get("name") or "").strip() |
| 54 | |
| 55 | return { |
| 56 | "config": config, |
| 57 | "chat_providers": chat_providers, |
| 58 | "embedding_providers": embedding_providers, |
| 59 | "chat_provider_details": chat_provider_details, |
| 60 | "embedding_provider_details": embedding_provider_details, |
| 61 | "api_key_status": api_key_status, |
| 62 | "model_configured": model_config.is_chat_model_configured(config), |
| 63 | "model_configured_label": " / ".join( |
| 64 | part for part in (chat_provider, chat_name) if part |
| 65 | ), |
| 66 | "presets": model_config.get_presets(), |
| 67 | "configured_preset": configured_preset, |
| 68 | "selected_preset": selected_preset, |
| 69 | } |