File size: 13,438 Bytes
3193174
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any

import torch
from pydantic import BaseModel, ConfigDict, Field

if TYPE_CHECKING:
    from tools.base import BaseTool

__all__ = ["AgentLLMConfig", "AgentProfile", "TaskNode"]


class AgentLLMConfig(BaseModel):
    """
    LLM configuration for AgentProfile.

    Allows setting individual LLM settings for an agent:
    - model_name: model name (gpt-4, claude-3-opus, llama3:70b)
    - base_url: API endpoint URL
    - api_key: API key (or reference to an environment variable $VAR)
    """

    model_config = ConfigDict(extra="allow")

    model_name: str | None = None
    base_url: str | None = None
    api_key: str | None = None
    max_tokens: int | None = None
    temperature: float | None = None
    timeout: float | None = None
    top_p: float | None = None
    stop_sequences: list[str] | None = None
    extra_params: dict[str, Any] = Field(default_factory=dict)

    def resolve_api_key(self) -> str | None:
        """Resolve the API key from an environment variable."""
        import os

        if self.api_key and self.api_key.startswith("$"):
            return os.environ.get(self.api_key[1:])
        return self.api_key

    def is_configured(self) -> bool:
        """Check whether the configuration is set."""
        return bool(self.model_name or self.base_url)

    def to_generation_params(self) -> dict[str, Any]:
        """Collect generation parameters for the LLM."""
        params = {}
        if self.max_tokens is not None:
            params["max_tokens"] = self.max_tokens
        if self.temperature is not None:
            params["temperature"] = self.temperature
        if self.top_p is not None:
            params["top_p"] = self.top_p
        if self.stop_sequences:
            params["stop"] = self.stop_sequences
        params.update(self.extra_params)
        return params


