| 1 | from agent import AgentContext |
| 2 | from helpers import projects, subagents |
| 3 | from helpers.api import ApiHandler, Request, Response |
| 4 | from helpers.persist_chat import save_tmp_chat |
| 5 | from helpers.state_monitor_integration import mark_dirty_for_context |
| 6 | from initialize import initialize_agent |
| 7 | |
| 8 | |
| 9 | class SetAgentProfile(ApiHandler): |
| 10 | async def process(self, input: dict, request: Request) -> dict | Response: |
| 11 | context_id = str(input.get("context_id", "") or "").strip() |
| 12 | profile = str(input.get("agent_profile", "") or "").strip() |
| 13 | |
| 14 | if not context_id: |
| 15 | return Response(status=400, response="Missing context_id") |
| 16 | if not profile: |
| 17 | return Response(status=400, response="Missing agent_profile") |
| 18 | |
| 19 | context = AgentContext.get(context_id) |
| 20 | if not context: |
| 21 | return Response(status=404, response="Context not found") |
| 22 | if context.is_running(): |
| 23 | return Response( |
| 24 | status=409, |
| 25 | response="Agent profile can be changed after the current run finishes.", |
| 26 | ) |
| 27 | |
| 28 | profiles = subagents.get_available_agents_dict( |
| 29 | projects.get_context_project_name(context) |
| 30 | ) |
| 31 | selected_profile = profiles.get(profile) |
| 32 | if selected_profile is None: |
| 33 | return Response(status=404, response=f"Agent profile '{profile}' not found") |
| 34 | |
| 35 | config = initialize_agent(override_settings={"agent_profile": profile}) |
| 36 | context.config = config |
| 37 | context.agent0.config = config |
| 38 | |
| 39 | save_tmp_chat(context) |
| 40 | mark_dirty_for_context(context.id, reason="agent_profile_change") |
| 41 | return { |
| 42 | "ok": True, |
| 43 | "agent_profile": profile, |
| 44 | "agent_profile_label": selected_profile.title or profile, |
| 45 | } |