hermes-agent/ui-tui/src/gatewayTypes.ts
Siddharth Balyan 73cd8622f9
feat(billing): /billing terminal billing — interactive TUI + CLI client (#45449)
* feat(billing): nous_billing http client + BillingState core (phase 2b)

Phase 2b terminal-billing client foundation:
- hermes_cli/nous_billing.py: typed client for the 4 /api/billing/* endpoints
  (state/charge/poll/auto-top-up). Raises typed errors (BillingScopeRequired,
  BillingRateLimited, BillingAuthError) mapped from the live-verified contract;
  fail-open is the caller's job. Idempotency-Key enforced client-side.
- agent/billing_view.py: surface-agnostic BillingState core + Decimal money
  parsing (server emits decimal strings, not 2dp), fail-open builder,
  idempotency-key gen, custom-amount validation.
- 51 unit tests (decimal parse/format, payload tiering, error->exception
  matrix, fail-open, amount validation).

Plan: docs/plans/2026-06-13-001-phase-2b-terminal-billing-tui-plan.md

* feat(billing): billing:manage scope + lazy step-up re-auth (phase 2b)

- NOUS_BILLING_MANAGE_SCOPE constant.
- nous_token_has_billing_scope(): split-based scope check (no false-positive
  substring match).
- step_up_nous_billing_scope(): re-runs the device flow requesting
  billing:manage, reusing the held credential's portal/inference URLs + client_id
  (so a preview stays a preview), persists like _login_nous but WITHOUT the model
  picker. Returns True iff the minted token carries the scope (False when NAS
  silently downscopes a non-admin / unticked grant).

Lazy step-up (plan D-A): normal login path unchanged; 403 insufficient_scope
from a billing call triggers this. 7 unit tests.

* feat(billing): billing JSON-RPC methods for the TUI (phase 2b)

billing.state / charge / charge_status / auto_reload / step_up in
tui_gateway/server.py. Return STRUCTURED success envelopes (result.ok +
result.error=<code>) rather than JSON-RPC-level errors, so the Ink rpc() promise
always resolves and the TUI branches on the typed billing error code
(insufficient_scope, rate_limited, no_payment_method, …) to render the right
affordance. Money serialized as decimal STRINGS + display strings. charge mints
+ echoes an idempotency_key for retry reuse. 16 unit tests.

* feat(billing): /billing CLI handler + command registry (phase 2b)

- CommandDef("billing", subcommands=buy|auto-reload|limit), added to
  _SLACK_VIA_HERMES_ONLY so it routes via /hermes on Slack (keeps the 50-cap
  parity test green, same as /credits).
- cli.py::_show_billing + screen helpers: all 5 screens (overview, buy→confirm→
  poll, auto-reload, monthly-limit read-only). Reuses _prompt_text_input_modal /
  _prompt_text_input (D-C). Non-interactive (_app is None) renders text + portal
  deep-link, never prompts (R7). Decimal money end-to-end. 2s/5-min cancellable
  poll loop; 429/503 = retry not failure; settled = ledger truth. Lazy step-up on
  403 insufficient_scope. no_payment_method treated as mainline funnel-to-portal.
- 6 CLI tests; 156 command tests (incl. Slack/Telegram parity) green.

* feat(billing): /billing Ink TUI screens + tests (phase 2b)

- ui-tui/src/app/slash/commands/billing.ts: /billing TUI command covering all 5
  screens — overview (text), buy <amt> → ConfirmReq → charge → non-blocking 2s/
  5-min poll loop → settled/failed/timeout branches, auto-reload <below> <to> →
  ConfirmReq → PATCH, limit (read-only). Reuses the existing ConfirmReq overlay
  (D-C) — no bespoke component. Typed-error envelope branching: insufficient_scope
  arms the lazy step-up confirm; no_payment_method/rate_limited/cap funnel to
  portal. Client-side amount validation mirrors the server (bounds + 2dp).
- gatewayTypes.ts: Billing* response interfaces.
- registry.ts: register billingCommands.
- billingCommand.test.ts: 12 vitest cases (overview/gating/buy-confirm-poll-
  settled/no_payment_method/step-up/limit/auto-reload/validation).

TUI build green; 12/12 vitest pass; slash tests pass once @hermes/ink is built.

* docs(billing): scrub private cross-repo references

NAS is a private repo — remove all references to it from the public PR:
- drop the cross-repo planning doc (planning scaffolding, not a deliverable;
  the PR description documents the design)
- replace 'NAS' / 'PR #412 preview' mentions in code + test comments with
  generic 'the server' / 'a preview deployment'

* docs(billing): scrub final NAS reference in step-up docstring

* docs(billing): drop dangling plan-doc refs

The phase-2b plan doc was removed in the cross-repo scrub (300afcc0b)
but two module docstrings still pointed at it. Drop the dead refs.

* feat(billing): interactive /billing overlay + step-up UX, portal-URL & token fixes

Adds the interactive /billing TUI overlay and hardens the terminal-billing
client across CLI and TUI.

- TUI: full /billing overlay state machine (overview to buy to confirm,
  auto-reload, read-only monthly limit) reusing the existing confirm overlay.
- Step-up: surface the verification link in-transcript and open the browser
  via the TUI's own opener (the device flow runs in the headless gateway, so a
  printed URL was being dropped); run the step-up handler off the main loop and
  emit the link as an out-of-band event so the gateway stays responsive.
- Step-up copy is scope-accurate ("Billing permission granted") and re-checks
  /state so it never claims "enabled" when the org kill-switch is still off.
- Portal deep-links resolve to absolute URLs against the active portal base
  (the server emits them relative) - fixes a bare "/billing?topup=open" link.
- Billing calls refresh an expired access token via the stored refresh token
  instead of reporting a false "not logged in".
- Optimistic funnel: advise "set up a saved card on the portal" up front when
  no card is on file (advisory, not a hard gate).
- Token resolution is cached briefly so the 2s charge poll loop stops
  re-locking + re-reading the auth store on every tick; 401 re-resolves fresh.
- Remove the temporary demo-mode shims.

Validation: 87 Python billing tests, 88 TS tests (billing command + gateway
event handler), tsc clean, ink + ui-tui builds green.

* docs(billing): add /billing TUI screenshots for PR

* fix(cli): guard _last_invalidate on bare instances; update stale prompt-fallback test

The UI-invalidate throttle read self._last_invalidate unconditionally, which
raised AttributeError on HermesCLI instances built without __init__ (the
thread-safety test's object.__new__ shell). Guard the read with getattr.

The off-main-thread branch of _prompt_text_input was changed (#23185) to cancel
cleanly to None instead of falling back to a bare input() that would hang on the
slash-worker thread; the test still asserted the old direct-input fallback.
Update it to assert the current intended behavior: returns None, calls neither
run_in_terminal nor input(), and does not hang.
2026-06-19 01:53:32 +05:30

697 lines
19 KiB
TypeScript

import type { SessionInfo, SlashCategory, SubagentStatus, Usage } from './types.js'
export interface GatewaySkin {
banner_hero?: string
banner_logo?: string
branding?: Record<string, string>
colors?: Record<string, string>
help_header?: string
tool_prefix?: string
}
export interface GatewayCompletionItem {
display: string
meta?: string
text: string
}
export interface GatewayTranscriptMessage {
context?: string
name?: string
role: 'assistant' | 'system' | 'tool' | 'user'
text?: string
}
// ── Commands / completion ────────────────────────────────────────────
export interface CommandsCatalogResponse {
canon?: Record<string, string>
categories?: SlashCategory[]
pairs?: [string, string][]
skill_count?: number
sub?: Record<string, string[]>
warning?: string
}
export interface CompletionResponse {
items?: GatewayCompletionItem[]
replace_from?: number
}
export interface SlashExecResponse {
output?: string
warning?: string
}
// ── Credits / top-up ─────────────────────────────────────────────────
export interface CreditsViewResponse {
balance_lines: string[]
depleted: boolean
identity_line: string | null
logged_in: boolean
topup_url: string | null
}
// ── Terminal billing (Phase 2b) ──────────────────────────────────────
export interface BillingCardInfo {
brand: string
last4: string
masked: string
}
export interface BillingMonthlyCap {
is_default_ceiling: boolean
limit_display: string
limit_usd: string | null
spent_display: string
spent_this_month_usd: string | null
}
export interface BillingAutoReload {
enabled: boolean
reload_to_display: string
reload_to_usd: string | null
threshold_display: string
threshold_usd: string | null
}
export interface BillingStateResponse {
auto_reload: BillingAutoReload | null
balance_display: string
balance_usd: string | null
can_charge: boolean
card: BillingCardInfo | null
charge_presets: string[]
charge_presets_display: string[]
cli_billing_enabled: boolean
error?: string | null
is_admin: boolean
logged_in: boolean
max_usd: string | null
min_usd: string | null
monthly_cap: BillingMonthlyCap | null
ok: boolean
org_name: string | null
portal_url: string | null
role: string | null
}
/**
* Raw error payload echoed from the server (`_serialize_billing_error`). Carries
* the extra fields a few error codes attach — notably `remainingUsd` on
* `monthly_cap_exceeded` — so the client can render the same detail the CLI does.
*/
export interface BillingErrorPayload {
isDefaultCeiling?: boolean
remainingUsd?: string
}
export interface BillingChargeResponse {
charge_id?: string
error?: string
idempotency_key?: string
message?: string
ok: boolean
payload?: BillingErrorPayload
portal_url?: string | null
retry_after?: number | null
}
export interface BillingChargeStatusResponse {
amount_usd?: string | null
error?: string
message?: string
ok: boolean
payload?: BillingErrorPayload
portal_url?: string | null
reason?: string | null
retry_after?: number | null
settled_at?: string | null
status?: string
}
export interface BillingMutationResponse {
error?: string
granted?: boolean
message?: string
ok: boolean
payload?: BillingErrorPayload
portal_url?: string | null
retry_after?: number | null
}
export type CommandDispatchResponse =
| { output?: string; type: 'exec' | 'plugin' }
| { target: string; type: 'alias' }
| { message?: string; name: string; type: 'skill' }
| { message: string; notice?: string; type: 'send' }
| { message: string; notice?: string; type: 'prefill' }
// ── Config ───────────────────────────────────────────────────────────
export interface ConfigDisplayConfig {
bell_on_complete?: boolean
busy_input_mode?: string
details_mode?: string
inline_diffs?: boolean
mouse_tracking?: boolean | null | number | string
sections?: Record<string, string>
show_cost?: boolean
show_reasoning?: boolean
streaming?: boolean
thinking_mode?: string
/**
* Nudge the user toward the /agents spawn-tree dashboard the first time a
* turn starts delegating, via a one-time transient activity hint. Opens
* nothing — just advertises the command. Default true.
*/
tui_agents_nudge?: boolean
tui_auto_resume_recent?: boolean
tui_compact?: boolean
/** Legacy alias for display.mouse_tracking. */
tui_mouse?: boolean | null | number | string
// Forward-compat: backend may send styles this client doesn't know yet —
// `normalizeIndicatorStyle` falls back to 'kaomoji' for those — but the
// wire type is documented as `string` so consumers don't get a false
// narrowing-and-autocomplete contract on a value that requires runtime
// validation anyway.
tui_status_indicator?: string
tui_statusbar?: 'bottom' | 'off' | 'on' | 'top' | boolean
}
export interface ConfigVoiceConfig {
// Raw `yaml.safe_load()` value from config; may be non-string if hand-edited.
// Callers must normalize/validate at runtime (parseVoiceRecordKey()).
record_key?: unknown
}
export interface ConfigFullResponse {
config?: { display?: ConfigDisplayConfig; voice?: ConfigVoiceConfig; paste_collapse_threshold?: number; paste_collapse_char_threshold?: number }
}
export interface ConfigMtimeResponse {
mtime?: number
}
export interface ConfigGetValueResponse {
display?: string
home?: string
value?: string
}
export interface ConfigSetResponse {
confirm_message?: string
confirm_required?: boolean
credential_warning?: string
history_reset?: boolean
info?: SessionInfo
value?: string
warning?: string
}
export interface SetupStatusResponse {
provider_configured?: boolean
}
// ── Session lifecycle ────────────────────────────────────────────────
export interface SessionCreateResponse {
info?: SessionInfo & { config_warning?: string; credential_warning?: string }
session_id: string
}
export interface SessionResumeResponse {
inflight?: null | SessionInflightTurn
info?: SessionInfo
message_count?: number
messages: GatewayTranscriptMessage[]
resumed?: string
running?: boolean
session_id: string
started_at?: number
status?: LiveSessionStatus
}
export type LiveSessionStatus = 'idle' | 'starting' | 'waiting' | 'working'
export interface SessionActiveItem {
current?: boolean
id: string
last_active?: number
message_count?: number
model?: string
preview?: string
session_key?: string
started_at?: number
status: LiveSessionStatus
title?: string
}
export interface SessionActiveListResponse {
sessions?: SessionActiveItem[]
}
export interface SessionInflightTurn {
assistant?: string
streaming?: boolean
user?: string
}
export interface SessionActivateResponse {
inflight?: null | SessionInflightTurn
info?: SessionInfo
message_count?: number
messages: GatewayTranscriptMessage[]
running?: boolean
session_id: string
session_key?: string
started_at?: number
status?: LiveSessionStatus
}
export interface SessionListItem {
id: string
message_count: number
preview: string
source?: string
started_at: number
title: string
}
export interface SessionListResponse {
sessions?: SessionListItem[]
}
export interface SessionDeleteResponse {
deleted: string
}
export interface SessionMostRecentResponse {
session_id?: null | string
source?: string
started_at?: number
title?: string
}
export interface SessionTitleResponse {
pending?: boolean
session_key?: string
title?: string
}
export interface SessionSaveResponse {
file?: string
}
export interface SessionUndoResponse {
removed?: number
}
export interface SessionUsageResponse {
cache_read?: number
cache_write?: number
calls?: number
compressions?: number
context_max?: number
context_percent?: number
context_used?: number
cost_status?: 'estimated' | 'exact'
cost_usd?: number
credits_lines?: string[]
input?: number
model?: string
output?: number
total?: number
}
export interface SessionStatusResponse {
output?: string
}
export interface SessionCompressResponse {
after_messages?: number
after_tokens?: number
before_messages?: number
before_tokens?: number
info?: SessionInfo
messages?: GatewayTranscriptMessage[]
removed?: number
summary?: {
headline?: string
noop?: boolean
note?: null | string
token_line?: string
}
usage?: Usage
}
export interface SessionBranchResponse {
session_id?: string
title?: string
}
export interface SessionCloseResponse {
closed?: boolean
ok?: boolean
}
export interface SessionInterruptResponse {
ok?: boolean
}
export interface SessionSteerResponse {
status?: 'queued' | 'rejected'
text?: string
}
// ── Prompt / submission ──────────────────────────────────────────────
export interface PromptSubmitResponse {
ok?: boolean
}
export interface BackgroundStartResponse {
task_id?: string
}
export interface ClarifyRespondResponse {
ok?: boolean
}
export interface ApprovalRespondResponse {
ok?: boolean
}
export interface SudoRespondResponse {
ok?: boolean
}
export interface SecretRespondResponse {
ok?: boolean
}
// ── Shell / clipboard / input ────────────────────────────────────────
export interface ShellExecResponse {
code: number
stderr?: string
stdout?: string
}
export interface ClipboardPasteResponse {
attached?: boolean
count?: number
height?: number
message?: string
token_estimate?: number
width?: number
}
export interface InputDetectDropResponse {
height?: number
is_image?: boolean
matched?: boolean
name?: string
text?: string
token_estimate?: number
width?: number
}
export interface TerminalResizeResponse {
ok?: boolean
}
// ── Image attach ─────────────────────────────────────────────────────
export interface ImageAttachResponse {
height?: number
name?: string
remainder?: string
token_estimate?: number
width?: number
}
// ── Voice ────────────────────────────────────────────────────────────
export interface VoiceToggleResponse {
audio_available?: boolean
available?: boolean
details?: string
enabled?: boolean
record_key?: string
stt_available?: boolean
tts?: boolean
}
export interface VoiceRecordResponse {
status?: 'busy' | 'recording' | 'stopped'
text?: string
}
// ── Tools (TS keeps configure since it resets local history) ─────────
export interface ToolsConfigureResponse {
changed?: string[]
enabled_toolsets?: string[]
info?: SessionInfo
missing_servers?: string[]
reset?: boolean
unknown?: string[]
}
// ── Model picker ─────────────────────────────────────────────────────
export interface ModelOptionProvider {
auth_type?: string
authenticated?: boolean
is_current?: boolean
key_env?: string
models?: string[]
name: string
slug: string
total_models?: number
warning?: string
}
export interface ModelOptionsResponse {
model?: string
provider?: string
providers?: ModelOptionProvider[]
}
// ── MCP ──────────────────────────────────────────────────────────────
export interface ReloadMcpResponse {
status?: string
message?: string
}
export interface ReloadEnvResponse {
updated?: number
}
export interface ProcessStopResponse {
killed?: number
}
export interface BrowserManageResponse {
connected?: boolean
messages?: string[]
url?: string
}
export interface RollbackCheckpoint {
hash: string
message?: string
timestamp?: string
}
export interface RollbackListResponse {
checkpoints?: RollbackCheckpoint[]
enabled?: boolean
}
export interface RollbackDiffResponse {
diff?: string
rendered?: string
stat?: string
}
export interface RollbackRestoreResponse {
error?: string
history_removed?: number
message?: string
reason?: string
restored_to?: string
success?: boolean
}
// ── Subagent events ──────────────────────────────────────────────────
export interface SubagentEventPayload {
api_calls?: number
cost_usd?: number
depth?: number
duration_seconds?: number
files_read?: string[]
files_written?: string[]
goal: string
input_tokens?: number
iteration?: number
model?: string
output_tail?: { is_error?: boolean; preview?: string; tool?: string }[]
output_tokens?: number
parent_id?: null | string
reasoning_tokens?: number
status?: SubagentStatus
subagent_id?: string
summary?: string
task_count?: number
task_index: number
text?: string
tool_count?: number
tool_name?: string
tool_preview?: string
toolsets?: string[]
}
// ── Delegation control RPCs ──────────────────────────────────────────
export interface DelegationStatusResponse {
active?: {
depth?: number
goal?: string
model?: null | string
parent_id?: null | string
started_at?: number
status?: string
subagent_id?: string
tool_count?: number
}[]
max_concurrent_children?: number
max_spawn_depth?: number
paused?: boolean
}
export interface DelegationPauseResponse {
paused?: boolean
}
export interface SubagentInterruptResponse {
found?: boolean
subagent_id?: string
}
// ── Spawn-tree snapshots ─────────────────────────────────────────────
export interface SpawnTreeListEntry {
count: number
finished_at?: number
label?: string
path: string
session_id?: string
started_at?: number | null
}
export interface SpawnTreeListResponse {
entries?: SpawnTreeListEntry[]
}
export interface SpawnTreeLoadResponse {
finished_at?: number
label?: string
session_id?: string
started_at?: null | number
subagents?: unknown[]
}
export type GatewayEvent =
| { payload?: { skin?: GatewaySkin }; session_id?: string; type: 'gateway.ready' }
| { payload?: GatewaySkin; session_id?: string; type: 'skin.changed' }
| { payload: SessionInfo; session_id?: string; type: 'session.info' }
| { payload?: { text?: string }; session_id?: string; type: 'thinking.delta' }
| { payload?: undefined; session_id?: string; type: 'message.start' }
| { payload?: { kind?: string; text?: string }; session_id?: string; type: 'status.update' }
| {
payload?: {
id?: string
key?: string
kind?: 'sticky' | 'ttl'
level?: 'error' | 'info' | 'success' | 'warn'
text?: string
ttl_ms?: null | number
}
session_id?: string
type: 'notification.show'
}
| { payload?: { key?: string }; session_id?: string; type: 'notification.clear' }
| {
payload: { user_code?: string; verification_url: string }
session_id?: string
type: 'billing.step_up.verification'
}
| { payload?: { state?: 'idle' | 'listening' | 'transcribing' }; session_id?: string; type: 'voice.status' }
| { payload?: { no_speech_limit?: boolean; text?: string }; session_id?: string; type: 'voice.transcript' }
| { payload: { line: string }; session_id?: string; type: 'gateway.stderr' }
| {
payload?: { level?: 'info' | 'warn' | 'error'; message?: string }
session_id?: string
type: 'browser.progress'
}
| {
payload?: { cwd?: string; python?: string; stderr_tail?: string }
session_id?: string
type: 'gateway.start_timeout'
}
| { payload?: { preview?: string }; session_id?: string; type: 'gateway.protocol_error' }
| { payload?: { text?: string; verbose?: boolean }; session_id?: string; type: 'reasoning.delta' | 'reasoning.available' }
| { payload: { name?: string; preview?: string }; session_id?: string; type: 'tool.progress' }
| { payload: { name?: string }; session_id?: string; type: 'tool.generating' }
| {
payload: { args_text?: string; context?: string; name?: string; tool_id: string; todos?: unknown[] }
session_id?: string
type: 'tool.start'
}
| {
payload: {
duration_s?: number
error?: string
inline_diff?: string
name?: string
result_text?: string
summary?: string
tool_id: string
todos?: unknown[]
}
session_id?: string
type: 'tool.complete'
}
| {
payload: { choices: string[] | null; question: string; request_id: string }
session_id?: string
type: 'clarify.request'
}
| {
payload: { allow_permanent?: boolean; command: string; description: string }
session_id?: string
type: 'approval.request'
}
| { payload: { request_id: string }; session_id?: string; type: 'sudo.request' }
| { payload: { env_var: string; prompt: string; request_id: string }; session_id?: string; type: 'secret.request' }
| { payload: { task_id: string; text: string }; session_id?: string; type: 'background.complete' }
| { payload?: { text?: string }; session_id?: string; type: 'review.summary' }
| { payload: SubagentEventPayload; session_id?: string; type: 'subagent.spawn_requested' }
| { payload: SubagentEventPayload; session_id?: string; type: 'subagent.start' }
| { payload: SubagentEventPayload; session_id?: string; type: 'subagent.thinking' }
| { payload: SubagentEventPayload; session_id?: string; type: 'subagent.tool' }
| { payload: SubagentEventPayload; session_id?: string; type: 'subagent.progress' }
| { payload: SubagentEventPayload; session_id?: string; type: 'subagent.complete' }
| { payload: { rendered?: string; text?: string }; session_id?: string; type: 'message.delta' }
| {
payload?: { reasoning?: string; rendered?: string; text?: string; usage?: Usage }
session_id?: string
type: 'message.complete'
}
| { payload?: { message?: string }; session_id?: string; type: 'error' }