class AgentProfile(BaseModel):
    """
    Agent profile with description, tools, and LLM configuration.

    If an agent has tools, they are ALWAYS used on every LLM call.
    Tools can be specified as strings (names from the registry) or as BaseTool objects.

    Example:
        from core.agent import AgentProfile
        from tools import CodeInterpreterTool, tool

        # Register a custom tool
        @tool
        def fibonacci(n: int) -> str:
            '''Calculate n-th Fibonacci number.'''
            a, b = 0, 1
            for _ in range(n):
                a, b = b, a + b
            return str(a)

        # Create an agent with tools
        agent = AgentProfile(
            agent_id="math",
            display_name="Math Agent",
            persona="a helpful math assistant",
            tools=["fibonacci", "code_interpreter"],
        )

        # Or pass objects directly
        agent = AgentProfile(
            agent_id="coder",
            display_name="Coder",
            persona="a Python programmer",
            tools=[CodeInterpreterTool()],
        )

    """

    model_config = ConfigDict(frozen=True, arbitrary_types_allowed=True)

    agent_id: str
    display_name: str
    persona: str = ""
    description: str = ""

    # LLM Configuration
    llm_backbone: str | None = None
    llm_config: AgentLLMConfig | None = Field(default=None, repr=False)

    # Tools β€” list of names (str) or objects (BaseTool)
    # When tools are used they are ALWAYS called via native function calling
    tools: list[Any] = Field(default_factory=list)

    raw: Mapping[str, Any] = Field(default_factory=dict)
    embedding: torch.Tensor | None = Field(default=None, repr=False)
    state: list[dict[str, Any]] = Field(default_factory=list)
    hidden_state: torch.Tensor | None = Field(default=None, repr=False)

    # Input/Output Schema for validation
    input_schema: Any | None = Field(default=None, repr=False)
    output_schema: Any | None = Field(default=None, repr=False)

    def get_tool_names(self) -> list[str]:
        """
        Get the agent's tool names.

        Returns:
            List of tool names (for strings β€” as-is, for dicts β€” from "name",
            for BaseTool β€” tool.name).

        """
        from tools.base import BaseTool

        names = []
        for t in self.tools:
            if isinstance(t, str):
                names.append(t)
            elif isinstance(t, dict):
                name = t.get("name") or t.get("tool") or t.get("id")
                if isinstance(name, str):
                    names.append(name)
            elif isinstance(t, BaseTool):
                names.append(t.name)
        return names

    def get_tool_objects(self) -> list["BaseTool"]:
        """
        Get the agent's tool objects.

        Supports three formats:
        - str: looks up by name in the global registry
        - dict: creates a tool from config via factory
          (e.g. ``{"name": "web_search", "use_selenium": True}``)
        - BaseTool: returned as-is

        Example:
            agent = AgentProfile(
                agent_id="browser",
                display_name="Browser Agent",
                tools=[
                    "shell",                                    # by name from registry
                    {"name": "web_search", "use_selenium": True},  # dict config
                    WebSearchTool(use_selenium=True),           # object directly
                ],
            )

        """
        from tools.base import BaseTool, create_tool_from_config, get_registry

        registry = get_registry()
        tools: list[BaseTool] = []
        for t in self.tools:
            if isinstance(t, str):
                tool_obj = registry.get(t)
                if tool_obj:
                    tools.append(tool_obj)
            elif isinstance(t, dict):
                tool_obj = create_tool_from_config(t)
                if tool_obj:
                    tools.append(tool_obj)
            elif isinstance(t, BaseTool):
                tools.append(t)
        return tools

    def has_tools(self) -> bool:
        """Check whether the agent has tools."""
        return len(self.tools) > 0

    def to_text(self) -> str:
        """Serialize the profile to text for the encoder."""
        parts = [self.display_name or self.agent_id]
        if self.persona and self.persona != self.description:
            parts.append(self.persona)
        if self.description:
            parts.append(self.description)
        if self.tools:
            tool_names = self.get_tool_names()
            parts.append("Tools: " + ", ".join(tool_names))
        model_name = self.get_model_name()
        if model_name:
            parts.append(f"LLM Backbone: {model_name}")
        return "\n".join(p.strip() for p in parts if p.strip())

    def get_model_name(self) -> str | None:
        """Get the model name (from llm_config or llm_backbone)."""
        if self.llm_config and self.llm_config.model_name:
            return self.llm_config.model_name
        return self.llm_backbone

    def get_llm_config(self) -> AgentLLMConfig:
        """Get the effective LLM configuration for the agent."""
        if self.llm_config:
            return self.llm_config
        return AgentLLMConfig(model_name=self.llm_backbone)

    def has_custom_llm(self) -> bool:
        """Check whether a custom LLM configuration is set."""
        return self.llm_config is not None and self.llm_config.is_configured()

    def with_llm_config(self, llm_config: AgentLLMConfig) -> "AgentProfile":
        """Return a copy of the profile with the given LLM configuration."""
        return self.model_copy(update={"llm_config": llm_config})

    def with_embedding(self, embedding: torch.Tensor) -> "AgentProfile":
        """Return a copy of the profile with an updated embedding."""
        return self.model_copy(update={"embedding": embedding})

    def with_state(self, state: list[dict[str, Any]]) -> "AgentProfile":
        """Return a copy of the profile with a new state."""
        return self.model_copy(update={"state": state})

    def append_state(self, message: dict[str, Any]) -> "AgentProfile":
        """Return a copy with the given message appended to the state history."""
        new_state = [*list(self.state), message]
        return self.model_copy(update={"state": new_state})

    def with_hidden_state(self, hidden_state: torch.Tensor) -> "AgentProfile":
        """Return a copy of the profile with an updated hidden state."""
        return self.model_copy(update={"hidden_state": hidden_state})

    def clear_state(self) -> "AgentProfile":
        """Return a copy with cleared local state."""
        return self.model_copy(update={"state": []})

    @property
    def role(self) -> str:
        """Alias for the agent role identifier."""
        return self.agent_id

    def to_dict(self) -> dict[str, Any]:
        """Convert the profile to a serializable dict."""
        result = {
            "agent_id": self.agent_id,
            "display_name": self.display_name,
            "persona": self.persona,
            "description": self.description,
            "llm_backbone": self.llm_backbone,
            "tools": self.get_tool_names(),
            "state": list(self.state),
            "embedding": self.embedding.cpu().tolist() if self.embedding is not None else None,
        }
        if self.llm_config:
            result["llm_config"] = self.llm_config.model_dump()
        if self.input_schema:
            result["input_schema"] = self.input_schema
        if self.output_schema:
            result["output_schema"] = self.output_schema
        return result


