From 622f9541195327d8d7c4a76e8940ec48e8683dd8 Mon Sep 17 00:00:00 2001 From: Saumya Date: Tue, 22 Jul 2025 22:46:34 +0530 Subject: [PATCH] use suna config from the code --- backend/agent/config_helper.py | 35 +++++++++++++++++++++----- backend/agent/suna/repository.py | 36 ++++++++------------------- backend/agent/suna/sync_service.py | 22 +++------------- backend/agent/suna/version_service.py | 24 +++++++++++------- 4 files changed, 58 insertions(+), 59 deletions(-) diff --git a/backend/agent/config_helper.py b/backend/agent/config_helper.py index 030a6f5c..dee32f6f 100644 --- a/backend/agent/config_helper.py +++ b/backend/agent/config_helper.py @@ -15,6 +15,16 @@ def extract_agent_config(agent_data: Dict[str, Any], version_data: Optional[Dict if version_data and ('configured_mcps' in version_data or 'custom_mcps' in version_data or 'system_prompt' in version_data): logger.info(f"Using version data from version manager for agent {agent_id}") + + # For Suna default agents, always use current system prompt & tools from code + if is_suna_default: + from agent.suna.config import SunaConfig + system_prompt = SunaConfig.get_system_prompt() + agentpress_tools = SunaConfig.DEFAULT_TOOLS + else: + system_prompt = version_data.get('system_prompt', '') + agentpress_tools = version_data.get('agentpress_tools', {}) + config = { 'agent_id': agent_data['agent_id'], 'name': agent_data['name'], @@ -23,14 +33,14 @@ def extract_agent_config(agent_data: Dict[str, Any], version_data: Optional[Dict 'account_id': agent_data.get('account_id'), 'current_version_id': agent_data.get('current_version_id'), 'version_name': version_data.get('version_name', 'v1'), - 'system_prompt': version_data.get('system_prompt', ''), + 'system_prompt': system_prompt, 'configured_mcps': version_data.get('configured_mcps', []), 'custom_mcps': version_data.get('custom_mcps', []), - 'agentpress_tools': version_data.get('agentpress_tools', {}), + 'agentpress_tools': agentpress_tools, 'avatar': agent_data.get('avatar'), 'avatar_color': agent_data.get('avatar_color'), 'tools': { - 'agentpress': version_data.get('agentpress_tools', {}), + 'agentpress': agentpress_tools, 'mcp': version_data.get('configured_mcps', []), 'custom_mcp': version_data.get('custom_mcps', []) }, @@ -55,6 +65,12 @@ def extract_agent_config(agent_data: Dict[str, Any], version_data: Optional[Dict config['current_version_id'] = agent_data.get('current_version_id') config['version_name'] = version_data.get('version_name', 'v1') + # For Suna default agents, override with current system prompt & tools from code + if is_suna_default: + from agent.suna.config import SunaConfig + config['system_prompt'] = SunaConfig.get_system_prompt() + config['tools']['agentpress'] = SunaConfig.DEFAULT_TOOLS + metadata = config.get('metadata', {}) config['avatar'] = metadata.get('avatar', agent_data.get('avatar')) config['avatar_color'] = metadata.get('avatar_color', agent_data.get('avatar_color')) @@ -107,9 +123,16 @@ def extract_agent_config(agent_data: Dict[str, Any], version_data: Optional[Dict source_data = version_data if version_data else agent_data - legacy_tools = source_data.get('agentpress_tools', {}) - simplified_tools = {} + # For Suna default agents, always use current system prompt & tools from code + if is_suna_default: + from agent.suna.config import SunaConfig + system_prompt = SunaConfig.get_system_prompt() + legacy_tools = SunaConfig.DEFAULT_TOOLS + else: + system_prompt = source_data.get('system_prompt', '') + legacy_tools = source_data.get('agentpress_tools', {}) + simplified_tools = {} for tool_name, tool_config in legacy_tools.items(): if isinstance(tool_config, dict): simplified_tools[tool_name] = tool_config.get('enabled', False) @@ -120,7 +143,7 @@ def extract_agent_config(agent_data: Dict[str, Any], version_data: Optional[Dict 'agent_id': agent_data['agent_id'], 'name': agent_data['name'], 'description': agent_data.get('description'), - 'system_prompt': source_data.get('system_prompt', ''), + 'system_prompt': system_prompt, 'tools': { 'agentpress': simplified_tools, 'mcp': source_data.get('configured_mcps', []), diff --git a/backend/agent/suna/repository.py b/backend/agent/suna/repository.py index f6ffcbb1..d0a8b172 100644 --- a/backend/agent/suna/repository.py +++ b/backend/agent/suna/repository.py @@ -70,31 +70,18 @@ class SunaAgentRepository: current_agent = current_agent_result.data[0] current_metadata = current_agent.get('metadata', {}) - print(f"[DEBUG] Repository - config_data keys: {config_data.keys()}") - print(f"[DEBUG] Repository - config_data custom_mcps: {config_data.get('custom_mcps', 'NOT PROVIDED')}") - # Get MCPs - use what sync provides, only fallback if not provided - if 'configured_mcps' in config_data: - preserved_configured_mcps = config_data['configured_mcps'] - else: - preserved_configured_mcps = current_agent.get('configured_mcps', []) - print(f"[DEBUG] Repository - configured_mcps not in config_data, using current agent data") - - if 'custom_mcps' in config_data: - preserved_custom_mcps = config_data['custom_mcps'] - else: - preserved_custom_mcps = current_agent.get('custom_mcps', []) - print(f"[DEBUG] Repository - custom_mcps not in config_data, using current agent data") + preserved_configured_mcps = config_data.get('configured_mcps', current_agent.get('configured_mcps', [])) + preserved_custom_mcps = config_data.get('custom_mcps', current_agent.get('custom_mcps', [])) for i, mcp in enumerate(preserved_custom_mcps): # Handle both camelCase and snake_case tools_count = len(mcp.get('enabledTools', mcp.get('enabled_tools', []))) logger.info(f"Agent {agent_id} - Preserving custom MCP {i+1} ({mcp.get('name', 'Unknown')}) with {tools_count} enabled tools") - print(f"[DEBUG] Repository - MCP {i+1} full data: {mcp}") + # For Suna agents, only update metadata and MCPs + # System prompt & tools are read dynamically from SunaConfig update_data = { - "system_prompt": config_data["system_prompt"], - "agentpress_tools": config_data["agentpress_tools"], "configured_mcps": preserved_configured_mcps, "custom_mcps": preserved_custom_mcps, "metadata": { @@ -103,24 +90,21 @@ class SunaAgentRepository: } } + # For Suna agents, build unified config with current system prompt & tools from code + from agent.suna.config import SunaConfig preserved_unified_config = self._build_preserved_unified_config( - system_prompt=config_data["system_prompt"], - agentpress_tools=config_data["agentpress_tools"], + system_prompt=SunaConfig.get_system_prompt(), + agentpress_tools=SunaConfig.DEFAULT_TOOLS, configured_mcps=preserved_configured_mcps, custom_mcps=preserved_custom_mcps, - avatar=config_data["avatar"], - avatar_color=config_data["avatar_color"] + avatar=SunaConfig.AVATAR, + avatar_color=SunaConfig.AVATAR_COLOR ) update_data["config"] = preserved_unified_config - print(f"[DEBUG] Repository - preserved_unified_config tools.custom_mcp: {preserved_unified_config.get('tools', {}).get('custom_mcp', [])}") - print(f"[DEBUG] Repository - update_data custom_mcps being sent to DB: {update_data.get('custom_mcps', [])}") - result = await client.table('agents').update(update_data).eq('agent_id', agent_id).execute() - print(f"[DEBUG] Repository - Update result: {bool(result.data)}") - logger.info(f"Surgically updated agent {agent_id} - preserved MCPs and customizations") return bool(result.data) diff --git a/backend/agent/suna/sync_service.py b/backend/agent/suna/sync_service.py index 58eacec9..aaa9c38e 100644 --- a/backend/agent/suna/sync_service.py +++ b/backend/agent/suna/sync_service.py @@ -212,22 +212,11 @@ class SunaSyncService: ).eq('agent_id', agent.agent_id).execute() current_agent_data = current_agent_result.data[0] if current_agent_result.data else {} - print(f"[DEBUG] Current agent has {len(current_agent_data.get('custom_mcps', []))} custom MCPs in DB") - current_user_mcps = await self.version_service._get_current_user_mcps(agent.agent_id, agent.account_id) - # Debug: Log exactly what we got from version service - print(f"[DEBUG] Retrieved MCPs from version service for agent {agent.agent_id}:") - print(f"[DEBUG] Raw current_user_mcps: {current_user_mcps}") - for i, mcp in enumerate(current_user_mcps.get('custom_mcps', [])): - # Handle both camelCase and snake_case - enabled_tools = mcp.get('enabledTools', mcp.get('enabled_tools', [])) - print(f"[DEBUG] - MCP {i+1}: {mcp.get('name', 'Unknown')} with {len(enabled_tools)} tools: {enabled_tools[:3]}{'...' if len(enabled_tools) > 3 else ''}") - print(f"[DEBUG] Full MCP data: {mcp}") - - # IMPORTANT: If version returned empty MCPs but agent has MCPs, use agent's MCPs + # If version returned empty MCPs but agent has MCPs, use agent's MCPs if not current_user_mcps.get('custom_mcps') and current_agent_data.get('custom_mcps'): - print(f"[DEBUG] Version had no MCPs, using agent's current MCPs instead") + logger.info(f"Version had no MCPs for agent {agent.agent_id}, using agent's current MCPs") current_user_mcps['custom_mcps'] = current_agent_data.get('custom_mcps', []) current_config = extract_agent_config(current_agent_data) if current_agent_data else {} @@ -236,11 +225,9 @@ class SunaSyncService: logger.info(f"🔍 Preserved {len(current_config.get('configured_mcps', []))} configured MCPs and {len(current_config.get('custom_mcps', []))} custom MCPs for agent {agent.agent_id}") + # For Suna agents, we only need to sync metadata and preserve user MCPs + # System prompt & tools are now read dynamically from SunaConfig minimal_config_data = { - "system_prompt": config.system_prompt, - "agentpress_tools": config.agentpress_tools, - "avatar": config.avatar, - "avatar_color": config.avatar_color, "configured_mcps": current_config.get('configured_mcps', []), "custom_mcps": current_config.get('custom_mcps', []), "metadata": { @@ -251,7 +238,6 @@ class SunaSyncService: } } - print(f"[DEBUG] Sync - minimal_config_data custom_mcps: {minimal_config_data.get('custom_mcps', [])}") logger.info(f"🔧 Preserving {len(minimal_config_data.get('configured_mcps', []))} configured MCPs and {len(minimal_config_data.get('custom_mcps', []))} custom MCPs for agent {agent.agent_id}") agent_updated = await self.repository.update_agent_record( diff --git a/backend/agent/suna/version_service.py b/backend/agent/suna/version_service.py index e0b16422..c1cc940f 100644 --- a/backend/agent/suna/version_service.py +++ b/backend/agent/suna/version_service.py @@ -31,15 +31,18 @@ class SunaVersionService: set_db_connection(self.db) + # For Suna agents, use current system prompt & tools from code + from agent.suna.config import SunaConfig + version = await version_manager.create_version( agent_id=agent_id, user_id=account_id, - system_prompt=config_data["system_prompt"], + system_prompt=SunaConfig.get_system_prompt(), configured_mcps=current_mcps["configured_mcps"], custom_mcps=current_mcps["custom_mcps"], - agentpress_tools=config_data["agentpress_tools"], + agentpress_tools=SunaConfig.DEFAULT_TOOLS, version_name=f"sync-{version_tag}", - change_description=f"Auto-sync system prompt & tools from suna_config.py (preserved MCPs)" + change_description=f"Auto-sync to latest SunaConfig (preserved user MCPs)" ) logger.info(f"Created sync version {version['version_id']} for agent {agent_id} (preserved user MCPs)") @@ -70,13 +73,16 @@ class SunaVersionService: set_db_connection(self.db) + # For Suna agents, use current system prompt & tools from code + from agent.suna.config import SunaConfig + version = await version_manager.create_version( agent_id=agent_id, user_id=account_id, - system_prompt=config_data["system_prompt"], - configured_mcps=config_data["configured_mcps"], - custom_mcps=config_data["custom_mcps"], - agentpress_tools=config_data["agentpress_tools"], + system_prompt=SunaConfig.get_system_prompt(), + configured_mcps=config_data.get("configured_mcps", []), + custom_mcps=config_data.get("custom_mcps", []), + agentpress_tools=SunaConfig.DEFAULT_TOOLS, version_name="v1", change_description="Initial Suna default agent version" ) @@ -143,7 +149,7 @@ class SunaVersionService: normalized_mcp['enabledTools'] = normalized_mcp.pop('enabled_tools') normalized_custom_mcps.append(normalized_mcp) - print(f"[DEBUG] Version data custom_mcps (normalized): {normalized_custom_mcps}") + logger.debug(f"Normalized {len(normalized_custom_mcps)} custom MCPs for agent {agent_id}") return { "configured_mcps": version_data.get('configured_mcps', []), "custom_mcps": normalized_custom_mcps @@ -164,7 +170,7 @@ class SunaVersionService: normalized_mcp['enabledTools'] = normalized_mcp.pop('enabled_tools') normalized_custom_mcps.append(normalized_mcp) - print(f"[DEBUG] Agent record custom_mcps (normalized): {normalized_custom_mcps}") + logger.debug(f"Normalized {len(normalized_custom_mcps)} custom MCPs from agent record for agent {agent_id}") return { "configured_mcps": agent_data.get('configured_mcps', []), "custom_mcps": normalized_custom_mcps