Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 6,385 Bytes
0c21cf1 0972775 0c21cf1 0972775 0c21cf1 0972775 0c21cf1 | 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 | """
Utils Tools - General utility operations
Provides system information like current date/time with timezone support.
"""
import zoneinfo
from datetime import datetime
from typing import Any, Dict, Literal
from agent.tools.types import ToolResult
# Operation names
OperationType = Literal["get_datetime"]
class UtilsTool:
"""Tool for general utility operations."""
async def execute(self, params: Dict[str, Any]) -> ToolResult:
"""Execute the specified utility operation."""
operation = params.get("operation")
args = params.get("args", {})
# If no operation provided, return usage instructions
if not operation:
return self._show_help()
# Normalize operation name
operation = operation.lower()
# Check if help is requested
if args.get("help"):
return self._show_operation_help(operation)
try:
# Route to appropriate handler
if operation == "get_datetime":
return await self._get_datetime(args)
else:
return {
"formatted": f'Unknown operation: "{operation}"\n\n'
"Available operations: get_datetime\n\n"
"Call this tool with no operation for full usage instructions.",
"totalResults": 0,
"resultsShared": 0,
"isError": True,
}
except Exception as e:
return {
"formatted": f"Error executing {operation}: {str(e)}",
"totalResults": 0,
"resultsShared": 0,
"isError": True,
}
def _show_help(self) -> ToolResult:
"""Show usage instructions when tool is called with no arguments."""
usage_text = """# Utils Tool
Utility operations for system information.
## Available Commands
- **get_datetime** - Get current date and time with timezone support
## Examples
### Get current date and time (Paris timezone by default)
Call this tool with:
```json
{
"operation": "get_datetime",
"args": {}
}
```
### Get current date and time in a specific timezone
Call this tool with:
```json
{
"operation": "get_datetime",
"args": {
"timezone": "America/New_York"
}
}
```
Common timezones: Europe/Paris, America/New_York, America/Los_Angeles, Asia/Tokyo, UTC
## Tips
- **Default timezone**: Paris (Europe/Paris)
- **Date format**: dd-mm-yyyy
- **Time format**: HH:MM:SS.mmm (24-hour format with milliseconds)
- **Timezone names**: Use IANA timezone database names (e.g., "Europe/Paris", "UTC")
"""
return {"formatted": usage_text, "totalResults": 1, "resultsShared": 1}
def _show_operation_help(self, operation: str) -> ToolResult:
"""Show help for a specific operation."""
help_text = f"Help for operation: {operation}\n\nCall with appropriate arguments. Use the main help for examples."
return {"formatted": help_text, "totalResults": 1, "resultsShared": 1}
async def _get_datetime(self, args: Dict[str, Any]) -> ToolResult:
"""Get current date and time with timezone support."""
timezone_name = args.get("timezone", "Europe/Paris")
try:
# Get timezone object
tz = zoneinfo.ZoneInfo(timezone_name)
# Get current datetime in specified timezone
now = datetime.now(tz)
# Format date as dd-mm-yyyy
date_str = now.strftime("%d-%m-%Y")
# Format time as HH:MM:SS.mmm
time_str = now.strftime("%H:%M:%S.%f")[
:-3
] # Remove last 3 digits to keep only milliseconds
# Get timezone abbreviation/offset
tz_offset = now.strftime("%z")
tz_name = now.strftime("%Z")
response = f"""✓ Current date and time
**Date:** {date_str}
**Time:** {time_str}
**Timezone:** {timezone_name} ({tz_name}, UTC{tz_offset[:3]}:{tz_offset[3:]})
**ISO Format:** {now.isoformat()}
**Unix Timestamp:** {int(now.timestamp())}"""
return {"formatted": response, "totalResults": 1, "resultsShared": 1}
except zoneinfo.ZoneInfoNotFoundError:
return {
"formatted": f"Invalid timezone: {timezone_name}\n\n"
"Use IANA timezone database names like:\n"
"- Europe/Paris\n"
"- America/New_York\n"
"- Asia/Tokyo\n"
"- UTC\n\n"
"See: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones",
"totalResults": 0,
"resultsShared": 0,
"isError": True,
}
except Exception as e:
return {
"formatted": f"Failed to get date/time: {str(e)}",
"totalResults": 0,
"resultsShared": 0,
"isError": True,
}
# Tool specification for agent registration
UTILS_TOOL_SPEC = {
"name": "utils",
"description": (
"Utility operations for system information. "
"Get current date (dd-mm-yyyy) and time (HH:MM:SS.mmm) with timezone support. "
"Default timezone: Paris (Europe/Paris). "
"Call with no operation for full usage instructions."
),
"parameters": {
"type": "object",
"properties": {
"operation": {
"type": "string",
"enum": ["get_datetime"],
"description": "Operation to execute. Valid values: [get_datetime]",
},
"args": {
"type": "object",
"description": (
"Operation-specific arguments as a JSON object. "
"For get_datetime: timezone (string, optional, default: Europe/Paris). "
"Use IANA timezone names like 'America/New_York', 'Asia/Tokyo', 'UTC'."
),
"additionalProperties": True,
},
},
},
}
async def utils_handler(arguments: Dict[str, Any]) -> tuple[str, bool]:
"""Handler for agent tool router."""
try:
tool = UtilsTool()
result = await tool.execute(arguments)
return result["formatted"], not result.get("isError", False)
except Exception as e:
return f"Error executing Utils tool: {str(e)}", False
|