Buckets:

download
raw
29.5 kB
"""
Navigation tool service using OpenStreetMap Nominatim and OSRM APIs.
This adapter provides navigation and geographic tool capabilities.
LIABILITY: This is informational only, not professional navigation advice.
"""
import asyncio
import logging
import os
import time
from typing import Any, Dict, List, Optional
from uuid import uuid4
import aiohttp
from ciris_engine.logic.utils.location_utils import get_user_location
from ciris_engine.schemas.adapters.tools import (
ToolDMAGuidance,
ToolDocumentation,
ToolExecutionResult,
ToolExecutionStatus,
ToolGotcha,
ToolInfo,
ToolParameterSchema,
UsageExample,
)
from ciris_engine.schemas.services.core import ServiceCapabilities
logger = logging.getLogger(__name__)
class NavigationToolService:
"""
Navigation tool service using OpenStreetMap.
Provides tools for:
- Geocoding (address to coordinates)
- Reverse geocoding (coordinates to address)
- Route calculation (point A to B)
SAFE DOMAIN: Navigation and geographic information only.
"""
def __init__(self) -> None:
"""Initialize the navigation tool service."""
# OpenStreetMap Nominatim doesn't require API key but has usage limits
# We should respect their usage policy: max 1 request per second
self.base_url = "https://nominatim.openstreetmap.org"
self.routing_url = "https://router.project-osrm.org" # OSRM for routing
# User agent is required by OSM policy
# Use PUBLIC_API_CONTACT_EMAIL from wizard, fall back to legacy env var
contact_email = os.getenv("PUBLIC_API_CONTACT_EMAIL", "")
if contact_email:
self.user_agent = f"CIRIS/2.3 ({contact_email})"
else:
self.user_agent = os.getenv("CIRIS_OSM_USER_AGENT", "CIRIS/1.0 (contact@ciris.ai)")
# Rate limiting
self._last_request_time: float = 0.0
self._min_request_interval = 1.0 # 1 second between requests
# User location from setup (for route_from_me functionality)
self._user_lat: Optional[float] = None
self._user_lon: Optional[float] = None
self._user_location_string: Optional[str] = None
user_location = get_user_location()
if user_location.has_coordinates():
self._user_lat = user_location.latitude
self._user_lon = user_location.longitude
self._user_location_string = user_location.location_string
logger.info(
f"User location available: {user_location.location_string} " f"({self._user_lat}, {self._user_lon})"
)
# Tool definitions
self._tools: Dict[str, ToolInfo] = self._define_tools()
logger.info(f"NavigationToolService initialized with user agent: {self.user_agent}")
def _define_tools(self) -> Dict[str, ToolInfo]:
"""Define available tools."""
tools: Dict[str, ToolInfo] = {
"navigation:geocode": ToolInfo(
name="navigation:geocode",
description="Convert an address or place name to geographic coordinates (latitude/longitude)",
parameters=ToolParameterSchema(
type="object",
properties={
"location": {
"type": "string",
"description": "Address or place name to geocode (e.g., 'San Francisco City Hall')",
}
},
required=["location"],
),
category="navigation",
cost=0.0,
when_to_use="When you need to find the coordinates of a location by name or address",
documentation=ToolDocumentation(
quick_start="Pass an address or place name to get latitude/longitude coordinates. "
"Use these coordinates with weather tools or for route planning.",
detailed_instructions="""
## Location Format
Accepts various formats:
- Full address: "123 Main St, San Francisco, CA 94105"
- Place name: "Eiffel Tower"
- City name: "Tokyo, Japan"
- Landmark: "Central Park, New York"
## Response Fields
- latitude/longitude: Coordinates in decimal degrees
- display_name: Full formatted address
- type: Location type (city, building, etc.)
- importance: Relevance score (0-1)
## Rate Limiting
This tool uses OpenStreetMap Nominatim which has a 1 request/second limit.
Multiple rapid requests will be automatically throttled.
""",
examples=[
UsageExample(
title="Address lookup",
code='{"location": "1600 Pennsylvania Avenue, Washington DC"}',
),
UsageExample(
title="Landmark lookup",
code='{"location": "Golden Gate Bridge, San Francisco"}',
),
],
gotchas=[
ToolGotcha(
title="Rate limited",
description="OpenStreetMap allows max 1 request per second. "
"Sequential geocoding requests will be delayed automatically.",
severity="info",
),
ToolGotcha(
title="Ambiguous locations",
description="Common place names may return unexpected results. "
"Be specific (include city/country) for accurate results.",
severity="warning",
),
],
),
dma_guidance=ToolDMAGuidance(
followup_actions=["weather:current if user wants weather at this location"],
),
),
"navigation:reverse_geocode": ToolInfo(
name="navigation:reverse_geocode",
description="Convert geographic coordinates to an address or place name",
parameters=ToolParameterSchema(
type="object",
properties={
"latitude": {
"type": "number",
"description": "Latitude coordinate",
},
"longitude": {
"type": "number",
"description": "Longitude coordinate",
},
},
required=["latitude", "longitude"],
),
category="navigation",
cost=0.0,
when_to_use="When you have coordinates and need to find the address or place name",
),
"navigation:route": ToolInfo(
name="navigation:route",
description="Calculate driving route between two locations with distance and duration",
parameters=ToolParameterSchema(
type="object",
properties={
"start": {
"type": "string",
"description": "Starting location (address or place name)",
},
"end": {
"type": "string",
"description": "Destination location (address or place name)",
},
},
required=["start", "end"],
),
category="navigation",
cost=0.0,
when_to_use="When you need to calculate distance and travel time between two locations",
),
}
# Add user location tools if user location is available from setup
if self._user_lat is not None and self._user_lon is not None:
tools["navigation:my_location"] = ToolInfo(
name="navigation:my_location",
description=(
f"Get the user's configured location coordinates ({self._user_location_string}). "
"Returns latitude/longitude for use with other tools."
),
parameters=ToolParameterSchema(
type="object",
properties={},
required=[],
),
category="navigation",
cost=0.0,
when_to_use="When you need to reference the user's home location for navigation or weather",
context_enrichment=True,
context_enrichment_params={"_cache_ttl": 3600.0}, # Cache for 1 hour (location rarely changes)
)
tools["navigation:route_from_me"] = ToolInfo(
name="navigation:route_from_me",
description=(
f"Calculate driving route FROM user's location ({self._user_location_string}) "
"to a destination. Only requires destination - start is your location."
),
parameters=ToolParameterSchema(
type="object",
properties={
"destination": {
"type": "string",
"description": "Destination address or place name",
},
},
required=["destination"],
),
category="navigation",
cost=0.0,
when_to_use="When the user asks 'how far is X' or 'how do I get to X' without specifying a start",
documentation=ToolDocumentation(
quick_start=(
f"Routes from your location ({self._user_location_string}). "
"Just provide the destination - no need to specify start."
),
examples=[
UsageExample(
title="Route to destination",
code='{"destination": "San Francisco Airport"}',
),
],
),
)
return tools
def get_capabilities(self) -> ServiceCapabilities:
"""Return service capabilities."""
return ServiceCapabilities(
service_name="navigation",
actions=list(self._tools.keys()),
version="1.0.0",
dependencies=[],
metadata={
"capabilities": [
"navigation:geocode",
"navigation:reverse_geocode",
"navigation:route",
"domain:navigation",
]
},
)
async def start(self) -> None:
"""Start the service."""
logger.info("NavigationToolService started")
async def stop(self) -> None:
"""Stop the service."""
logger.info("NavigationToolService stopped")
# ========== Tool Service Protocol Methods ==========
async def execute_tool(self, tool_name: str, parameters: Dict[str, Any]) -> ToolExecutionResult:
"""Execute a navigation tool."""
correlation_id = str(uuid4())
if tool_name not in self._tools:
return ToolExecutionResult(
tool_name=tool_name,
status=ToolExecutionStatus.NOT_FOUND,
success=False,
error=f"Unknown tool: {tool_name}",
correlation_id=correlation_id,
)
try:
if tool_name == "navigation:geocode":
return await self._execute_geocode(parameters, correlation_id)
elif tool_name == "navigation:reverse_geocode":
return await self._execute_reverse_geocode(parameters, correlation_id)
elif tool_name == "navigation:route":
return await self._execute_route(parameters, correlation_id)
elif tool_name == "navigation:my_location":
return await self._execute_my_location(correlation_id)
elif tool_name == "navigation:route_from_me":
return await self._execute_route_from_me(parameters, correlation_id)
else:
return ToolExecutionResult(
tool_name=tool_name,
status=ToolExecutionStatus.NOT_FOUND,
success=False,
error=f"Tool not implemented: {tool_name}",
correlation_id=correlation_id,
)
except Exception as e:
logger.error(f"Tool execution error: {e}", exc_info=True)
return ToolExecutionResult(
tool_name=tool_name,
status=ToolExecutionStatus.FAILED,
success=False,
error=str(e),
correlation_id=correlation_id,
)
async def list_tools(self) -> List[str]:
"""List available tools."""
return list(self._tools.keys())
async def get_tool_schema(self, tool_name: str) -> Optional[ToolParameterSchema]:
"""Get parameter schema for a tool."""
tool = self._tools.get(tool_name)
return tool.parameters if tool else None
async def get_available_tools(self) -> List[str]:
"""Get list of available tools."""
return list(self._tools.keys())
async def get_tool_info(self, tool_name: str) -> Optional[ToolInfo]:
"""Get detailed tool information."""
return self._tools.get(tool_name)
async def get_all_tool_info(self) -> List[ToolInfo]:
"""Get info for all tools."""
return list(self._tools.values())
async def validate_parameters(self, tool_name: str, parameters: Dict[str, Any]) -> bool:
"""Validate parameters for a tool."""
tool = self._tools.get(tool_name)
if not tool:
return False
# Check required parameters
for required in tool.parameters.required:
if required not in parameters:
return False
return True
async def get_tool_result(self, correlation_id: str, timeout: float = 30.0) -> Optional[ToolExecutionResult]:
"""Get result by correlation ID (tools execute synchronously so this returns None)."""
return None
def get_service_metadata(self) -> Dict[str, Any]:
"""Get service metadata."""
return {
"data_source": True,
"data_source_type": "rest",
"contains_pii": False,
"gdpr_applicable": False,
"connector_id": "openstreetmap",
}
# ========== Internal Methods ==========
async def _rate_limit(self) -> None:
"""Enforce rate limiting for OSM API."""
current_time = time.time()
elapsed = current_time - self._last_request_time
if elapsed < self._min_request_interval:
await asyncio.sleep(self._min_request_interval - elapsed)
self._last_request_time = time.time()
async def _geocode(self, location: str) -> Optional[Dict[str, Any]]:
"""Convert location name to coordinates."""
await self._rate_limit()
headers = {"User-Agent": self.user_agent}
params: Dict[str, Any] = {"q": location, "format": "json", "limit": 1}
try:
async with aiohttp.ClientSession() as session:
async with session.get(
f"{self.base_url}/search",
headers=headers,
params=params,
timeout=aiohttp.ClientTimeout(total=10),
) as response:
if response.status == 200:
data = await response.json()
if data:
return {
"latitude": float(data[0]["lat"]),
"longitude": float(data[0]["lon"]),
"display_name": data[0]["display_name"],
"type": data[0].get("type", "unknown"),
}
except Exception as e:
logger.warning(f"Geocoding failed for {location}: {e}")
return None
async def _reverse_geocode(self, lat: float, lon: float) -> Optional[Dict[str, Any]]:
"""Convert coordinates to address."""
await self._rate_limit()
headers = {"User-Agent": self.user_agent}
params: Dict[str, Any] = {"lat": lat, "lon": lon, "format": "json"}
try:
async with aiohttp.ClientSession() as session:
async with session.get(
f"{self.base_url}/reverse",
headers=headers,
params=params,
timeout=aiohttp.ClientTimeout(total=10),
) as response:
if response.status == 200:
data = await response.json()
return {
"display_name": data.get("display_name", "Unknown"),
"address": data.get("address", {}),
"type": data.get("type", "unknown"),
}
except Exception as e:
logger.warning(f"Reverse geocoding failed for {lat}, {lon}: {e}")
return None
async def _get_route(self, start_coords: Dict[str, Any], end_coords: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Get route between two coordinates using OSRM."""
url = (
f"{self.routing_url}/route/v1/driving/"
f"{start_coords['longitude']},{start_coords['latitude']};"
f"{end_coords['longitude']},{end_coords['latitude']}"
f"?overview=false&steps=false"
)
try:
async with aiohttp.ClientSession() as session:
async with session.get(
url,
headers={"User-Agent": self.user_agent},
timeout=aiohttp.ClientTimeout(total=10),
) as response:
if response.status == 200:
data = await response.json()
if data.get("code") == "Ok" and data.get("routes"):
route = data["routes"][0]
return {
"distance_km": round(route["distance"] / 1000, 1),
"distance_miles": round(route["distance"] / 1609.34, 1),
"duration_minutes": round(route["duration"] / 60, 0),
"duration_hours": round(route["duration"] / 3600, 2),
}
except Exception as e:
logger.warning(f"Routing failed: {e}")
return None
# ========== Tool Implementations ==========
async def _execute_my_location(self, correlation_id: str) -> ToolExecutionResult:
"""Execute my_location tool - returns user's configured location."""
if self._user_lat is None or self._user_lon is None:
return ToolExecutionResult(
tool_name="navigation:my_location",
status=ToolExecutionStatus.FAILED,
success=False,
error="No user location available. Set location during setup to use this feature.",
correlation_id=correlation_id,
)
return ToolExecutionResult(
tool_name="navigation:my_location",
status=ToolExecutionStatus.COMPLETED,
success=True,
data={
"location": self._user_location_string,
"latitude": self._user_lat,
"longitude": self._user_lon,
"source": "user_setup",
},
correlation_id=correlation_id,
)
async def _execute_route_from_me(self, parameters: Dict[str, Any], correlation_id: str) -> ToolExecutionResult:
"""Execute route_from_me tool - routes from user's location to destination."""
if self._user_lat is None or self._user_lon is None:
return ToolExecutionResult(
tool_name="navigation:route_from_me",
status=ToolExecutionStatus.FAILED,
success=False,
error="No user location available. Set location during setup to use this feature.",
correlation_id=correlation_id,
)
destination = parameters.get("destination")
if not destination:
return ToolExecutionResult(
tool_name="navigation:route_from_me",
status=ToolExecutionStatus.FAILED,
success=False,
error="Missing required parameter: destination",
correlation_id=correlation_id,
)
# Use the regular route execution but with user location as start
# First, geocode the destination
dest_result = await self._geocode(destination)
if not dest_result:
return ToolExecutionResult(
tool_name="navigation:route_from_me",
status=ToolExecutionStatus.FAILED,
success=False,
error=f"Could not find destination: {destination}",
correlation_id=correlation_id,
)
# Calculate route from user's coordinates to destination
route = await self._get_route(
{"latitude": self._user_lat, "longitude": self._user_lon},
{"latitude": dest_result["latitude"], "longitude": dest_result["longitude"]},
)
if route:
return ToolExecutionResult(
tool_name="navigation:route_from_me",
status=ToolExecutionStatus.COMPLETED,
success=True,
data={
"start": self._user_location_string,
"start_coordinates": {"latitude": self._user_lat, "longitude": self._user_lon},
"destination": destination,
"destination_coordinates": {
"latitude": dest_result["latitude"],
"longitude": dest_result["longitude"],
},
"distance_km": route["distance_km"],
"distance_miles": route["distance_miles"],
"duration_minutes": route["duration_minutes"],
"duration_formatted": route["duration_formatted"],
},
correlation_id=correlation_id,
)
else:
return ToolExecutionResult(
tool_name="navigation:route_from_me",
status=ToolExecutionStatus.FAILED,
success=False,
error=f"Could not calculate route to {destination}",
correlation_id=correlation_id,
)
async def _execute_geocode(self, parameters: Dict[str, Any], correlation_id: str) -> ToolExecutionResult:
"""Execute geocode tool."""
location = parameters.get("location")
if not location:
return ToolExecutionResult(
tool_name="navigation:geocode",
status=ToolExecutionStatus.FAILED,
success=False,
error="Missing required parameter: location",
correlation_id=correlation_id,
)
result = await self._geocode(location)
if result:
return ToolExecutionResult(
tool_name="navigation:geocode",
status=ToolExecutionStatus.COMPLETED,
success=True,
data={
"location": location,
"latitude": result["latitude"],
"longitude": result["longitude"],
"display_name": result["display_name"],
"type": result["type"],
},
correlation_id=correlation_id,
)
else:
return ToolExecutionResult(
tool_name="navigation:geocode",
status=ToolExecutionStatus.FAILED,
success=False,
error=f"Could not find location: {location}",
correlation_id=correlation_id,
)
async def _execute_reverse_geocode(self, parameters: Dict[str, Any], correlation_id: str) -> ToolExecutionResult:
"""Execute reverse geocode tool."""
lat = parameters.get("latitude")
lon = parameters.get("longitude")
if lat is None or lon is None:
return ToolExecutionResult(
tool_name="navigation:reverse_geocode",
status=ToolExecutionStatus.FAILED,
success=False,
error="Missing required parameters: latitude and longitude",
correlation_id=correlation_id,
)
try:
lat_float = float(lat)
lon_float = float(lon)
except (ValueError, TypeError):
return ToolExecutionResult(
tool_name="navigation:reverse_geocode",
status=ToolExecutionStatus.FAILED,
success=False,
error="Invalid coordinate values",
correlation_id=correlation_id,
)
result = await self._reverse_geocode(lat_float, lon_float)
if result:
return ToolExecutionResult(
tool_name="navigation:reverse_geocode",
status=ToolExecutionStatus.COMPLETED,
success=True,
data={
"latitude": lat_float,
"longitude": lon_float,
"display_name": result["display_name"],
"address": result["address"],
"type": result["type"],
},
correlation_id=correlation_id,
)
else:
return ToolExecutionResult(
tool_name="navigation:reverse_geocode",
status=ToolExecutionStatus.FAILED,
success=False,
error=f"Could not reverse geocode coordinates: {lat}, {lon}",
correlation_id=correlation_id,
)
async def _execute_route(self, parameters: Dict[str, Any], correlation_id: str) -> ToolExecutionResult:
"""Execute route calculation tool."""
start = parameters.get("start")
end = parameters.get("end")
if not start or not end:
return ToolExecutionResult(
tool_name="navigation:route",
status=ToolExecutionStatus.FAILED,
success=False,
error="Missing required parameters: start and end",
correlation_id=correlation_id,
)
# Geocode both locations
start_coords = await self._geocode(start)
if not start_coords:
return ToolExecutionResult(
tool_name="navigation:route",
status=ToolExecutionStatus.FAILED,
success=False,
error=f"Could not find start location: {start}",
correlation_id=correlation_id,
)
end_coords = await self._geocode(end)
if not end_coords:
return ToolExecutionResult(
tool_name="navigation:route",
status=ToolExecutionStatus.FAILED,
success=False,
error=f"Could not find end location: {end}",
correlation_id=correlation_id,
)
# Calculate route
route = await self._get_route(start_coords, end_coords)
if route:
return ToolExecutionResult(
tool_name="navigation:route",
status=ToolExecutionStatus.COMPLETED,
success=True,
data={
"start": {
"query": start,
"resolved": start_coords["display_name"],
"latitude": start_coords["latitude"],
"longitude": start_coords["longitude"],
},
"end": {
"query": end,
"resolved": end_coords["display_name"],
"latitude": end_coords["latitude"],
"longitude": end_coords["longitude"],
},
"distance_km": route["distance_km"],
"distance_miles": route["distance_miles"],
"duration_minutes": route["duration_minutes"],
"duration_hours": route["duration_hours"],
"source": "OpenStreetMap/OSRM",
"disclaimer": "Travel times are estimates. Always follow traffic laws and check current conditions.",
},
correlation_id=correlation_id,
)
else:
return ToolExecutionResult(
tool_name="navigation:route",
status=ToolExecutionStatus.FAILED,
success=False,
error="Could not calculate route between locations",
correlation_id=correlation_id,
)

Xet Storage Details

Size:
29.5 kB
·
Xet hash:
8e6d8ad3ef91b4307c696b9424ca496bb8eaa7d5f8fa639c422c322313f7a3c1

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.