Update dynamic_conversational_swarm.py

pull/1034/head
CI-DEV 2 months ago committed by GitHub
parent 0023904247
commit b5728cc05d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,13 +1,14 @@
import json import json
import random import random
from swarms.structs.agent import Agent from swarms.structs.agent import Agent
from typing import List from typing import List, Optional
from swarms.structs.conversation import Conversation from swarms.structs.conversation import Conversation
from swarms.structs.ma_blocks import find_agent_by_name from swarms.structs.ma_blocks import find_agent_by_name
from swarms.utils.history_output_formatter import ( from swarms.utils.history_output_formatter import (
history_output_formatter, history_output_formatter,
) )
from swarms.utils.any_to_str import any_to_str from swarms.utils.any_to_str import any_to_str
from swarms.security import SwarmShieldIntegration, ShieldConfig
tools = [ tools = [
{ {
@ -55,6 +56,9 @@ class DynamicConversationalSwarm:
agents: List[Agent] = [], agents: List[Agent] = [],
max_loops: int = 1, max_loops: int = 1,
output_type: str = "list", output_type: str = "list",
shield_config: Optional[ShieldConfig] = None,
enable_security: bool = True,
security_level: str = "standard",
*args, *args,
**kwargs, **kwargs,
): ):
@ -64,6 +68,9 @@ class DynamicConversationalSwarm:
self.max_loops = max_loops self.max_loops = max_loops
self.output_type = output_type self.output_type = output_type
# Initialize SwarmShield integration
self._initialize_swarm_shield(shield_config, enable_security, security_level)
self.conversation = Conversation() self.conversation = Conversation()
# Agents in the chat # Agents in the chat
@ -74,6 +81,86 @@ class DynamicConversationalSwarm:
self.inject_tools() self.inject_tools()
def _initialize_swarm_shield(
self,
shield_config: Optional[ShieldConfig] = None,
enable_security: bool = True,
security_level: str = "standard"
) -> None:
"""Initialize SwarmShield integration for security features."""
self.enable_security = enable_security
self.security_level = security_level
if enable_security:
if shield_config is None:
shield_config = ShieldConfig.get_security_level(security_level)
self.swarm_shield = SwarmShieldIntegration(shield_config)
else:
self.swarm_shield = None
# Security methods
def validate_task_with_shield(self, task: str) -> str:
"""Validate and sanitize task input using SwarmShield."""
if self.swarm_shield:
return self.swarm_shield.validate_and_protect_input(task)
return task
def validate_agent_config_with_shield(self, agent_config: dict) -> dict:
"""Validate agent configuration using SwarmShield."""
if self.swarm_shield:
return self.swarm_shield.validate_and_protect_input(str(agent_config))
return agent_config
def process_agent_communication_with_shield(self, message: str, agent_name: str) -> str:
"""Process agent communication through SwarmShield security."""
if self.swarm_shield:
return self.swarm_shield.process_agent_communication(message, agent_name)
return message
def check_rate_limit_with_shield(self, agent_name: str) -> bool:
"""Check rate limits for an agent using SwarmShield."""
if self.swarm_shield:
return self.swarm_shield.check_rate_limit(agent_name)
return True
def add_secure_message(self, message: str, agent_name: str) -> None:
"""Add a message to secure conversation history."""
if self.swarm_shield:
self.swarm_shield.add_secure_message(message, agent_name)
def get_secure_messages(self) -> List[dict]:
"""Get secure conversation messages."""
if self.swarm_shield:
return self.swarm_shield.get_secure_messages()
return []
def get_security_stats(self) -> dict:
"""Get security statistics and metrics."""
if self.swarm_shield:
return self.swarm_shield.get_security_stats()
return {"security_enabled": False}
def update_shield_config(self, new_config: ShieldConfig) -> None:
"""Update SwarmShield configuration."""
if self.swarm_shield:
self.swarm_shield.update_config(new_config)
def enable_security(self) -> None:
"""Enable SwarmShield security features."""
if not self.swarm_shield:
self._initialize_swarm_shield(enable_security=True, security_level=self.security_level)
def disable_security(self) -> None:
"""Disable SwarmShield security features."""
self.swarm_shield = None
self.enable_security = False
def cleanup_security(self) -> None:
"""Clean up SwarmShield resources."""
if self.swarm_shield:
self.swarm_shield.cleanup()
# Inject tools into the agents # Inject tools into the agents
def inject_tools(self): def inject_tools(self):
for agent in self.agents: for agent in self.agents:

Loading…
Cancel
Save