mirror of
https://github.com/zvx-echo6/recon.git
synced 2026-05-20 06:34:40 +02:00
fix(navi): format tool output as human-readable directions
This commit is contained in:
parent
a9510b5ed9
commit
9841c38011
1 changed files with 26 additions and 31 deletions
|
|
@ -1,7 +1,7 @@
|
|||
"""
|
||||
title: Navigation
|
||||
author: Echo6
|
||||
version: 1.0.0
|
||||
version: 1.1.0
|
||||
description: Turn-by-turn directions and geocoding via Photon + Valhalla on recon-vm. Supports driving, walking, cycling, and truck routing with worldwide coverage (281M places).
|
||||
"""
|
||||
|
||||
|
|
@ -57,28 +57,24 @@ class Tools:
|
|||
mode: str = "auto",
|
||||
) -> str:
|
||||
"""
|
||||
Get turn-by-turn driving, walking, or cycling directions between two locations.
|
||||
Use this when someone asks how to get somewhere, asks for directions, or wants to know distance/time between places.
|
||||
Get turn-by-turn directions between two locations. When this tool returns results, present the directions exactly as returned — do not summarize or rephrase. Include all steps.
|
||||
|
||||
:param origin: Starting location — address, place name, or lat,lon coordinates
|
||||
:param destination: Destination — address, place name, or lat,lon coordinates
|
||||
:param mode: Travel mode: auto, pedestrian, bicycle, or truck (default: auto)
|
||||
:return: Directions with distance, time, and turn-by-turn maneuvers
|
||||
:return: Formatted turn-by-turn directions
|
||||
"""
|
||||
if mode not in ("auto", "pedestrian", "bicycle", "truck"):
|
||||
mode = "auto"
|
||||
|
||||
# Geocode origin
|
||||
orig_lat, orig_lon, orig_name = self._geocode(origin)
|
||||
if orig_lat is None:
|
||||
return json.dumps({"error": f"Could not find location: {origin}"})
|
||||
return f"Could not find location: {origin}"
|
||||
|
||||
# Geocode destination
|
||||
dest_lat, dest_lon, dest_name = self._geocode(destination)
|
||||
if dest_lat is None:
|
||||
return json.dumps({"error": f"Could not find location: {destination}"})
|
||||
return f"Could not find location: {destination}"
|
||||
|
||||
# Route via Valhalla
|
||||
try:
|
||||
resp = requests.post(
|
||||
f"{self.valves.valhalla_url}/route",
|
||||
|
|
@ -93,30 +89,29 @@ class Tools:
|
|||
timeout=30,
|
||||
)
|
||||
except requests.RequestException:
|
||||
return json.dumps({"error": "Navigation service unavailable"})
|
||||
return "Navigation service unavailable"
|
||||
|
||||
if resp.status_code != 200:
|
||||
return json.dumps({"error": "No route found between locations"})
|
||||
return "No route found between locations"
|
||||
|
||||
trip = resp.json()["trip"]
|
||||
summary = trip["summary"]
|
||||
maneuvers = []
|
||||
for m in trip["legs"][0]["maneuvers"]:
|
||||
streets = m.get("street_names", [])
|
||||
entry = {
|
||||
"instruction": m["instruction"],
|
||||
"distance_miles": round(m.get("length", 0), 2),
|
||||
}
|
||||
if streets:
|
||||
entry["street"] = streets[0]
|
||||
maneuvers.append(entry)
|
||||
legs = trip["legs"][0]["maneuvers"]
|
||||
|
||||
result = {
|
||||
"origin": orig_name,
|
||||
"destination": dest_name,
|
||||
"distance_miles": round(summary["length"], 1),
|
||||
"time_minutes": round(summary["time"] / 60, 1),
|
||||
"mode": mode,
|
||||
"maneuvers": maneuvers,
|
||||
}
|
||||
return json.dumps(result)
|
||||
miles = round(summary["length"], 1)
|
||||
minutes = round(summary["time"] / 60, 1)
|
||||
|
||||
lines = [
|
||||
f"Directions from {orig_name} to {dest_name} ({mode}):",
|
||||
f"Distance: {miles} miles | Time: {minutes} minutes",
|
||||
"",
|
||||
]
|
||||
for i, m in enumerate(legs, 1):
|
||||
inst = m["instruction"]
|
||||
dist = m.get("length", 0)
|
||||
if dist > 0:
|
||||
lines.append(f"{i}. {inst} — {round(dist, 1)} mi")
|
||||
else:
|
||||
lines.append(f"{i}. {inst}")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue