From 546537e678e9af61e825bfea1063bbdac3cb3fac Mon Sep 17 00:00:00 2001 From: andyst-dev <150129844+andyst-dev@users.noreply.github.com> Date: Wed, 29 Jul 2026 11:56:06 +0200 Subject: [PATCH] Show billing account identity in desktop settings --- .../src/app/settings/billing/dev-fixtures.ts | 1 + .../src/app/settings/billing/index.test.tsx | 3 ++ .../src/app/settings/billing/index.tsx | 44 ++++++++++++++++++- apps/shared/src/billing-types.ts | 4 ++ tests/tui_gateway/test_billing_rpc.py | 15 +++++++ tui_gateway/server.py | 29 ++++++++++++ 6 files changed, 95 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/app/settings/billing/dev-fixtures.ts b/apps/desktop/src/app/settings/billing/dev-fixtures.ts index 91e3f9f953d56..b1471add27ec9 100644 --- a/apps/desktop/src/app/settings/billing/dev-fixtures.ts +++ b/apps/desktop/src/app/settings/billing/dev-fixtures.ts @@ -19,6 +19,7 @@ const current = ( }) export const todayBillingState = { + account_email: 'owner@sid5.example', auto_reload: { card: { kind: 'canonical' }, enabled: true, diff --git a/apps/desktop/src/app/settings/billing/index.test.tsx b/apps/desktop/src/app/settings/billing/index.test.tsx index 1836c03917b9e..bf2676754363e 100644 --- a/apps/desktop/src/app/settings/billing/index.test.tsx +++ b/apps/desktop/src/app/settings/billing/index.test.tsx @@ -83,6 +83,9 @@ describe('BillingSettings', () => { expect(await screen.findByText('$996.47')).toBeTruthy() expect(screen.getByText('Ultra · $200/mo')).toBeTruthy() + expect(screen.getByText('owner@sid5.example')).toBeTruthy() + expect(screen.getByText('sid-5 · OWNER')).toBeTruthy() + expect(screen.getByRole('button', { name: /Reconnect \/ switch account/ })).toBeTruthy() expect(screen.getByText('Visa •••• 3206')).toBeTruthy() expect( screen.getByText( diff --git a/apps/desktop/src/app/settings/billing/index.tsx b/apps/desktop/src/app/settings/billing/index.tsx index 040d37b3b43ec..c8699c55ef084 100644 --- a/apps/desktop/src/app/settings/billing/index.tsx +++ b/apps/desktop/src/app/settings/billing/index.tsx @@ -7,7 +7,7 @@ import { Progress } from '@/components/ui/progress' import { SegmentedControl } from '@/components/ui/segmented-control' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { Skeleton } from '@/components/ui/skeleton' -import { BarChart3, CreditCard, ExternalLink, Package, Wrench } from '@/lib/icons' +import { BarChart3, CreditCard, ExternalLink, Package, Users, Wrench } from '@/lib/icons' import { cn } from '@/lib/utils' import { useRouteEnumParam } from '../../hooks/use-route-enum-param' @@ -48,6 +48,7 @@ const BILLING_VIEWS = ['overview', 'plans'] as const type BillingSubView = (typeof BILLING_VIEWS)[number] const FEATURE_BILLING_INVOICES = false +const FALLBACK_PORTAL_URL = 'https://portal.nousresearch.com/billing' const BILLING_DEV_FIXTURE_NAMES = import.meta.env.DEV ? (Object.keys(billingDevFixtures) as BillingDevFixtureName[]) @@ -103,6 +104,45 @@ function NoticeCard({ notice }: { notice: BillingNoticeView }) { ) } +function BillingIdentityCard({ billing }: { billing?: BillingStateResponse }) { + if (!billing?.logged_in) { + return null + } + + const email = billing.account_email?.trim() + const org = billing.org_name?.trim() + + const orgLabel = org || 'Personal workspace' + const identityLabel = email || 'Account identity unavailable' + + const caption = email + ? `${orgLabel}${billing.role ? ` · ${billing.role}` : ''}` + : org + ? `${orgLabel} · email unavailable` + : 'Email and workspace unavailable — reconnect to refresh the account context.' + + return ( + + openExternal(billing.portal_url ?? FALLBACK_PORTAL_URL)} + size="sm" + type="button" + variant="outline" + > + Reconnect / switch account + + + } + below={
{caption}
} + description="The account and workspace used for the balance shown below." + title={identityLabel} + /> +
+ ) +} + // The payment method as it rides in the "Payment & credits" heading: the current // card (muted) plus a single underline text action (Update / Add payment method). function PaymentMethodAside({ row }: { row: BillingAccountRowView }) { @@ -514,6 +554,8 @@ function BillingSettingsContent({ + + {view.plan && ( setSubView('plans')} plan={view.plan} /> diff --git a/apps/shared/src/billing-types.ts b/apps/shared/src/billing-types.ts index 21b356e2f7d2f..bbfe7cce44ff5 100644 --- a/apps/shared/src/billing-types.ts +++ b/apps/shared/src/billing-types.ts @@ -193,6 +193,8 @@ export interface BillingAutoReload { } export interface BillingStateResponse { + /** Authenticated Nous user email for the SAME OAuth context used by billing.state. */ + account_email?: string | null auto_reload: BillingAutoReload | null balance_display: string balance_usd: string | null @@ -213,7 +215,9 @@ export interface BillingStateResponse { min_usd: string | null monthly_cap: BillingMonthlyCap | null ok: boolean + org_id?: string | null org_name: string | null + org_slug?: string | null portal_url: string | null role: string | null // Shared dollar usage model (two-bar view), embedded by the gateway so /topup diff --git a/tests/tui_gateway/test_billing_rpc.py b/tests/tui_gateway/test_billing_rpc.py index ee0bec5a82ce3..d56b6a9c5098b 100644 --- a/tests/tui_gateway/test_billing_rpc.py +++ b/tests/tui_gateway/test_billing_rpc.py @@ -15,6 +15,7 @@ import pytest import tui_gateway.server as srv import hermes_cli.nous_billing as nb +import hermes_cli.nous_account as na import agent.billing_view as bv from agent.billing_view import BillingState, CardInfo, MonthlyCap, PaymentMethodInfo @@ -33,6 +34,7 @@ def _call(method: str, params: dict) -> dict: def test_billing_state_serializes_decimals_as_strings(monkeypatch): state = BillingState( logged_in=True, + org_id="org_1", org_name="Acme", role="OWNER", balance_usd=Decimal("142.5"), @@ -52,8 +54,21 @@ def test_billing_state_serializes_decimals_as_strings(monkeypatch): portal_url="https://portal/billing?topup=open", ) monkeypatch.setattr(bv, "build_billing_state", lambda *a, **kw: state) + monkeypatch.setattr( + na, + "get_nous_portal_account_info", + lambda *a, **kw: na.NousPortalAccountInfo( + logged_in=True, + source="account_api", + fresh=True, + org_id="org_1", + email="owner@acme.example", + ), + ) res = _call("billing.state", {}) assert res["ok"] is True and res["logged_in"] is True + assert res["account_email"] == "owner@acme.example" + assert res["org_id"] == "org_1" # Money on the wire is STRING, not float/number. assert res["balance_usd"] == "142.5" assert res["balance_display"] == "$142.50" diff --git a/tui_gateway/server.py b/tui_gateway/server.py index 87568f7f9094f..c3c35424190db 100644 --- a/tui_gateway/server.py +++ b/tui_gateway/server.py @@ -8419,9 +8419,12 @@ def _serialize_billing_state(state) -> dict: "reload_to_display": format_money(ar.reload_to_usd), "card": card_out, } + account_email = _billing_account_email(state) return { "ok": True, "logged_in": state.logged_in, + "account_email": account_email, + "org_id": state.org_id, "org_name": state.org_name, "org_slug": state.org_slug, "role": state.role, @@ -8449,6 +8452,32 @@ def _serialize_billing_state(state) -> dict: } +def _billing_account_email(state) -> Optional[str]: + """Best-effort Nous user email for the billing.state OAuth context. + + The balance/org fields come from NAS through the same stored Nous OAuth + credential. Fetching the normalized account snapshot here keeps the rendered + Desktop identity in the same RPC generation as the balance, while failing + open on older/offline auth states instead of blocking the billing overview. + """ + if not getattr(state, "logged_in", False): + return None + try: + from hermes_cli.nous_account import get_nous_portal_account_info + + info = get_nous_portal_account_info(force_fresh=True) + except Exception: + return None + if not getattr(info, "logged_in", False): + return None + state_org_id = getattr(state, "org_id", None) + info_org_id = getattr(info, "org_id", None) + if state_org_id and info_org_id and state_org_id != info_org_id: + return None + email = getattr(info, "email", None) + return email.strip() if isinstance(email, str) and email.strip() else None + + def _usage_payload(state) -> dict: """Best-effort shared usage model for the /topup + /subscription overlay bars.