mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-06-30 11:52:04 +00:00
* feat(billing): /usage → portal top-up browser handoff
Add the terminal side of the billing slice (phase 2a): start a top-up by
throwing the user to the portal billing page with the top-up modal open. The
terminal does not confirm, poll, or track payment — checkout completes in the
browser and the next /usage shows the new balance.
- nous_account.py: parse organisation.slug/name from /api/oauth/account into
NousPortalAccountInfo; add nous_portal_topup_url() building the org-pinned
{base}/orgs/{slug}/billing?topup=open with a null-slug fallback to the legacy
{base}/billing?topup=open (never /orgs/None/...).
- portal_cli.py: 'hermes portal topup' — fresh account fetch, identity line
(Topping up as <email> / org <name>), browser open with printed-URL fallback,
no-wait closing copy. No polling/confirmation (deferred to 2b).
- account_usage.py: the shared /usage credits block now links the org-pinned
top-up URL (auto-opens the modal) + points to the command.
Depends on NAS #409 (organisation.slug/name + ?topup=open). Do not merge until
that is live on the target env; until then /api/oauth/account returns
organisation: { id } only and the URL falls back to legacy.
* feat(billing): /credits command for balance + top-up handoff
Replace the standalone `hermes portal topup` subcommand with an in-session
/credits slash command — a focused money surface (balance in, top-up out) that
works in the CLI, TUI, and every messaging platform from one registry entry.
- commands.py: register /credits (Info category). Slack is at its 50-slash cap,
so /credits is routed via /hermes credits on Slack only (new
_SLACK_VIA_HERMES_ONLY set) to avoid clamping a canonical command off the
native list and breaking Telegram parity; native everywhere else.
- account_usage.py: build_credits_view() — one portal fetch → balance lines +
identity line + org-pinned top-up URL + depleted flag, consumed by all
surfaces. Reuses the same snapshot/URL builder as /usage so numbers match.
- cli.py: _show_credits() — balance block + identity line + 3-button panel
(Open top-up / Copy link / Cancel) via the existing prompt_toolkit modal.
ASK, never auto-launch; headless falls back to printing the URL.
- gateway/slash_commands.py: _handle_credits_command() — renders the block +
tappable top-up URL + no-wait copy; works on button and plain-text platforms.
- /usage credits line now points to /credits.
- Retire `hermes portal topup` (portal_cli.py back to baseline); the engine
(slug/name parse + nous_portal_topup_url) stays as the shared core.
No polling, no payment confirmation (billing phase 2a). Depends on NAS #409.
* fix(credits): /credits works in the TUI slash-worker (non-interactive)
In the TUI, /credits runs in the slash-worker subprocess where there is no
live prompt_toolkit app and stdin is the JSON-RPC pipe. _show_credits called
the 3-button modal unconditionally, which fell back to reading stdin →
exception → slash.exec rejected → the command produced no output (only the
pre-existing 'Credit access paused' banner showed).
- _show_credits: when self._app is None (TUI worker / piped / non-interactive),
render the text variant — balance block + tappable top-up URL + no-wait line,
same affordance as the messaging surfaces — and skip the modal entirely. The
3-button panel still renders in the interactive CLI.
- Depleted banner copy: 'run /usage for balance' → 'run /credits to top up'
now that /credits is the dedicated money surface (+ tests).
- Regression tests: _show_credits with self._app=None renders text and never
invokes the modal; logged-out path.
* feat(tui): credits.view RPC for the /credits tappable top-up button
Add a credits.view JSON-RPC method returning the structured CreditsView
(logged_in, balance_lines, identity_line, topup_url, depleted) so the TUI can
render a clickable <Link> top-up button instead of plain text. Account-
independent (portal fetch gated on a logged-in Nous account), fail-open to
{logged_in: false} on any hiccup. Mirrors session.usage's credits-block pattern.
Frontend (TUI-local /credits command + Ink component) lands separately.
* feat(tui): /credits command with keyboard-driven top-up confirm
TUI-local /credits: fetches the structured balance via the credits.view RPC,
prints the balance + identity + top-up URL, then arms the EXISTING confirm
overlay (Enter = open top-up in browser via openExternalUrl, Esc = cancel).
Reuses ConfirmReq — no new overlay component/state/input handler. Headless
(openExternalUrl returns false) falls back to printing the URL.
- gatewayTypes.ts: CreditsViewResponse.
- commands/credits.ts: the command (mirrors /status's rpc+guarded pattern).
- registry.ts: register creditsCommands.
- test: balance+overlay armed, headless fallback, no-url, logged-out (4 cases).
Matches the CLI /credits 'Enter to open' affordance. Phase 2a: no polling.
165 lines
5 KiB
Python
165 lines
5 KiB
Python
"""Tests for build_nous_credits_snapshot (L6-A, magnitudes-only)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from agent.account_usage import build_nous_credits_snapshot
|
|
from hermes_cli.nous_account import (
|
|
NousPaidServiceAccessInfo,
|
|
NousPortalAccountInfo,
|
|
NousPortalSubscriptionInfo,
|
|
)
|
|
|
|
|
|
def _account(**kwargs) -> NousPortalAccountInfo:
|
|
kwargs.setdefault("logged_in", True)
|
|
kwargs.setdefault("source", "account_api")
|
|
kwargs.setdefault("fresh", True)
|
|
return NousPortalAccountInfo(**kwargs)
|
|
|
|
|
|
def _all_lines(snapshot) -> list[str]:
|
|
return list(snapshot.details)
|
|
|
|
|
|
def test_healthy():
|
|
info = _account(
|
|
paid_service_access=True,
|
|
paid_service_access_info=NousPaidServiceAccessInfo(
|
|
subscription_credits_remaining=18.0,
|
|
purchased_credits_remaining=12.34,
|
|
total_usable_credits=30.34,
|
|
),
|
|
subscription=NousPortalSubscriptionInfo(
|
|
plan="Pro",
|
|
current_period_end="2026-07-01",
|
|
),
|
|
)
|
|
snap = build_nous_credits_snapshot(info)
|
|
assert snap is not None
|
|
assert snap.available is True
|
|
assert snap.plan == "Pro"
|
|
assert snap.provider == "nous"
|
|
assert snap.title == "Nous credits"
|
|
blob = "\n".join(_all_lines(snap))
|
|
assert "$18.00" in blob
|
|
assert "$12.34" in blob
|
|
assert "$30.34" in blob
|
|
assert "Renews: 2026-07-01" in blob
|
|
assert "/billing" in blob
|
|
# money-rule: magnitudes-only, never a percentage
|
|
assert "%" not in blob
|
|
|
|
|
|
def test_money_rule_no_percent():
|
|
info = _account(
|
|
paid_service_access=True,
|
|
paid_service_access_info=NousPaidServiceAccessInfo(
|
|
subscription_credits_remaining=18.0,
|
|
purchased_credits_remaining=12.34,
|
|
total_usable_credits=30.34,
|
|
),
|
|
subscription=NousPortalSubscriptionInfo(plan="Pro"),
|
|
)
|
|
snap = build_nous_credits_snapshot(info)
|
|
assert snap is not None
|
|
for line in snap.details:
|
|
assert "%" not in line
|
|
|
|
|
|
def test_depleted():
|
|
info = _account(
|
|
paid_service_access=False,
|
|
paid_service_access_info=NousPaidServiceAccessInfo(
|
|
subscription_credits_remaining=0.0,
|
|
purchased_credits_remaining=0.0,
|
|
total_usable_credits=0.0,
|
|
),
|
|
subscription=NousPortalSubscriptionInfo(plan="Pro"),
|
|
)
|
|
snap = build_nous_credits_snapshot(info)
|
|
assert snap is not None
|
|
blob = "\n".join(_all_lines(snap))
|
|
assert "access depleted" in blob
|
|
assert "/billing" in blob
|
|
|
|
|
|
def test_purchased_only():
|
|
info = _account(
|
|
paid_service_access=True,
|
|
paid_service_access_info=NousPaidServiceAccessInfo(
|
|
subscription_credits_remaining=None,
|
|
purchased_credits_remaining=30.0,
|
|
total_usable_credits=30.0,
|
|
),
|
|
subscription=None,
|
|
)
|
|
snap = build_nous_credits_snapshot(info)
|
|
assert snap is not None
|
|
blob = "\n".join(_all_lines(snap))
|
|
assert "Subscription credits" not in blob
|
|
assert "Top-up credits: $30.00" in blob
|
|
assert snap.plan is None
|
|
|
|
|
|
def test_logged_out():
|
|
info = _account(
|
|
logged_in=False,
|
|
paid_service_access=True,
|
|
paid_service_access_info=NousPaidServiceAccessInfo(
|
|
total_usable_credits=10.0,
|
|
),
|
|
)
|
|
assert build_nous_credits_snapshot(info) is None
|
|
|
|
|
|
def test_none():
|
|
assert build_nous_credits_snapshot(None) is None
|
|
|
|
|
|
def test_never_raises_empty():
|
|
info = _account(
|
|
paid_service_access=True,
|
|
paid_service_access_info=None,
|
|
subscription=None,
|
|
)
|
|
# No usable numbers and not depleted -> None, without raising.
|
|
assert build_nous_credits_snapshot(info) is None
|
|
|
|
|
|
def test_topup_line_is_org_pinned_when_slug_present():
|
|
info = _account(
|
|
portal_base_url="https://portal.example.test",
|
|
org_slug="acme",
|
|
org_name="Acme Inc",
|
|
paid_service_access=True,
|
|
paid_service_access_info=NousPaidServiceAccessInfo(
|
|
purchased_credits_remaining=30.0,
|
|
total_usable_credits=30.0,
|
|
),
|
|
subscription=None,
|
|
)
|
|
snap = build_nous_credits_snapshot(info)
|
|
assert snap is not None
|
|
blob = "\n".join(_all_lines(snap))
|
|
# The /usage top-up link auto-opens the modal and is org-pinned.
|
|
assert "https://portal.example.test/orgs/acme/billing?topup=open" in blob
|
|
assert "/credits" in blob
|
|
|
|
|
|
def test_topup_line_falls_back_to_legacy_when_slug_null():
|
|
info = _account(
|
|
portal_base_url="https://portal.example.test",
|
|
org_slug=None,
|
|
paid_service_access=True,
|
|
paid_service_access_info=NousPaidServiceAccessInfo(
|
|
purchased_credits_remaining=30.0,
|
|
total_usable_credits=30.0,
|
|
),
|
|
subscription=None,
|
|
)
|
|
snap = build_nous_credits_snapshot(info)
|
|
assert snap is not None
|
|
blob = "\n".join(_all_lines(snap))
|
|
# Null slug → legacy page (which forwards the param); never /orgs/None/...
|
|
assert "https://portal.example.test/billing?topup=open" in blob
|
|
assert "/orgs/" not in blob
|