File size: 9,409 Bytes
a9dc537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""
Base Agent for SPARKNET
Defines the core agent interface and functionality
"""

from abc import ABC, abstractmethod
from typing import List, Dict, Optional, Any
from dataclasses import dataclass
from datetime import datetime
from loguru import logger
import json

from ..llm.ollama_client import OllamaClient
from ..tools.base_tool import BaseTool, ToolRegistry, ToolResult


@dataclass
class Message:
    """Message for agent communication."""
    role: str  # 'system', 'user', 'assistant', 'agent'
    content: str
    sender: Optional[str] = None
    timestamp: Optional[datetime] = None
    metadata: Optional[Dict[str, Any]] = None

    def __post_init__(self):
        if self.timestamp is None:
            self.timestamp = datetime.now()

    def to_dict(self) -> Dict[str, str]:
        """Convert to dictionary for Ollama API."""
        return {
            "role": "user" if self.role == "agent" else self.role,
            "content": self.content,
        }


@dataclass
class Task:
    """Task for agent execution."""
    id: str
    description: str
    priority: int = 0
    status: str = "pending"  # pending, in_progress, completed, failed
    result: Optional[Any] = None
    error: Optional[str] = None
    metadata: Optional[Dict[str, Any]] = None

    def __post_init__(self):
        if self.metadata is None:
            self.metadata = {}


class BaseAgent(ABC):
    """Base class for all SPARKNET agents."""

    def __init__(
        self,
        name: str,
        description: str,
        llm_client: OllamaClient,
        model: str,
        system_prompt: str,
        tools: Optional[List[BaseTool]] = None,
        temperature: float = 0.7,
        max_tokens: Optional[int] = None,
    ):
        """
        Initialize agent.

        Args:
            name: Agent name
            description: Agent description
            llm_client: Ollama client instance
            model: Model to use
            system_prompt: System prompt for the agent
            tools: List of available tools
            temperature: LLM temperature
            max_tokens: Max tokens to generate
        """
        self.name = name
        self.description = description
        self.llm_client = llm_client
        self.model = model
        self.system_prompt = system_prompt
        self.tools = {tool.name: tool for tool in (tools or [])}
        self.temperature = temperature
        self.max_tokens = max_tokens

        # Message history
        self.messages: List[Message] = []

        # Tool registry
        self.tool_registry: Optional[ToolRegistry] = None

        logger.info(f"Initialized agent: {self.name} with model {self.model}")

    def add_tool(self, tool: BaseTool):
        """
        Add a tool to the agent's toolbox.

        Args:
            tool: Tool to add
        """
        self.tools[tool.name] = tool
        logger.info(f"Agent {self.name} added tool: {tool.name}")

    def remove_tool(self, tool_name: str):
        """
        Remove a tool from the agent's toolbox.

        Args:
            tool_name: Name of tool to remove
        """
        if tool_name in self.tools:
            del self.tools[tool_name]
            logger.info(f"Agent {self.name} removed tool: {tool_name}")

    def set_tool_registry(self, registry: ToolRegistry):
        """
        Set the tool registry for accessing shared tools.

        Args:
            registry: Tool registry instance
        """
        self.tool_registry = registry

    async def call_llm(
        self,
        prompt: Optional[str] = None,
        messages: Optional[List[Message]] = None,
        temperature: Optional[float] = None,
    ) -> str:
        """
        Call the LLM with a prompt or messages.

        Args:
            prompt: Single prompt string
            messages: List of messages
            temperature: Override temperature

        Returns:
            LLM response
        """
        temp = temperature if temperature is not None else self.temperature

        if prompt:
            # Single prompt
            response = self.llm_client.generate(
                prompt=prompt,
                model=self.model,
                system=self.system_prompt,
                temperature=temp,
                max_tokens=self.max_tokens,
            )
        elif messages:
            # Chat with messages
            # Add system message
            chat_messages = [
                {"role": "system", "content": self.system_prompt}
            ]
            # Add conversation messages
            chat_messages.extend([msg.to_dict() for msg in messages])

            response = self.llm_client.chat(
                messages=chat_messages,
                model=self.model,
                temperature=temp,
            )
        else:
            raise ValueError("Either prompt or messages must be provided")

        logger.debug(f"Agent {self.name} received LLM response: {len(response)} chars")
        return response

    async def execute_tool(self, tool_name: str, **kwargs) -> ToolResult:
        """
        Execute a tool by name.

        Args:
            tool_name: Name of tool to execute
            **kwargs: Tool parameters

        Returns:
            ToolResult from tool execution
        """
        # Try agent's tools first
        tool = self.tools.get(tool_name)

        # If not found, try tool registry
        if tool is None and self.tool_registry:
            tool = self.tool_registry.get_tool(tool_name)

        if tool is None:
            logger.error(f"Tool not found: {tool_name}")
            return ToolResult(
                success=False,
                output=None,
                error=f"Tool not found: {tool_name}",
            )

        logger.info(f"Agent {self.name} executing tool: {tool_name}")
        result = await tool.safe_execute(**kwargs)

        return result

    def add_message(self, message: Message):
        """
        Add a message to the agent's history.

        Args:
            message: Message to add
        """
        self.messages.append(message)

    async def receive_message(self, message: Message) -> Optional[str]:
        """
        Receive and process a message from another agent or user.

        Args:
            message: Incoming message

        Returns:
            Response or None
        """
        logger.info(f"Agent {self.name} received message from {message.sender}")
        self.add_message(message)

        # Process message (can be overridden by subclasses)
        return await self.process_message(message)

    async def process_message(self, message: Message) -> Optional[str]:
        """
        Process an incoming message. Can be overridden by subclasses.

        Args:
            message: Message to process

        Returns:
            Response or None
        """
        # Default: generate a response using LLM
        response = await self.call_llm(messages=self.messages)

        # Add response to history
        self.add_message(
            Message(
                role="assistant",
                content=response,
                sender=self.name,
            )
        )

        return response

    @abstractmethod
    async def process_task(self, task: Task) -> Task:
        """
        Process a task. Must be implemented by subclasses.

        Args:
            task: Task to process

        Returns:
            Updated task with results
        """
        pass

    async def send_message(self, recipient: "BaseAgent", content: str) -> Optional[str]:
        """
        Send a message to another agent.

        Args:
            recipient: Recipient agent
            content: Message content

        Returns:
            Response from recipient
        """
        message = Message(
            role="agent",
            content=content,
            sender=self.name,
        )

        logger.info(f"Agent {self.name} sending message to {recipient.name}")
        response = await recipient.receive_message(message)

        return response

    def get_available_tools(self) -> List[str]:
        """
        Get list of available tool names.

        Returns:
            List of tool names
        """
        tool_names = list(self.tools.keys())

        if self.tool_registry:
            tool_names.extend(self.tool_registry.list_tools())

        return list(set(tool_names))  # Remove duplicates

    def get_tool_schemas(self) -> List[Dict[str, Any]]:
        """
        Get schemas for all available tools.

        Returns:
            List of tool schemas
        """
        schemas = [tool.get_schema() for tool in self.tools.values()]

        if self.tool_registry:
            schemas.extend(self.tool_registry.get_schemas())

        return schemas

    def clear_history(self):
        """Clear message history."""
        self.messages.clear()
        logger.info(f"Agent {self.name} cleared message history")

    def get_stats(self) -> Dict[str, Any]:
        """
        Get agent statistics.

        Returns:
            Dictionary with agent stats
        """
        return {
            "name": self.name,
            "model": self.model,
            "messages_count": len(self.messages),
            "tools_count": len(self.tools),
        }

    def __repr__(self) -> str:
        return f"<Agent: {self.name} (model={self.model}, tools={len(self.tools)})>"