Get the current weather for a city. Args: city: The name of the city to get weather for. Returns: A dictionary with weather information.
(city: str, tool_context: ToolContext)
| 23 | |
| 24 | |
| 25 | async def get_weather(city: str, tool_context: ToolContext) -> dict: |
| 26 | """Get the current weather for a city. |
| 27 | |
| 28 | Args: |
| 29 | city: The name of the city to get weather for. |
| 30 | |
| 31 | Returns: |
| 32 | A dictionary with weather information. |
| 33 | """ |
| 34 | # Simulate some async processing time (non-blocking) |
| 35 | await asyncio.sleep(2) |
| 36 | |
| 37 | # Mock weather data |
| 38 | weather_data = { |
| 39 | 'New York': {'temp': 72, 'condition': 'sunny', 'humidity': 45}, |
| 40 | 'London': {'temp': 60, 'condition': 'cloudy', 'humidity': 80}, |
| 41 | 'Tokyo': {'temp': 68, 'condition': 'rainy', 'humidity': 90}, |
| 42 | 'San Francisco': {'temp': 65, 'condition': 'foggy', 'humidity': 85}, |
| 43 | 'Paris': {'temp': 58, 'condition': 'overcast', 'humidity': 70}, |
| 44 | 'Sydney': {'temp': 75, 'condition': 'sunny', 'humidity': 60}, |
| 45 | } |
| 46 | |
| 47 | result = weather_data.get( |
| 48 | city, |
| 49 | { |
| 50 | 'temp': 70, |
| 51 | 'condition': 'unknown', |
| 52 | 'humidity': 50, |
| 53 | 'note': ( |
| 54 | f'Weather data not available for {city}, showing default values' |
| 55 | ), |
| 56 | }, |
| 57 | ) |
| 58 | |
| 59 | # Store in context for testing thread safety |
| 60 | if 'weather_requests' not in tool_context.state: |
| 61 | tool_context.state['weather_requests'] = [] |
| 62 | tool_context.state['weather_requests'].append( |
| 63 | {'city': city, 'result': result} |
| 64 | ) |
| 65 | |
| 66 | return { |
| 67 | 'city': city, |
| 68 | 'temperature': result['temp'], |
| 69 | 'condition': result['condition'], |
| 70 | 'humidity': result['humidity'], |
| 71 | **({'note': result['note']} if 'note' in result else {}), |
| 72 | } |
| 73 | |
| 74 | |
| 75 | async def get_currency_rate( |