File size: 3,309 Bytes
b0979b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Place/hotel search tool for MCP server.
"""
from smolagents import Tool
from typing import Optional, Dict, Any
from services.place_service import place_service

class PlaceSearchTool(Tool):
    """Tool for searching hotels and accommodations using enhanced place service."""
    
    def __init__(self):
        self.name = "place_search"
        self.description = "Search for hotels and accommodations in a location"
        self.input_type = "object"
        self.output_type = "object"
        self.inputs = {
            "location": {
                "type": "string",
                "description": "Location to search for places"
            },
            "max_distance": {
                "type": "integer",
                "description": "Maximum distance in miles",
                "optional": True,
                "nullable": True
            }
        }
        self.outputs = {
            "location": {
                "type": "string",
                "description": "Search location"
            },
            "total_found": {
                "type": "integer",
                "description": "Total number of places found"
            },
            "places": {
                "type": "array",
                "description": "List of places found"
            }
        }
        self.required_inputs = ["location"]
        self.is_initialized = True

    def forward(self, location: str, max_distance: Optional[int] = None) -> dict:
        """Search for places using enhanced place service."""
        try:
            # Convert max_distance to float for service compatibility
            distance = float(max_distance) if max_distance is not None else None
            
            # Use the enhanced place service
            results = place_service.search_places(location, distance)
            
            # Handle error cases
            if "error" in results:
                return {
                    "location": location,
                    "total_found": 0,
                    "places": [],
                    "error": results["error"]
                }
            
            # Convert service results to tool output format
            formatted_places = []
            for place in results.get('top_places', []):
                formatted_places.append({
                    "name": place.get('name', 'Unnamed Place'),
                    "type": place.get('type', 'Accommodation'),
                    "address": place.get('address', 'Address not available'),
                    "rating": place.get('rating', 'Not rated'),
                    "distance": place.get('distance', 'Distance not available'),
                    "description": place.get('description', 'Quality accommodation')
                })
            
            return {
                "location": results.get('location', location),
                "search_radius": results.get('search_radius', 'Not specified'),
                "total_found": results.get('total_found', len(formatted_places)),
                "places": formatted_places
            }
            
        except Exception as e:
            return {
                "location": location,
                "total_found": 0,
                "places": [],
                "error": f"Unexpected error: {str(e)}"
            }