(args: list[str])
| 1207 | |
| 1208 | |
| 1209 | def _cmd_spend(args: list[str]) -> None: |
| 1210 | from uncommon_route.spend_control import SpendControl |
| 1211 | |
| 1212 | sc = SpendControl() |
| 1213 | |
| 1214 | if not args: |
| 1215 | args = ["status"] |
| 1216 | |
| 1217 | sub = args[0] |
| 1218 | |
| 1219 | if sub == "status": |
| 1220 | s = sc.status() |
| 1221 | print(" Spending Limits:") |
| 1222 | if s.limits.per_request is not None: |
| 1223 | print(f" Per-request: ${s.limits.per_request:.2f}") |
| 1224 | if s.limits.hourly is not None: |
| 1225 | rem = s.remaining.get("hourly") |
| 1226 | print(f" Hourly: ${s.limits.hourly:.2f} (spent: ${s.spent['hourly']:.4f}, remaining: ${rem:.4f})" if rem is not None else "") |
| 1227 | if s.limits.daily is not None: |
| 1228 | rem = s.remaining.get("daily") |
| 1229 | print(f" Daily: ${s.limits.daily:.2f} (spent: ${s.spent['daily']:.4f}, remaining: ${rem:.4f})" if rem is not None else "") |
| 1230 | if s.limits.session is not None: |
| 1231 | rem = s.remaining.get("session") |
| 1232 | print(f" Session: ${s.limits.session:.2f} (spent: ${s.spent['session']:.4f}, remaining: ${rem:.4f})" if rem is not None else "") |
| 1233 | if all(v is None for v in vars(s.limits).values()): |
| 1234 | print(" (no limits set)") |
| 1235 | print(f"\n Total calls this session: {s.calls}") |
| 1236 | |
| 1237 | elif sub == "set": |
| 1238 | if len(args) < 3: |
| 1239 | print("Usage: uncommon-route spend set <window> <amount>", file=sys.stderr) |
| 1240 | print(" Windows: per_request, hourly, daily, session", file=sys.stderr) |
| 1241 | sys.exit(1) |
| 1242 | window = args[1] |
| 1243 | amount = float(args[2]) |
| 1244 | sc.set_limit(window, amount) # type: ignore[arg-type] |
| 1245 | print(f" Set {window} limit: ${amount:.2f}") |
| 1246 | |
| 1247 | elif sub == "clear": |
| 1248 | if len(args) < 2: |
| 1249 | print("Usage: uncommon-route spend clear <window>", file=sys.stderr) |
| 1250 | sys.exit(1) |
| 1251 | window = args[1] |
| 1252 | sc.clear_limit(window) # type: ignore[arg-type] |
| 1253 | print(f" Cleared {window} limit") |
| 1254 | |
| 1255 | elif sub == "history": |
| 1256 | flags, _ = _parse_flags(args[1:], {"limit": True}) |
| 1257 | limit = int(flags.get("limit", 20)) |
| 1258 | records = sc.history(limit=limit) |
| 1259 | if not records: |
| 1260 | print(" No spending records") |
| 1261 | return |
| 1262 | print(f" Recent spending ({len(records)} records):") |
| 1263 | for r in records: |
| 1264 | ts = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(r.timestamp)) |
| 1265 | model_str = f" [{r.model}]" if r.model else "" |
| 1266 | print(f" {ts} ${r.amount:.6f}{model_str}") |
nothing calls this directly
no test coverage detected