|
3 | 3 | import webbrowser |
4 | 4 | import requests |
5 | 5 | from rich.console import Console |
6 | | -from rich.prompt import Confirm |
7 | 6 | from rich.table import Table |
8 | 7 | from . import __base_url__ |
9 | 8 | from .api import handle_api_error |
10 | 9 | from .auth import handle_authentication |
11 | 10 | from .config import load_weco_api_key |
12 | 11 |
|
13 | 12 |
|
14 | | -def check_promotional_credits(model: str, api_keys: dict[str, str] | None, console: Console) -> str: |
15 | | - """Check for promotional credits and prompt user to use them if applicable. |
16 | | -
|
17 | | - Returns the (possibly modified) model string with provider prefix. |
18 | | - """ |
19 | | - # Skip if user is using their own API keys (BYOK — not billed) |
20 | | - if api_keys: |
21 | | - return model |
22 | | - |
23 | | - # If model already has provider prefix, inform user about matching credits |
24 | | - if "/" in model: |
25 | | - provider = model.split("/", 1)[0] |
26 | | - try: |
27 | | - weco_api_key = load_weco_api_key() |
28 | | - if not weco_api_key: |
29 | | - return model |
30 | | - resp = requests.get( |
31 | | - f"{__base_url__}/billing/balance", headers={"Authorization": f"Bearer {weco_api_key}"}, timeout=5 |
32 | | - ) |
33 | | - if resp.ok: |
34 | | - promo_credits = resp.json().get("promotional_credits", []) |
35 | | - matching = [g for g in promo_credits if g.get("provider") == provider] |
36 | | - if matching: |
37 | | - total = sum(g.get("remaining_credits", 0) for g in matching) |
38 | | - expires = matching[0].get("expires_at", "")[:10] |
39 | | - console.print( |
40 | | - f"[green]✅ Using {provider.capitalize()} promotional credits " |
41 | | - f"(${total:.2f} remaining, expires {expires})[/]" |
42 | | - ) |
43 | | - except Exception: |
44 | | - pass # Non-critical; don't block the run |
45 | | - return model |
46 | | - |
47 | | - # Bare model name — check for promotional credits |
48 | | - try: |
49 | | - weco_api_key = load_weco_api_key() |
50 | | - if not weco_api_key: |
51 | | - return model |
52 | | - resp = requests.get(f"{__base_url__}/billing/balance", headers={"Authorization": f"Bearer {weco_api_key}"}, timeout=5) |
53 | | - if not resp.ok: |
54 | | - return model |
55 | | - |
56 | | - promo_credits = resp.json().get("promotional_credits", []) |
57 | | - if not promo_credits: |
58 | | - return model |
59 | | - |
60 | | - # Check if any grant provider could serve this model |
61 | | - for grant in promo_credits: |
62 | | - grant_provider = grant.get("provider", "") |
63 | | - remaining = grant.get("remaining_credits", 0) |
64 | | - expires = grant.get("expires_at", "")[:10] |
65 | | - |
66 | | - # Suggest using the grant's provider |
67 | | - prefixed_model = f"{grant_provider}/{model}" |
68 | | - console.print( |
69 | | - f"\n[yellow]💡 You have ${remaining:.2f} in {grant_provider.capitalize()} credits (expires {expires}).[/]" |
70 | | - ) |
71 | | - use_credits = Confirm.ask(f" Use [bold]{prefixed_model}[/] to apply them?", default=True) |
72 | | - if use_credits: |
73 | | - console.print(f"[green]✅ Using {grant_provider.capitalize()} promotional credits[/]\n") |
74 | | - return prefixed_model |
75 | | - |
76 | | - except Exception: |
77 | | - pass # Non-critical; don't block the run |
78 | | - |
79 | | - return model |
80 | | - |
81 | | - |
82 | 13 | def handle_credits_command(args, console: Console) -> None: |
83 | 14 | """Handle the credits command and its subcommands.""" |
84 | 15 | # Ensure user is authenticated |
@@ -124,16 +55,6 @@ def check_balance(console: Console, auth_headers: dict) -> None: |
124 | 55 |
|
125 | 56 | console.print(table) |
126 | 57 |
|
127 | | - # Show promotional credits if any |
128 | | - promo_credits = data.get("promotional_credits", []) |
129 | | - if promo_credits: |
130 | | - console.print("\n[bold cyan]Promotional Credits:[/]") |
131 | | - for grant in promo_credits: |
132 | | - provider = grant.get("provider", "Unknown") |
133 | | - remaining = grant.get("remaining_credits", 0) |
134 | | - expires_at = grant.get("expires_at", "")[:10] # Date portion only |
135 | | - console.print(f" {provider.capitalize()} models: [green]${remaining:.2f}[/] (expires {expires_at})") |
136 | | - |
137 | 58 | except requests.exceptions.HTTPError as e: |
138 | 59 | if e.response.status_code == 401: |
139 | 60 | console.print("[bold red]Authentication failed. Please log in again with 'weco'.[/]") |
|
0 commit comments