File size: 1,112 Bytes
59ce1e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Agent 3 — Recommendation Agent"""

def compute_score(p: dict, max_dist: float) -> float:
    proximity    = 1 - (p["distance_km"] / max_dist) if max_dist > 0 else 1.0
    rating       = (p.get("rating", 3) - 1) / 4
    availability = 1.0 if p.get("available") else 0.0
    return round(0.40 * proximity + 0.40 * rating + 0.20 * availability, 4)

def run(discovery: dict) -> dict:
    providers = discovery.get("providers", [])
    if not providers:
        return {"best_provider": None, "all_ranked": [], "reasoning": "No providers found."}

    max_dist = max(p["distance_km"] for p in providers) or 1
    scored = [{**p, "score": compute_score(p, max_dist)} for p in providers]
    scored.sort(key=lambda x: x["score"], reverse=True)
    best = scored[0]

    reasoning = (
        f"**{best['name']}** selected — "
        f"{best['distance_km']} km away, "
        f"⭐ {best['rating']} rating, "
        f"{'available' if best['available'] else 'unavailable'}. "
        f"Composite score: **{best['score']}**"
    )
    return {"best_provider": best, "all_ranked": scored, "reasoning": reasoning}