Skip to content

Agent Zero AI framework

Merge branch 'pr/846' into development

frdelef68a16eeedf
diff --cc python/api/api_message.py
index f955024d2,9fb433335..c25cb5369
--- a/python/api/api_message.py
+++ b/python/api/api_message.py
@@@ -3,8 -3,9 +3,9 @@@ import o
from datetime import datetime, timedelta
from agent import AgentContext, UserMessage, AgentContextType
from python.helpers.api import ApiHandler, Request, Response
-from python.helpers import files
+from python.helpers import files, projects
from python.helpers.print_style import PrintStyle
+ from python.helpers.projects import activate_project
from werkzeug.utils import secure_filename
from initialize import initialize_agent
import threading
@@@ -72,24 -79,27 +79,42 @@@ class ApiMessage(ApiHandler)
context = AgentContext.use(context_id)
if not context:
return Response('{"error": "Context not found"}', status=404, mimetype="application/json")
+
++ # Validation: if agent profile is provided, it must match the exising
++ if agent_profile and context.agent0.config.profile != agent_profile:
++ return Response('{"error": "Cannot override agent profile on existing context"}', status=400, mimetype="application/json")
++
++
+ # Validation: if project is provided but context already has different project
+ existing_project = context.get_data(projects.CONTEXT_DATA_KEY_PROJECT)
- if project and existing_project and existing_project != project:
++ if project_name and existing_project and existing_project != project_name:
+ return Response('{"error": "Project can only be set on first message"}', status=400, mimetype="application/json")
else:
- config = initialize_agent()
+ config = initialize_agent(override_settings=override_settings)
context = AgentContext(config=config, type=AgentContextType.USER)
AgentContext.use(context.id)
context_id = context.id
+ # Activate project if provided
+ if project_name:
+ try:
+ activate_project(context_id, project_name)
+ except Exception as e:
+ # Handle project or context errors more gracefully
+ error_msg = str(e)
+ PrintStyle.error(f"Failed to activate project '{project_name}' for context '{context_id}': {error_msg}")
+ return Response(
+ f'{{"error": "Failed to activate project \\"{project_name}\\""}}',
+ status=500,
+ mimetype="application/json",
+ )
+ # Activate project if provided
- if project:
++ if project_name:
+ try:
- projects.activate_project(context_id, project)
++ projects.activate_project(context_id, project_name)
+ except Exception as e:
+ return Response(f'{{"error": "Failed to activate project: {str(e)}"}}', status=400, mimetype="application/json")
+
# Update chat lifetime
with self._cleanup_lock:
self._chat_lifetimes[context_id] = datetime.now() + timedelta(hours=lifetime_hours)