Skip to content

Agent Zero AI framework

Make agent profiles context scoped

Alessandro56a42b97d7ea
diff --git a/api/agent_profile_set.py b/api/agent_profile_set.py
new file mode 100644
index 000000000..331e21fd0
--- /dev/null
+++ b/api/agent_profile_set.py
@@ -0,0 +1,54 @@
+from agent import Agent, AgentContext
+from helpers import subagents
+from helpers.api import ApiHandler, Request, Response
+from helpers.persist_chat import save_tmp_chat
+from helpers.state_monitor_integration import mark_dirty_for_context
+from initialize import initialize_agent
+
+
+def _agent_profile_labels() -> dict[str, str]:
+ return {
+ str(item.get("key") or ""): str(item.get("label") or item.get("key") or "")
+ for item in subagents.get_all_agents_list()
+ if item.get("key")
+ }
+
+
+class SetAgentProfile(ApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ context_id = str(input.get("context_id", "") or "").strip()
+ profile = str(input.get("agent_profile", "") or "").strip()
+
+ if not context_id:
+ return Response(status=400, response="Missing context_id")
+ if not profile:
+ return Response(status=400, response="Missing agent_profile")
+
+ context = AgentContext.get(context_id)
+ if not context:
+ return Response(status=404, response="Context not found")
+ if context.is_running():
+ return Response(
+ status=409,
+ response="Agent profile can be changed after the current run finishes.",
+ )
+
+ labels = _agent_profile_labels()
+ if profile not in labels:
+ return Response(status=404, response=f"Agent profile '{profile}' not found")
+
+ config = initialize_agent(override_settings={"agent_profile": profile})
+ context.config = config
+
+ agent = context.agent0
+ while agent:
+ agent.config = config
+ agent = agent.get_data(Agent.DATA_NAME_SUBORDINATE)
+
+ save_tmp_chat(context)
+ mark_dirty_for_context(context.id, reason="agent_profile_change")
+ return {
+ "ok": True,
+ "agent_profile": profile,
+ "agent_profile_label": labels.get(profile, profile),
+ }
diff --git a/docs/guides/agent-profiles.md b/docs/guides/agent-profiles.md
index 60ad2dc66..fc97c5851 100644
--- a/docs/guides/agent-profiles.md
+++ b/docs/guides/agent-profiles.md
@@ -2,7 +2,7 @@
Agent profiles let you give Agent Zero different identities, prompt overrides, tools, extensions, and optionally model settings. Use them when you want a specialized agent such as a researcher, developer, security auditor, copywriter, or domain-specific assistant.
-You can select the active profile from **Settings > Agent Config** or from the chat composer profile selector.
+Use **Settings > Agent Config** to choose the default profile for new chats. The chat composer profile selector shows and changes the profile for the currently selected chat only, so different chats can keep different active profiles.
## Where Profiles Live
diff --git a/docs/setup/installation.md b/docs/setup/installation.md
index 727dd5a07..68c66e31d 100644
--- a/docs/setup/installation.md
+++ b/docs/setup/installation.md
@@ -318,7 +318,7 @@ See the [Agent Profiles guide](../guides/agent-profiles.md) for profile file loc
> Since v0.9.7, custom prompts belong in `/a0/agents/<agent_name>/prompts/` rather than a shared `/prompts` folder. See the [Extensions guide](../developer/extensions.md#prompts) for details.
> [!NOTE]
-> The Hacker profile is included in the main image. After launch, choose the **hacker** agent profile in Settings if you want the security-focused prompts and tooling. The "hacker" branch is deprecated.
+> The Hacker profile is included in the main image. After launch, choose the **hacker** agent profile in Settings to make it the default for new chats, or switch the selected chat from the composer profile selector. The "hacker" branch is deprecated.
![settings](../res/setup/settings/1-agentConfig.png)
diff --git a/helpers/persist_chat.py b/helpers/persist_chat.py
index db04e1b5e..3d0081ddd 100644
--- a/helpers/persist_chat.py
+++ b/helpers/persist_chat.py
@@ -116,6 +116,12 @@ def remove_msg_files(ctxid):
def _serialize_context(context: AgentContext):
+ profile = str(
+ getattr(context.config, "profile", None)
+ or getattr(context.agent0.config, "profile", None)
+ or ""
+ )
+
# serialize agents
agents = []
agent = context.agent0
@@ -145,6 +151,7 @@ def _serialize_context(context: AgentContext):
"streaming_agent": (
context.streaming_agent.number if context.streaming_agent else 0
),
+ "agent_profile": profile,
"log": _serialize_log(context.log),
"data": data,
"output_data": output_data,
@@ -179,7 +186,9 @@ def _serialize_log(log: Log):
def _deserialize_context(data):
- config = initialize_agent()
+ profile = data.get("agent_profile")
+ override_settings = {"agent_profile": profile} if profile else None
+ config = initialize_agent(override_settings=override_settings)
log = _deserialize_log(data.get("log", None))
context = AgentContext(
diff --git a/helpers/settings.py b/helpers/settings.py
index cfa587347..eedfaa00e 100644
--- a/helpers/settings.py
+++ b/helpers/settings.py
@@ -496,8 +496,9 @@ def _apply_settings(previous: Settings | None):
from agent import AgentContext
from initialize import initialize_agent
- config = initialize_agent()
for ctx in AgentContext.all():
+ profile = str(getattr(ctx.config, "profile", "") or _settings["agent_profile"])
+ config = initialize_agent(override_settings={"agent_profile": profile})
ctx.config = config # reinitialize context config with new settings
# apply config to agents
agent = ctx.agent0
diff --git a/plugins/_a0_connector/api/v1/agent_profile_set.py b/plugins/_a0_connector/api/v1/agent_profile_set.py
new file mode 100644
index 000000000..30401c772
--- /dev/null
+++ b/plugins/_a0_connector/api/v1/agent_profile_set.py
@@ -0,0 +1,13 @@
+"""POST /api/plugins/_a0_connector/v1/agent_profile_set."""
+from __future__ import annotations
+
+from helpers.api import Request, Response
+import plugins._a0_connector.api.v1.base as connector_base
+
+
+class AgentProfileSet(connector_base.ProtectedConnectorApiHandler):
+ async def process(self, input: dict, request: Request) -> dict | Response:
+ from api.agent_profile_set import SetAgentProfile
+
+ handler = SetAgentProfile(self.app, self.thread_lock)
+ return await handler.process(input, request)
diff --git a/plugins/_a0_connector/api/v1/capabilities.py b/plugins/_a0_connector/api/v1/capabilities.py
index 446163b44..2189863bd 100644
--- a/plugins/_a0_connector/api/v1/capabilities.py
+++ b/plugins/_a0_connector/api/v1/capabilities.py
@@ -29,6 +29,7 @@ _BASE_FEATURES = [
_OPTIONAL_FEATURES: dict[str, tuple[str, ...]] = {
"settings_get": ("helpers.settings", "helpers.subagents"),
"settings_set": ("helpers.settings", "helpers.subagents"),
+ "agent_profile_set": ("api.agent_profile_set",),
"agents_list": ("helpers.subagents",),
"skills_list": ("helpers.skills", "helpers.files", "helpers.projects", "helpers.runtime"),
"skills_delete": ("helpers.skills", "helpers.files", "helpers.projects", "helpers.runtime"),
diff --git a/plugins/_model_config/webui/switcher-mixin.js b/plugins/_model_config/webui/switcher-mixin.js
index f05cf27ae..14b50a0d7 100644
--- a/plugins/_model_config/webui/switcher-mixin.js
+++ b/plugins/_model_config/webui/switcher-mixin.js
@@ -144,35 +144,26 @@ export const switcherMethods = {
return false;
}
- const activeProfile = selectedContext?.agent_profile || this.agentProfileSettings?.agent_profile || "";
+ const activeProfile = selectedContext?.agent_profile || "";
if (activeProfile === agentProfile) return true;
this.agentProfileSaving = true;
try {
await this.loadAgentProfiles();
- const settings = { ...(this.agentProfileSettings || {}), agent_profile: agentProfile };
- const res = await fetchApi("/settings_set", {
+ const res = await fetchApi("/agent_profile_set", {
method: "POST",
headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ settings }),
+ body: JSON.stringify({ context_id: contextId, agent_profile: agentProfile }),
});
+ if (!res.ok) throw new Error(await res.text());
const data = await res.json();
- if (!data.settings) return false;
+ if (!data.ok) return false;
- this.agentProfileSettings = data.settings;
- this.agentProfiles = (data.additional?.agent_subdirs || this.agentProfiles)
- .map(profile => ({
- key: profile.value || profile.key || "",
- label: profile.label || profile.value || profile.key || "",
- }))
- .filter(profile => profile.key && profile.key !== "_example");
-
- const label = this.agentProfiles.find(profile => profile.key === agentProfile)?.label || agentProfile;
+ const label = data.agent_profile_label || this.agentProfiles.find(profile => profile.key === agentProfile)?.label || agentProfile;
if (selectedContext) {
- selectedContext.agent_profile = agentProfile;
+ selectedContext.agent_profile = data.agent_profile || agentProfile;
selectedContext.agent_profile_label = label;
}
- document.dispatchEvent(new CustomEvent("settings-updated", { detail: this.agentProfileSettings }));
window.justToast?.(`Agent profile: ${label}`, "success", 1600, "agent-profile-switch");
return true;
} catch (e) {
diff --git a/webui/components/settings/agent/agent.html b/webui/components/settings/agent/agent.html
index 8d4e95469..410c5dba4 100644
--- a/webui/components/settings/agent/agent.html
+++ b/webui/components/settings/agent/agent.html
@@ -16,7 +16,7 @@
<div class="field-label">
<div class="field-title">Default agent profile</div>
<div class="field-description">
- Subdirectory of /agents folder to be used by default agent no. 0. Subordinate agents can be spawned with other profiles, that is on their superior agent to decide. This setting affects the behaviour of the top level agent you communicate with.
+ Profile used for new top-level chats. Existing chats keep their own active profile; use the chat composer selector to change the selected chat.
</div>
</div>
<div class="field-control">