2025-07-08 12:51:17 +08:00
|
|
|
from typing import Dict, Any, Optional, List
|
|
|
|
from utils.logger import logger
|
2025-08-25 03:17:41 +08:00
|
|
|
import os
|
2025-07-08 12:51:17 +08:00
|
|
|
|
2025-07-25 15:54:34 +08:00
|
|
|
|
2025-07-08 12:51:17 +08:00
|
|
|
def extract_agent_config(agent_data: Dict[str, Any], version_data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
2025-08-17 08:44:57 +08:00
|
|
|
"""Extract agent configuration with simplified logic for Suna vs custom agents."""
|
2025-07-13 22:29:38 +08:00
|
|
|
agent_id = agent_data.get('agent_id', 'Unknown')
|
2025-07-23 00:11:10 +08:00
|
|
|
metadata = agent_data.get('metadata', {})
|
|
|
|
is_suna_default = metadata.get('is_suna_default', False)
|
2025-08-17 08:44:57 +08:00
|
|
|
|
2025-08-25 03:17:41 +08:00
|
|
|
# Debug logging
|
|
|
|
if os.getenv("ENV_MODE", "").upper() == "STAGING":
|
|
|
|
print(f"[DEBUG] extract_agent_config: Called for agent {agent_id}, is_suna_default={is_suna_default}")
|
|
|
|
print(f"[DEBUG] extract_agent_config: Input agent_data has icon_name={agent_data.get('icon_name')}, icon_color={agent_data.get('icon_color')}, icon_background={agent_data.get('icon_background')}")
|
|
|
|
|
2025-08-17 08:44:57 +08:00
|
|
|
# Handle Suna agents with special logic
|
|
|
|
if is_suna_default:
|
|
|
|
return _extract_suna_agent_config(agent_data, version_data)
|
|
|
|
|
|
|
|
# Handle custom agents with versioning
|
|
|
|
return _extract_custom_agent_config(agent_data, version_data)
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_suna_agent_config(agent_data: Dict[str, Any], version_data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
|
|
|
"""Extract config for Suna agents - always use central config with user customizations."""
|
2025-08-17 10:34:52 +08:00
|
|
|
from agent.suna_config import SUNA_CONFIG
|
2025-08-17 08:44:57 +08:00
|
|
|
|
|
|
|
agent_id = agent_data.get('agent_id', 'Unknown')
|
2025-08-17 10:10:56 +08:00
|
|
|
logger.debug(f"Using Suna central config for agent {agent_id}")
|
2025-08-17 08:44:57 +08:00
|
|
|
|
|
|
|
# Start with central Suna config
|
|
|
|
config = {
|
|
|
|
'agent_id': agent_data['agent_id'],
|
|
|
|
'name': SUNA_CONFIG['name'],
|
|
|
|
'description': SUNA_CONFIG['description'],
|
|
|
|
'system_prompt': SUNA_CONFIG['system_prompt'],
|
|
|
|
'model': SUNA_CONFIG['model'],
|
|
|
|
'agentpress_tools': _extract_agentpress_tools_for_run(SUNA_CONFIG['agentpress_tools']),
|
|
|
|
'avatar': SUNA_CONFIG['avatar'],
|
|
|
|
'avatar_color': SUNA_CONFIG['avatar_color'],
|
|
|
|
'is_default': True,
|
|
|
|
'is_suna_default': True,
|
|
|
|
'centrally_managed': True,
|
|
|
|
'account_id': agent_data.get('account_id'),
|
|
|
|
'current_version_id': agent_data.get('current_version_id'),
|
|
|
|
'version_name': version_data.get('version_name', 'v1') if version_data else 'v1',
|
|
|
|
'profile_image_url': agent_data.get('profile_image_url'),
|
|
|
|
'restrictions': {
|
|
|
|
'system_prompt_editable': False,
|
|
|
|
'tools_editable': False,
|
|
|
|
'name_editable': False,
|
|
|
|
'description_editable': False,
|
|
|
|
'mcps_editable': True
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if version_data:
|
|
|
|
if version_data.get('config'):
|
|
|
|
version_config = version_data['config']
|
|
|
|
tools = version_config.get('tools', {})
|
|
|
|
config['configured_mcps'] = tools.get('mcp', [])
|
|
|
|
config['custom_mcps'] = tools.get('custom_mcp', [])
|
|
|
|
config['workflows'] = version_config.get('workflows', [])
|
|
|
|
config['triggers'] = version_config.get('triggers', [])
|
|
|
|
else:
|
|
|
|
config['configured_mcps'] = version_data.get('configured_mcps', [])
|
|
|
|
config['custom_mcps'] = version_data.get('custom_mcps', [])
|
|
|
|
config['workflows'] = []
|
|
|
|
config['triggers'] = []
|
|
|
|
else:
|
|
|
|
config['configured_mcps'] = agent_data.get('configured_mcps', [])
|
|
|
|
config['custom_mcps'] = agent_data.get('custom_mcps', [])
|
|
|
|
config['workflows'] = []
|
|
|
|
config['triggers'] = []
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_custom_agent_config(agent_data: Dict[str, Any], version_data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
|
|
|
agent_id = agent_data.get('agent_id', 'Unknown')
|
2025-07-23 00:11:10 +08:00
|
|
|
|
2025-08-25 03:17:41 +08:00
|
|
|
# Debug logging for icon fields
|
|
|
|
if os.getenv("ENV_MODE", "").upper() == "STAGING":
|
|
|
|
print(f"[DEBUG] _extract_custom_agent_config: Input agent_data has icon_name={agent_data.get('icon_name')}, icon_color={agent_data.get('icon_color')}, icon_background={agent_data.get('icon_background')}")
|
|
|
|
|
2025-07-24 02:45:53 +08:00
|
|
|
if version_data:
|
2025-08-17 10:10:56 +08:00
|
|
|
logger.debug(f"Using version data for custom agent {agent_id} (version: {version_data.get('version_name', 'unknown')})")
|
2025-07-23 01:16:34 +08:00
|
|
|
|
2025-07-24 02:45:53 +08:00
|
|
|
if version_data.get('config'):
|
|
|
|
config = version_data['config'].copy()
|
|
|
|
system_prompt = config.get('system_prompt', '')
|
2025-08-10 23:42:47 +08:00
|
|
|
model = config.get('model')
|
2025-07-24 02:45:53 +08:00
|
|
|
tools = config.get('tools', {})
|
|
|
|
configured_mcps = tools.get('mcp', [])
|
|
|
|
custom_mcps = tools.get('custom_mcp', [])
|
|
|
|
agentpress_tools = tools.get('agentpress', {})
|
2025-08-15 03:56:31 +08:00
|
|
|
workflows = config.get('workflows', [])
|
2025-08-16 19:26:13 +08:00
|
|
|
triggers = config.get('triggers', [])
|
2025-07-24 02:45:53 +08:00
|
|
|
else:
|
|
|
|
system_prompt = version_data.get('system_prompt', '')
|
2025-08-10 23:42:47 +08:00
|
|
|
model = version_data.get('model')
|
2025-07-24 02:45:53 +08:00
|
|
|
configured_mcps = version_data.get('configured_mcps', [])
|
|
|
|
custom_mcps = version_data.get('custom_mcps', [])
|
|
|
|
agentpress_tools = version_data.get('agentpress_tools', {})
|
2025-08-17 08:44:57 +08:00
|
|
|
workflows = []
|
|
|
|
triggers = []
|
2025-07-24 02:45:53 +08:00
|
|
|
|
2025-08-25 03:17:41 +08:00
|
|
|
config = {
|
2025-07-14 01:56:24 +08:00
|
|
|
'agent_id': agent_data['agent_id'],
|
|
|
|
'name': agent_data['name'],
|
|
|
|
'description': agent_data.get('description'),
|
2025-07-23 01:16:34 +08:00
|
|
|
'system_prompt': system_prompt,
|
2025-08-10 23:42:47 +08:00
|
|
|
'model': model,
|
2025-08-17 08:44:57 +08:00
|
|
|
'agentpress_tools': _extract_agentpress_tools_for_run(agentpress_tools),
|
2025-07-24 02:45:53 +08:00
|
|
|
'configured_mcps': configured_mcps,
|
|
|
|
'custom_mcps': custom_mcps,
|
2025-08-15 03:56:31 +08:00
|
|
|
'workflows': workflows,
|
2025-08-16 19:26:13 +08:00
|
|
|
'triggers': triggers,
|
2025-07-14 01:56:24 +08:00
|
|
|
'avatar': agent_data.get('avatar'),
|
|
|
|
'avatar_color': agent_data.get('avatar_color'),
|
2025-08-11 22:00:57 +08:00
|
|
|
'profile_image_url': agent_data.get('profile_image_url'),
|
2025-08-25 01:04:32 +08:00
|
|
|
'icon_name': agent_data.get('icon_name'),
|
|
|
|
'icon_color': agent_data.get('icon_color'),
|
|
|
|
'icon_background': agent_data.get('icon_background'),
|
2025-07-24 02:45:53 +08:00
|
|
|
'is_default': agent_data.get('is_default', False),
|
2025-08-17 08:44:57 +08:00
|
|
|
'is_suna_default': False,
|
|
|
|
'centrally_managed': False,
|
2025-07-24 02:45:53 +08:00
|
|
|
'account_id': agent_data.get('account_id'),
|
|
|
|
'current_version_id': agent_data.get('current_version_id'),
|
2025-08-17 08:44:57 +08:00
|
|
|
'version_name': version_data.get('version_name', 'v1'),
|
|
|
|
'restrictions': {}
|
|
|
|
}
|
2025-08-25 03:17:41 +08:00
|
|
|
|
|
|
|
# Debug logging for returned config
|
|
|
|
if os.getenv("ENV_MODE", "").upper() == "STAGING":
|
|
|
|
print(f"[DEBUG] _extract_custom_agent_config: Returning config with icon_name={config.get('icon_name')}, icon_color={config.get('icon_color')}, icon_background={config.get('icon_background')}")
|
|
|
|
|
|
|
|
return config
|
2025-07-08 12:51:17 +08:00
|
|
|
|
2025-08-17 08:44:57 +08:00
|
|
|
logger.warning(f"No version data found for custom agent {agent_id}, creating default configuration")
|
2025-07-30 02:11:22 +08:00
|
|
|
|
2025-08-25 03:17:41 +08:00
|
|
|
fallback_config = {
|
2025-07-30 02:11:22 +08:00
|
|
|
'agent_id': agent_data['agent_id'],
|
|
|
|
'name': agent_data.get('name', 'Unnamed Agent'),
|
|
|
|
'description': agent_data.get('description', ''),
|
|
|
|
'system_prompt': 'You are a helpful AI assistant.',
|
2025-08-17 08:44:57 +08:00
|
|
|
'model': None,
|
|
|
|
'agentpress_tools': _extract_agentpress_tools_for_run(_get_default_agentpress_tools()),
|
2025-07-30 02:11:22 +08:00
|
|
|
'configured_mcps': [],
|
|
|
|
'custom_mcps': [],
|
2025-08-15 03:56:31 +08:00
|
|
|
'workflows': [],
|
2025-08-16 19:26:13 +08:00
|
|
|
'triggers': [],
|
2025-07-30 02:11:22 +08:00
|
|
|
'avatar': agent_data.get('avatar'),
|
|
|
|
'avatar_color': agent_data.get('avatar_color'),
|
2025-08-11 22:00:57 +08:00
|
|
|
'profile_image_url': agent_data.get('profile_image_url'),
|
2025-08-25 01:04:32 +08:00
|
|
|
'icon_name': agent_data.get('icon_name'),
|
|
|
|
'icon_color': agent_data.get('icon_color'),
|
|
|
|
'icon_background': agent_data.get('icon_background'),
|
2025-08-17 08:44:57 +08:00
|
|
|
'is_default': agent_data.get('is_default', False),
|
|
|
|
'is_suna_default': False,
|
|
|
|
'centrally_managed': False,
|
|
|
|
'account_id': agent_data.get('account_id'),
|
|
|
|
'current_version_id': agent_data.get('current_version_id'),
|
|
|
|
'version_name': 'v1',
|
|
|
|
'restrictions': {}
|
2025-07-30 02:11:22 +08:00
|
|
|
}
|
2025-08-25 03:17:41 +08:00
|
|
|
|
|
|
|
# Debug logging for fallback config
|
|
|
|
if os.getenv("ENV_MODE", "").upper() == "STAGING":
|
|
|
|
print(f"[DEBUG] _extract_custom_agent_config: Fallback config with icon_name={fallback_config.get('icon_name')}, icon_color={fallback_config.get('icon_color')}, icon_background={fallback_config.get('icon_background')}")
|
|
|
|
|
|
|
|
return fallback_config
|
2025-07-08 12:51:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
def build_unified_config(
|
|
|
|
system_prompt: str,
|
|
|
|
agentpress_tools: Dict[str, Any],
|
|
|
|
configured_mcps: List[Dict[str, Any]],
|
|
|
|
custom_mcps: Optional[List[Dict[str, Any]]] = None,
|
|
|
|
avatar: Optional[str] = None,
|
2025-07-23 00:11:10 +08:00
|
|
|
avatar_color: Optional[str] = None,
|
2025-08-15 03:56:31 +08:00
|
|
|
suna_metadata: Optional[Dict[str, Any]] = None,
|
2025-08-16 19:26:13 +08:00
|
|
|
workflows: Optional[List[Dict[str, Any]]] = None,
|
|
|
|
triggers: Optional[List[Dict[str, Any]]] = None
|
2025-07-08 12:51:17 +08:00
|
|
|
) -> Dict[str, Any]:
|
|
|
|
simplified_tools = {}
|
|
|
|
for tool_name, tool_config in agentpress_tools.items():
|
|
|
|
if isinstance(tool_config, dict):
|
|
|
|
simplified_tools[tool_name] = tool_config.get('enabled', False)
|
|
|
|
elif isinstance(tool_config, bool):
|
|
|
|
simplified_tools[tool_name] = tool_config
|
2025-07-23 00:11:10 +08:00
|
|
|
|
|
|
|
config = {
|
2025-07-08 12:51:17 +08:00
|
|
|
'system_prompt': system_prompt,
|
|
|
|
'tools': {
|
|
|
|
'agentpress': simplified_tools,
|
|
|
|
'mcp': configured_mcps or [],
|
|
|
|
'custom_mcp': custom_mcps or []
|
|
|
|
},
|
2025-08-15 03:56:31 +08:00
|
|
|
'workflows': workflows or [],
|
2025-08-16 19:26:13 +08:00
|
|
|
'triggers': triggers or [],
|
2025-07-08 12:51:17 +08:00
|
|
|
'metadata': {
|
|
|
|
'avatar': avatar,
|
|
|
|
'avatar_color': avatar_color
|
|
|
|
}
|
|
|
|
}
|
2025-07-23 00:11:10 +08:00
|
|
|
|
|
|
|
if suna_metadata:
|
|
|
|
config['suna_metadata'] = suna_metadata
|
|
|
|
|
|
|
|
return config
|
2025-07-08 12:51:17 +08:00
|
|
|
|
|
|
|
|
2025-08-17 08:44:57 +08:00
|
|
|
def _get_default_agentpress_tools() -> Dict[str, bool]:
|
|
|
|
"""Get default AgentPress tools configuration for new custom agents."""
|
|
|
|
return {
|
|
|
|
"sb_shell_tool": True,
|
|
|
|
"sb_files_tool": True,
|
|
|
|
"sb_deploy_tool": True,
|
|
|
|
"sb_expose_tool": True,
|
|
|
|
"web_search_tool": True,
|
|
|
|
"sb_vision_tool": True,
|
|
|
|
"sb_image_edit_tool": True,
|
|
|
|
"sb_presentation_outline_tool": True,
|
|
|
|
"sb_presentation_tool": True,
|
2025-08-21 14:06:01 +08:00
|
|
|
|
2025-08-17 08:44:57 +08:00
|
|
|
"sb_sheets_tool": True,
|
|
|
|
"sb_web_dev_tool": True,
|
|
|
|
"browser_tool": True,
|
|
|
|
"data_providers_tool": True,
|
|
|
|
"agent_config_tool": True,
|
|
|
|
"mcp_search_tool": True,
|
|
|
|
"credential_profile_tool": True,
|
|
|
|
"workflow_tool": True,
|
|
|
|
"trigger_tool": True
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-07-24 02:45:53 +08:00
|
|
|
def _extract_agentpress_tools_for_run(agentpress_config: Dict[str, Any]) -> Dict[str, Any]:
|
2025-08-17 08:44:57 +08:00
|
|
|
"""Convert agentpress tools config to runtime format."""
|
2025-07-24 02:45:53 +08:00
|
|
|
if not agentpress_config:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
run_tools = {}
|
|
|
|
for tool_name, enabled in agentpress_config.items():
|
2025-07-08 12:51:17 +08:00
|
|
|
if isinstance(enabled, bool):
|
2025-07-24 02:45:53 +08:00
|
|
|
run_tools[tool_name] = {
|
2025-07-08 12:51:17 +08:00
|
|
|
'enabled': enabled,
|
2025-07-24 02:45:53 +08:00
|
|
|
'description': f"{tool_name} tool"
|
2025-07-08 12:51:17 +08:00
|
|
|
}
|
|
|
|
elif isinstance(enabled, dict):
|
2025-07-24 02:45:53 +08:00
|
|
|
run_tools[tool_name] = enabled
|
|
|
|
else:
|
|
|
|
run_tools[tool_name] = {
|
|
|
|
'enabled': bool(enabled),
|
|
|
|
'description': f"{tool_name} tool"
|
|
|
|
}
|
2025-07-08 12:51:17 +08:00
|
|
|
|
2025-07-24 02:45:53 +08:00
|
|
|
return run_tools
|
|
|
|
|
|
|
|
|