|
| 1 | +"""Attribute extractors for SmoLAgents agent operations.""" |
| 2 | + |
| 3 | +from typing import Any, Dict, Optional, Tuple |
| 4 | +import uuid |
| 5 | +import time |
| 6 | + |
| 7 | +from agentops.instrumentation.common.attributes import get_common_attributes |
| 8 | +from agentops.semconv.agent import AgentAttributes |
| 9 | +from agentops.semconv.tool import ToolAttributes |
| 10 | + |
| 11 | + |
| 12 | +def get_agent_attributes( |
| 13 | + args: Optional[Tuple] = None, |
| 14 | + kwargs: Optional[Dict] = None, |
| 15 | + return_value: Optional[Any] = None, |
| 16 | +) -> Dict[str, Any]: |
| 17 | + """Extract attributes from an agent execution. |
| 18 | +
|
| 19 | + Args: |
| 20 | + args: Optional tuple of positional arguments |
| 21 | + kwargs: Optional dict of keyword arguments |
| 22 | + return_value: Optional return value from the wrapped function |
| 23 | +
|
| 24 | + Returns: |
| 25 | + Dict containing extracted attributes |
| 26 | + """ |
| 27 | + attributes = get_common_attributes() |
| 28 | + |
| 29 | + # Extract agent info from instance |
| 30 | + if args and len(args) > 0: |
| 31 | + instance = args[0] |
| 32 | + agent_type = instance.__class__.__name__ |
| 33 | + tools = [t.name for t in instance.tools] if hasattr(instance, "tools") else [] |
| 34 | + attributes.update( |
| 35 | + { |
| 36 | + AgentAttributes.AGENT_ID: str(uuid.uuid4()), |
| 37 | + AgentAttributes.AGENT_NAME: agent_type, |
| 38 | + AgentAttributes.AGENT_ROLE: "executor", |
| 39 | + AgentAttributes.AGENT_TOOLS: tools, |
| 40 | + } |
| 41 | + ) |
| 42 | + |
| 43 | + # Extract task from kwargs or args |
| 44 | + task = kwargs.get("task", args[1] if len(args) > 1 else "unknown") if kwargs else "unknown" |
| 45 | + attributes[AgentAttributes.AGENT_REASONING] = task |
| 46 | + |
| 47 | + return attributes |
| 48 | + |
| 49 | + |
| 50 | +def get_tool_call_attributes( |
| 51 | + args: Optional[Tuple] = None, |
| 52 | + kwargs: Optional[Dict] = None, |
| 53 | + return_value: Optional[Any] = None, |
| 54 | +) -> Dict[str, Any]: |
| 55 | + """Extract attributes from a tool call. |
| 56 | +
|
| 57 | + Args: |
| 58 | + args: Optional tuple of positional arguments |
| 59 | + kwargs: Optional dict of keyword arguments |
| 60 | + return_value: Optional return value from the wrapped function |
| 61 | +
|
| 62 | + Returns: |
| 63 | + Dict containing extracted attributes |
| 64 | + """ |
| 65 | + attributes = get_common_attributes() |
| 66 | + |
| 67 | + # Extract tool info from instance and args |
| 68 | + if args and len(args) > 0: |
| 69 | + instance = args[0] |
| 70 | + tool_name = instance.name if hasattr(instance, "name") else "unknown" |
| 71 | + tool_description = instance.description if hasattr(instance, "description") else "unknown" |
| 72 | + |
| 73 | + # Get arguments from args/kwargs |
| 74 | + arguments = {} |
| 75 | + if len(args) > 1: |
| 76 | + arguments = args[1] |
| 77 | + elif kwargs: |
| 78 | + arguments = kwargs |
| 79 | + |
| 80 | + # Track execution time and success |
| 81 | + start_time = time.time() |
| 82 | + error = None |
| 83 | + |
| 84 | + try: |
| 85 | + if return_value is not None: |
| 86 | + execution_time = time.time() - start_time |
| 87 | + attributes.update( |
| 88 | + { |
| 89 | + ToolAttributes.TOOL_ID: str(uuid.uuid4()), |
| 90 | + ToolAttributes.TOOL_NAME: tool_name, |
| 91 | + ToolAttributes.TOOL_DESCRIPTION: tool_description, |
| 92 | + ToolAttributes.TOOL_PARAMETERS: arguments, |
| 93 | + ToolAttributes.TOOL_STATUS: "success", |
| 94 | + ToolAttributes.TOOL_RESULT: str(return_value), |
| 95 | + "tool.execution_time": execution_time, |
| 96 | + } |
| 97 | + ) |
| 98 | + except Exception as e: |
| 99 | + error = str(e) |
| 100 | + attributes.update( |
| 101 | + { |
| 102 | + ToolAttributes.TOOL_ID: str(uuid.uuid4()), |
| 103 | + ToolAttributes.TOOL_NAME: tool_name, |
| 104 | + ToolAttributes.TOOL_DESCRIPTION: tool_description, |
| 105 | + ToolAttributes.TOOL_PARAMETERS: arguments, |
| 106 | + ToolAttributes.TOOL_STATUS: "error", |
| 107 | + ToolAttributes.TOOL_ERROR: error, |
| 108 | + } |
| 109 | + ) |
| 110 | + |
| 111 | + return attributes |
| 112 | + |
| 113 | + |
| 114 | +def get_planning_step_attributes( |
| 115 | + args: Optional[Tuple] = None, |
| 116 | + kwargs: Optional[Dict] = None, |
| 117 | + return_value: Optional[Any] = None, |
| 118 | +) -> Dict[str, Any]: |
| 119 | + """Extract attributes from a planning step. |
| 120 | +
|
| 121 | + Args: |
| 122 | + args: Optional tuple of positional arguments |
| 123 | + kwargs: Optional dict of keyword arguments |
| 124 | + return_value: Optional return value from the wrapped function |
| 125 | +
|
| 126 | + Returns: |
| 127 | + Dict containing extracted attributes |
| 128 | + """ |
| 129 | + attributes = get_common_attributes() |
| 130 | + |
| 131 | + # Extract step info from kwargs |
| 132 | + if kwargs: |
| 133 | + step_number = kwargs.get("step_number", 0) |
| 134 | + is_first_step = kwargs.get("is_first_step", False) |
| 135 | + task = kwargs.get("task", "unknown") |
| 136 | + |
| 137 | + attributes.update( |
| 138 | + { |
| 139 | + AgentAttributes.AGENT_REASONING: task, |
| 140 | + "planning.step_number": step_number, |
| 141 | + "planning.is_first_step": is_first_step, |
| 142 | + } |
| 143 | + ) |
| 144 | + |
| 145 | + return attributes |
0 commit comments