Spaces:
Running
Running
File size: 1,933 Bytes
1635e66 | 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 | """Helpers for session usage-threshold approval warnings."""
from typing import Any
USAGE_THRESHOLD_TOOL_NAME = "usage_threshold"
USAGE_WARNING_FIRST_THRESHOLD_USD = 5.0
USAGE_WARNING_MULTIPLIER = 2.0
def normalize_usage_threshold(value: Any) -> float:
"""Return a usable positive threshold, defaulting to the first warning."""
if isinstance(value, bool):
return USAGE_WARNING_FIRST_THRESHOLD_USD
try:
threshold = float(value)
except (TypeError, ValueError):
return USAGE_WARNING_FIRST_THRESHOLD_USD
if threshold <= 0:
return USAGE_WARNING_FIRST_THRESHOLD_USD
return threshold
def next_usage_warning_threshold(
current_spend_usd: float,
acknowledged_threshold_usd: float,
) -> float:
"""Advance the next threshold until it is above the current spend."""
threshold = normalize_usage_threshold(acknowledged_threshold_usd)
current = max(0.0, float(current_spend_usd or 0.0))
while threshold <= current:
threshold *= USAGE_WARNING_MULTIPLIER
return round(threshold, 4)
def is_usage_threshold_pending(pending_approval: Any) -> bool:
return (
isinstance(pending_approval, dict)
and pending_approval.get("kind") == USAGE_THRESHOLD_TOOL_NAME
)
def usage_threshold_pending_to_tool(pending_approval: dict[str, Any]) -> dict[str, Any]:
"""Represent a synthetic usage approval as the existing pending-tool shape."""
tool_call_id = str(pending_approval.get("tool_call_id") or "")
arguments = {
"threshold_usd": pending_approval.get("threshold_usd"),
"current_spend_usd": pending_approval.get("current_spend_usd"),
"next_threshold_usd": pending_approval.get("next_threshold_usd"),
"billing_source": pending_approval.get("billing_source"),
}
return {
"tool": USAGE_THRESHOLD_TOOL_NAME,
"tool_call_id": tool_call_id,
"arguments": arguments,
}
|