class TaskNode(BaseModel):
    """Virtual task node that connects all agents."""

    model_config = ConfigDict(frozen=True, arbitrary_types_allowed=True)

    agent_id: str = Field(default="__task__", alias="id")
    type: str = Field(default="task")
    query: str
    description: str = Field(default="Virtual task node that encodes the problem statement and connects to all agents.")
    embedding: torch.Tensor | None = Field(default=None, repr=False)

    display_name: str = Field(default="Task")
    persona: str = Field(default="")
    llm_backbone: str | None = Field(default=None)
    tools: list[str] = Field(default_factory=list)
    state: list[dict[str, Any]] = Field(default_factory=list)

    def with_embedding(self, embedding: torch.Tensor) -> "TaskNode":
        """Return a copy of the task node with the given embedding."""
        return self.model_copy(update={"embedding": embedding})

    def to_text(self) -> str:
        """Serialize the task to a text description."""
        parts = []
        if self.description:
            parts.append(self.description.strip())
        query_text = self.query.strip() if self.query.strip() else "(unspecified)"
        parts.append(f"Task: {query_text}")
        return "\n".join(p for p in parts if p)


def extract_agent_profiles(agents_data: Mapping[str, Any]) -> list[AgentProfile]:
    """Collect unique `AgentProfile` instances from a dict containing a list of agents."""
    seen: dict[str, AgentProfile] = {}

    entries = agents_data.get("agents", []) if isinstance(agents_data, dict) else []

    for entry in entries:
        agent_dict = entry.get("agent") if isinstance(entry, dict) else entry
        if not isinstance(agent_dict, dict):
            continue

        agent_id = _extract_text(agent_dict.get("role") or agent_dict.get("name"))
        if not agent_id or agent_id in seen:
            continue

        profile = AgentProfile(
            agent_id=agent_id,
            display_name=_extract_text(agent_dict.get("name")) or agent_id,
            persona=_extract_text(agent_dict.get("persona")),
            description=_extract_text(agent_dict.get("description")),
            llm_backbone=_extract_llm_backbone(agent_dict),
            tools=_extract_tools(agent_dict),
            raw=agent_dict,
        )
        seen[agent_id] = profile

    return list(seen.values())


def _extract_text(value: Any) -> str:
    """Return stripped text if the value is a string, otherwise an empty string."""
    return value.strip() if isinstance(value, str) else ""


def _extract_tools(agent_dict: Mapping[str, Any]) -> list[str]:
    """Extract the list of unique tools from an agent description."""
    tools = agent_dict.get("tools")
    if not isinstance(tools, (list, tuple, set)):
        return []

    result: list[str] = []
    for entry in tools:
        if isinstance(entry, str):
            value = entry.strip()
        elif isinstance(entry, dict):
            name = entry.get("name") or entry.get("tool") or entry.get("id")
            value = name.strip() if isinstance(name, str) else ""
        else:
            value = ""
        if value and value not in result:
            result.append(value)
    return result


def _extract_llm_backbone(agent_dict: Mapping[str, Any]) -> str | None:
    """Extract the LLM identifier from various possible description fields."""
    candidate = agent_dict.get("llm") or agent_dict.get("model") or agent_dict.get("llm_backbone")

    if isinstance(candidate, dict):
        for key in ("model", "name", "type"):
            value = candidate.get(key)
            if isinstance(value, str) and value.strip():
                return value.strip()
        return None

    if isinstance(candidate, str) and candidate.strip():
        return candidate.strip()

    return None