# 创建一个 session session_stateful = await session_service_stateful.create_session( app_name=APP_NAME, # Use the consistent app name user_id=USER_ID_STATEFUL, session_id=SESSION_ID_STATEFUL, state=initial_state # <<< Initialize state during creation )
if city_normalized in mock_weather_db: data = mock_weather_db[city_normalized] temp_c = data["temp_c"] condition = data["condition"]
# Format temperature based on state preference if preferred_unit == "Fahrenheit": temp_value = (temp_c * 9/5) + 32# Calculate Fahrenheit temp_unit = "°F" else: # Default to Celsius temp_value = temp_c temp_unit = "°C"
report = f"The weather in {city.capitalize()} is {condition} with a temperature of {temp_value:.0f}{temp_unit}." result = {"status": "success", "report": report} print(f"--- Tool: Generated report in {preferred_unit}. Result: {result} ---")
# Example of writing back to state (optional for this tool) tool_context.state["last_city_checked_stateful"] = city print(f"--- Tool: Updated state 'last_city_checked_stateful': {city} ---")
return result else: # Handle city not found error_msg = f"Sorry, I don't have weather information for '{city}'." print(f"--- Tool: City '{city}' not found. ---") return {"status": "error", "error_message": error_msg}