|
| 1 | +""" |
| 2 | +Agent import system for the AgentFarm DB to Memory System converter. |
| 3 | +""" |
| 4 | + |
| 5 | +import logging |
| 6 | +from dataclasses import dataclass |
| 7 | +from typing import Any, Dict, List, Optional |
| 8 | + |
| 9 | +from .config import ConverterConfig |
| 10 | +from .db import DatabaseManager |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class AgentMetadata: |
| 17 | + """Metadata for an imported agent.""" |
| 18 | + |
| 19 | + agent_id: str |
| 20 | + name: str |
| 21 | + metadata: Dict[str, Any] |
| 22 | + created_at: str |
| 23 | + updated_at: str |
| 24 | + |
| 25 | + |
| 26 | +class AgentImporter: |
| 27 | + """ |
| 28 | + Handles the import of agents from AgentFarm database to memory system. |
| 29 | +
|
| 30 | + This class manages the process of importing agents, including validation, |
| 31 | + metadata preservation, and error handling. |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self, db_manager: DatabaseManager, config: ConverterConfig): |
| 35 | + """ |
| 36 | + Initialize the agent importer. |
| 37 | +
|
| 38 | + Args: |
| 39 | + db_manager: Database manager instance |
| 40 | + config: Converter configuration |
| 41 | + """ |
| 42 | + self.db_manager = db_manager |
| 43 | + self.config = config |
| 44 | + |
| 45 | + def import_agents(self) -> List[AgentMetadata]: |
| 46 | + """ |
| 47 | + Import agents from the database. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + List of imported agent metadata |
| 51 | +
|
| 52 | + Raises: |
| 53 | + ValueError: If agent validation fails and error_handling is 'fail' |
| 54 | + """ |
| 55 | + agents = [] |
| 56 | + with self.db_manager.session() as session: |
| 57 | + # Get agent query based on import mode |
| 58 | + query = self._get_agent_query(session) |
| 59 | + |
| 60 | + # Process agents in batches |
| 61 | + for batch in self._batch_query(query): |
| 62 | + for agent in batch: |
| 63 | + try: |
| 64 | + agent_metadata = self._import_agent(agent) |
| 65 | + agents.append(agent_metadata) |
| 66 | + except Exception as e: |
| 67 | + self._handle_import_error(e, agent) |
| 68 | + |
| 69 | + return agents |
| 70 | + |
| 71 | + def _get_agent_query(self, session): |
| 72 | + """Get the appropriate agent query based on import mode.""" |
| 73 | + query = session.query(self.db_manager.AgentModel) |
| 74 | + |
| 75 | + if self.config.import_mode == "incremental": |
| 76 | + # Add incremental import conditions |
| 77 | + pass |
| 78 | + |
| 79 | + if self.config.selective_agents: |
| 80 | + query = query.filter( |
| 81 | + self.db_manager.AgentModel.agent_id.in_(self.config.selective_agents) |
| 82 | + ) |
| 83 | + |
| 84 | + return query |
| 85 | + |
| 86 | + def _batch_query(self, query): |
| 87 | + """Process query in batches.""" |
| 88 | + offset = 0 |
| 89 | + while True: |
| 90 | + batch = query.offset(offset).limit(self.config.batch_size).all() |
| 91 | + if not batch: |
| 92 | + break |
| 93 | + yield batch |
| 94 | + offset += self.config.batch_size |
| 95 | + |
| 96 | + def _import_agent(self, agent) -> AgentMetadata: |
| 97 | + """ |
| 98 | + Import a single agent. |
| 99 | +
|
| 100 | + Args: |
| 101 | + agent: Agent model instance |
| 102 | +
|
| 103 | + Returns: |
| 104 | + AgentMetadata instance |
| 105 | +
|
| 106 | + Raises: |
| 107 | + ValueError: If agent validation fails |
| 108 | + """ |
| 109 | + # Validate agent |
| 110 | + if self.config.validate: |
| 111 | + self._validate_agent(agent) |
| 112 | + |
| 113 | + # Create agent metadata |
| 114 | + metadata = AgentMetadata( |
| 115 | + agent_id=agent.agent_id, |
| 116 | + # Use agent_id as the name if agent doesn't have a name attribute |
| 117 | + name=getattr(agent, "name", f"Agent-{agent.agent_id}"), |
| 118 | + metadata=self._extract_agent_metadata(agent), |
| 119 | + created_at=str(agent.birth_time), |
| 120 | + updated_at=str(agent.death_time or agent.birth_time), |
| 121 | + ) |
| 122 | + |
| 123 | + return metadata |
| 124 | + |
| 125 | + def _validate_agent(self, agent): |
| 126 | + """ |
| 127 | + Validate an agent. |
| 128 | +
|
| 129 | + Args: |
| 130 | + agent: Agent model instance |
| 131 | +
|
| 132 | + Raises: |
| 133 | + ValueError: If validation fails |
| 134 | + """ |
| 135 | + if not agent.agent_id: |
| 136 | + raise ValueError("Agent must have an ID") |
| 137 | + |
| 138 | + def _extract_agent_metadata(self, agent) -> Dict[str, Any]: |
| 139 | + """ |
| 140 | + Extract metadata from an agent. |
| 141 | +
|
| 142 | + Args: |
| 143 | + agent: Agent model instance |
| 144 | +
|
| 145 | + Returns: |
| 146 | + Dictionary of agent metadata |
| 147 | + """ |
| 148 | + return { |
| 149 | + "type": agent.agent_type, |
| 150 | + "position": {"x": agent.position_x, "y": agent.position_y}, |
| 151 | + "initial_resources": agent.initial_resources, |
| 152 | + "starting_health": agent.starting_health, |
| 153 | + "starvation_threshold": agent.starvation_threshold, |
| 154 | + "genome_id": agent.genome_id, |
| 155 | + "generation": agent.generation, |
| 156 | + "action_weights": agent.action_weights, |
| 157 | + } |
| 158 | + |
| 159 | + def _handle_import_error(self, error: Exception, agent: Any): |
| 160 | + """ |
| 161 | + Handle agent import error based on configuration. |
| 162 | +
|
| 163 | + Args: |
| 164 | + error: The error that occurred |
| 165 | + agent: The agent that caused the error |
| 166 | + """ |
| 167 | + error_msg = f"Error importing agent {agent.agent_id}: {str(error)}" |
| 168 | + |
| 169 | + if self.config.error_handling == "fail": |
| 170 | + raise ValueError(error_msg) |
| 171 | + elif self.config.error_handling == "log": |
| 172 | + logger.error(error_msg) |
| 173 | + # Skip mode just continues without raising or logging |
0 commit comments