Files
agent-zero/plugins/_context_window/helpers/currency.py
Alessandro 935fb5cdcc Add context window display controls and price currency selection
Add plugin settings to toggle the full context breakdown and each provider
usage row. Match price typography to the cache and token summary rows.

Keep USD amounts unchanged without exchange-rate requests. Convert non-USD
prices using validated Frankfurter rates cached for 24 hours, with dated
tooltips and an explicit USD fallback when a rate is unavailable. Preserve
stored provider usage and back off failed lookups for five minutes.

Validation: 20 context-window plugin tests pass. Verified settings persistence,
EUR price rendering, USD fallback, and exchange-rate lookup against the live
Docker runtime at localhost:32081.
2026-10-07 23:31:54 +02:00

40 lines
1.2 KiB
Python

import math
import re
import time
from datetime import date
import httpx
from helpers import cache
CACHE_AREA = "context_window_currency(plugins)"
async def get_exchange_rate(currency: str) -> dict | None:
if currency == "USD" or not re.fullmatch(r"[A-Z]{3}", currency):
return None
cached = cache.get(CACHE_AREA, currency)
if cached and time.monotonic() < cached[0]:
return cached[1]
result = None
try:
async with httpx.AsyncClient(timeout=3) as client:
response = await client.get(f"https://api.frankfurter.dev/v2/rate/USD/{currency}")
response.raise_for_status()
data = response.json()
rate = float(data["rate"])
rate_date = date.fromisoformat(data["date"]).isoformat()
if (
data.get("base") != "USD" or data.get("quote") != currency
or isinstance(data["rate"], bool) or not math.isfinite(rate) or rate <= 0
):
raise ValueError("Invalid exchange rate")
result = {"code": currency, "rate": rate, "date": rate_date}
except (httpx.HTTPError, KeyError, TypeError, ValueError):
pass
cache.add(CACHE_AREA, currency, (time.monotonic() + (86400 if result else 300), result))
return result