from abc import ABC import xml.etree.ElementTree as ET from .llm_engine import LlmEngine from .response_parser import ResponseParser from .system_metrics import SystemMetrics from .working_memory import WorkingMemory class BaseAgent(ABC): """ Abstract base class for SIA agents. Provides core functionality for maintaining working memory, system metrics, and coordinating components for LLM inference. """ def __init__( self, system_prompt: str, action_schema: str, working_memory: WorkingMemory, metrics: SystemMetrics, parser: ResponseParser, ): """ Initialize agent with required components. """ self._system_prompt = system_prompt self._action_schema = action_schema self._working_memory = working_memory self._metrics = metrics self._parser = parser @property def system_prompt(self) -> str: """Get the system prompt.""" return f"{self._system_prompt}\n{self._action_schema}" @property def working_memory(self) -> WorkingMemory: """Get the working memory.""" return self._working_memory def _compile_context(self, llmEngine: LlmEngine) -> ET: """ Compile the current context for LLM inference. Includes system metrics and working memory entries. Returns: str: Complete context as XML string """ memory_context = self._working_memory.generate_context() metrics_data = self._metrics.get_metrics() # Create context element context = ET.Element("context") context.set("time", metrics_data["timestamp"]) context.set("memory_used", str(metrics_data["memory_used"])) context.set("memory_total", str(metrics_data["memory_total"])) context.set("disk_used", str(metrics_data["disk_used"])) context.set("disk_total", str(metrics_data["disk_total"])) context.set("stdin", str(self._parser.io_buffer.buffer_length())) context.set("context", "100%") for entry in memory_context: context.append(entry) # Calculate token usage percentage token_count = llmEngine.token_count(self.system_prompt, context) token_limit = llmEngine.token_limit() context_usage = (float(token_count) / float(token_limit)) * 100.0 # Update context usage metric context.set("context", f"{str(round(context_usage, 2))}%") return context