use suna config from the code

This commit is contained in:
Saumya 2025-07-22 22:46:34 +05:30
parent 34ae50a68b
commit 622f954119
4 changed files with 58 additions and 59 deletions

View File

@ -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): 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}") 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 = { config = {
'agent_id': agent_data['agent_id'], 'agent_id': agent_data['agent_id'],
'name': agent_data['name'], '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'), 'account_id': agent_data.get('account_id'),
'current_version_id': agent_data.get('current_version_id'), 'current_version_id': agent_data.get('current_version_id'),
'version_name': version_data.get('version_name', 'v1'), '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', []), 'configured_mcps': version_data.get('configured_mcps', []),
'custom_mcps': version_data.get('custom_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': agent_data.get('avatar'),
'avatar_color': agent_data.get('avatar_color'), 'avatar_color': agent_data.get('avatar_color'),
'tools': { 'tools': {
'agentpress': version_data.get('agentpress_tools', {}), 'agentpress': agentpress_tools,
'mcp': version_data.get('configured_mcps', []), 'mcp': version_data.get('configured_mcps', []),
'custom_mcp': version_data.get('custom_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['current_version_id'] = agent_data.get('current_version_id')
config['version_name'] = version_data.get('version_name', 'v1') 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', {}) metadata = config.get('metadata', {})
config['avatar'] = metadata.get('avatar', agent_data.get('avatar')) config['avatar'] = metadata.get('avatar', agent_data.get('avatar'))
config['avatar_color'] = metadata.get('avatar_color', agent_data.get('avatar_color')) 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 source_data = version_data if version_data else agent_data
legacy_tools = source_data.get('agentpress_tools', {}) # For Suna default agents, always use current system prompt & tools from code
simplified_tools = {} 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(): for tool_name, tool_config in legacy_tools.items():
if isinstance(tool_config, dict): if isinstance(tool_config, dict):
simplified_tools[tool_name] = tool_config.get('enabled', False) 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'], 'agent_id': agent_data['agent_id'],
'name': agent_data['name'], 'name': agent_data['name'],
'description': agent_data.get('description'), 'description': agent_data.get('description'),
'system_prompt': source_data.get('system_prompt', ''), 'system_prompt': system_prompt,
'tools': { 'tools': {
'agentpress': simplified_tools, 'agentpress': simplified_tools,
'mcp': source_data.get('configured_mcps', []), 'mcp': source_data.get('configured_mcps', []),

View File

@ -70,31 +70,18 @@ class SunaAgentRepository:
current_agent = current_agent_result.data[0] current_agent = current_agent_result.data[0]
current_metadata = current_agent.get('metadata', {}) 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 # Get MCPs - use what sync provides, only fallback if not provided
if 'configured_mcps' in config_data: preserved_configured_mcps = config_data.get('configured_mcps', current_agent.get('configured_mcps', []))
preserved_configured_mcps = config_data['configured_mcps'] preserved_custom_mcps = config_data.get('custom_mcps', current_agent.get('custom_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")
for i, mcp in enumerate(preserved_custom_mcps): for i, mcp in enumerate(preserved_custom_mcps):
# Handle both camelCase and snake_case # Handle both camelCase and snake_case
tools_count = len(mcp.get('enabledTools', mcp.get('enabled_tools', []))) 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") 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 = { update_data = {
"system_prompt": config_data["system_prompt"],
"agentpress_tools": config_data["agentpress_tools"],
"configured_mcps": preserved_configured_mcps, "configured_mcps": preserved_configured_mcps,
"custom_mcps": preserved_custom_mcps, "custom_mcps": preserved_custom_mcps,
"metadata": { "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( preserved_unified_config = self._build_preserved_unified_config(
system_prompt=config_data["system_prompt"], system_prompt=SunaConfig.get_system_prompt(),
agentpress_tools=config_data["agentpress_tools"], agentpress_tools=SunaConfig.DEFAULT_TOOLS,
configured_mcps=preserved_configured_mcps, configured_mcps=preserved_configured_mcps,
custom_mcps=preserved_custom_mcps, custom_mcps=preserved_custom_mcps,
avatar=config_data["avatar"], avatar=SunaConfig.AVATAR,
avatar_color=config_data["avatar_color"] avatar_color=SunaConfig.AVATAR_COLOR
) )
update_data["config"] = preserved_unified_config 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() 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") logger.info(f"Surgically updated agent {agent_id} - preserved MCPs and customizations")
return bool(result.data) return bool(result.data)

View File

@ -212,22 +212,11 @@ class SunaSyncService:
).eq('agent_id', agent.agent_id).execute() ).eq('agent_id', agent.agent_id).execute()
current_agent_data = current_agent_result.data[0] if current_agent_result.data else {} 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) 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 # If version returned empty MCPs but agent has MCPs, use agent's MCPs
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 not current_user_mcps.get('custom_mcps') and current_agent_data.get('custom_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_user_mcps['custom_mcps'] = current_agent_data.get('custom_mcps', [])
current_config = extract_agent_config(current_agent_data) if current_agent_data else {} 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}") 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 = { 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', []), "configured_mcps": current_config.get('configured_mcps', []),
"custom_mcps": current_config.get('custom_mcps', []), "custom_mcps": current_config.get('custom_mcps', []),
"metadata": { "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}") 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( agent_updated = await self.repository.update_agent_record(

View File

@ -31,15 +31,18 @@ class SunaVersionService:
set_db_connection(self.db) 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( version = await version_manager.create_version(
agent_id=agent_id, agent_id=agent_id,
user_id=account_id, user_id=account_id,
system_prompt=config_data["system_prompt"], system_prompt=SunaConfig.get_system_prompt(),
configured_mcps=current_mcps["configured_mcps"], configured_mcps=current_mcps["configured_mcps"],
custom_mcps=current_mcps["custom_mcps"], custom_mcps=current_mcps["custom_mcps"],
agentpress_tools=config_data["agentpress_tools"], agentpress_tools=SunaConfig.DEFAULT_TOOLS,
version_name=f"sync-{version_tag}", 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)") 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) 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( version = await version_manager.create_version(
agent_id=agent_id, agent_id=agent_id,
user_id=account_id, user_id=account_id,
system_prompt=config_data["system_prompt"], system_prompt=SunaConfig.get_system_prompt(),
configured_mcps=config_data["configured_mcps"], configured_mcps=config_data.get("configured_mcps", []),
custom_mcps=config_data["custom_mcps"], custom_mcps=config_data.get("custom_mcps", []),
agentpress_tools=config_data["agentpress_tools"], agentpress_tools=SunaConfig.DEFAULT_TOOLS,
version_name="v1", version_name="v1",
change_description="Initial Suna default agent version" change_description="Initial Suna default agent version"
) )
@ -143,7 +149,7 @@ class SunaVersionService:
normalized_mcp['enabledTools'] = normalized_mcp.pop('enabled_tools') normalized_mcp['enabledTools'] = normalized_mcp.pop('enabled_tools')
normalized_custom_mcps.append(normalized_mcp) 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 { return {
"configured_mcps": version_data.get('configured_mcps', []), "configured_mcps": version_data.get('configured_mcps', []),
"custom_mcps": normalized_custom_mcps "custom_mcps": normalized_custom_mcps
@ -164,7 +170,7 @@ class SunaVersionService:
normalized_mcp['enabledTools'] = normalized_mcp.pop('enabled_tools') normalized_mcp['enabledTools'] = normalized_mcp.pop('enabled_tools')
normalized_custom_mcps.append(normalized_mcp) 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 { return {
"configured_mcps": agent_data.get('configured_mcps', []), "configured_mcps": agent_data.get('configured_mcps', []),
"custom_mcps": normalized_custom_mcps "custom_mcps": normalized_custom_mcps