@retasc/cli 1.3.2 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/api.js CHANGED
@@ -19,6 +19,12 @@ const fns = {
19
19
  acceptInvite: makeFunctionReference("manage:acceptInvite"),
20
20
  listInvites: makeFunctionReference("manage:listInvites"),
21
21
  revokeInvite: makeFunctionReference("manage:revokeInvite"),
22
+ // Billing (RTSC-279). The CLI is a HUMAN surface (device-login JWT), so these are the
23
+ // very functions the Dash uses — already owner-gated, already org-scoped across every
24
+ // payment link. No key-based path needed here (that's the `billing_summary` MCP tool).
25
+ billingStatus: makeFunctionReference("manage:billingStatus"),
26
+ chargeHistory: makeFunctionReference("manage:chargeHistory"),
27
+ orgPayments: makeFunctionReference("manage:orgPayments"),
22
28
  };
23
29
  function client() {
24
30
  const cfg = loadConfig();
@@ -92,4 +98,7 @@ export const api = {
92
98
  acceptInvite: (args) => withAuth(() => client().mutation(fns.acceptInvite, args)),
93
99
  listInvites: (args) => withAuth(() => client().query(fns.listInvites, args)),
94
100
  revokeInvite: (args) => withAuth(() => client().mutation(fns.revokeInvite, args)),
101
+ billingStatus: (args) => withAuth(() => client().query(fns.billingStatus, args)),
102
+ chargeHistory: (args) => withAuth(() => client().query(fns.chargeHistory, args)),
103
+ orgPayments: (args) => withAuth(() => client().action(fns.orgPayments, args)),
95
104
  };
@@ -0,0 +1,121 @@
1
+ import { api } from "../api.js";
2
+ // RTSC-279 — `retasc billing`: the whole billing picture in the terminal, so you don't
3
+ // have to open the Dash to answer "what do I owe / what have I paid".
4
+ //
5
+ // The CLI is a HUMAN surface (device-login JWT), so this reuses the very functions the
6
+ // Dash renders — already owner-gated, and already ORG-scoped across every payment link
7
+ // the org has ridden (charges: RTSC-254, payments: RTSC-277). A link change therefore
8
+ // never hides prior history here either.
9
+ //
10
+ // Payments are a LIVE Xenarch read fanned per link, so they can be slow or unavailable;
11
+ // they're fetched last and degrade to a note rather than failing the whole command.
12
+ /** Metered billing is micro-USDC, so `toFixed(2)` hides real sub-cent amounts. Show up
13
+ * to 6 decimals, trim trailing zeros, keep a 2-decimal floor so money still reads right. */
14
+ function usd(n) {
15
+ if (n == null)
16
+ return "—";
17
+ const trimmed = n.toFixed(6).replace(/0+$/, "").replace(/\.$/, "");
18
+ const dot = trimmed.indexOf(".");
19
+ const decimals = dot === -1 ? 0 : trimmed.length - dot - 1;
20
+ return `$${decimals >= 2 ? trimmed : n.toFixed(2)}`;
21
+ }
22
+ function date(ms) {
23
+ return ms == null ? "—" : new Date(ms).toISOString().slice(0, 10);
24
+ }
25
+ function isoDate(s) {
26
+ return s ? s.slice(0, 10) : "—";
27
+ }
28
+ const row = (label, value) => console.log(` ${label.padEnd(22)}${value}`);
29
+ export async function billingAction(opts) {
30
+ // Resolve the org: explicit --org-id wins; otherwise use the only one you're in. With
31
+ // several, ask rather than guess — printing the wrong org's money is worse than an error.
32
+ let orgId = opts.orgId;
33
+ let orgLabel = "";
34
+ const me = await api.me();
35
+ if (!orgId) {
36
+ if (me.orgs.length === 0)
37
+ throw new Error("You're not a member of any org yet.");
38
+ if (me.orgs.length > 1) {
39
+ const list = me.orgs.map((o) => ` ${o.id} ${o.name}${o.slug ? ` (${o.slug})` : ""}`);
40
+ throw new Error(`Several orgs — pass --org-id <id>:\n${list.join("\n")}`);
41
+ }
42
+ orgId = me.orgs[0].id;
43
+ }
44
+ const org = me.orgs.find((o) => o.id === orgId);
45
+ orgLabel = org ? `${org.name}${org.slug ? ` (${org.slug})` : ""}` : String(orgId);
46
+ // Fail early with the actionable message rather than a raw FORBIDDEN from the server.
47
+ if (org && org.role !== "owner") {
48
+ throw new Error(`Billing is owner-only — your role in ${orgLabel} is "${org.role}".`);
49
+ }
50
+ const [status, charges] = await Promise.all([
51
+ api.billingStatus({ orgId: orgId }),
52
+ api.chargeHistory({ orgId: orgId }),
53
+ ]);
54
+ // Live Xenarch fan-out — slowest and most failure-prone, so it's last and non-fatal.
55
+ let payments = null;
56
+ let paymentsError = null;
57
+ try {
58
+ payments = await api.orgPayments({ orgId: orgId });
59
+ }
60
+ catch (e) {
61
+ paymentsError = String(e?.message ?? e);
62
+ }
63
+ if (opts.json) {
64
+ console.log(JSON.stringify({ org: orgLabel, status, charges, payments, paymentsError }, null, 2));
65
+ return;
66
+ }
67
+ const s = status;
68
+ const c = charges;
69
+ console.log(`\nBilling — ${orgLabel}\n`);
70
+ console.log("SUBSCRIPTION");
71
+ row("Status", s.billingExempt ? "comp — not billed" : `${s.status}${s.gated ? " (gated)" : ""}`);
72
+ row("Subscription", s.subscriptionId ?? "none yet");
73
+ if (s.spendingCapUsd != null) {
74
+ row("Budget", `${usd(s.spendingCapUsd)}${s.capPeriod ? ` / ${s.capPeriod.replace("per_", "")}` : ""}`);
75
+ }
76
+ if (s.collectionMessage)
77
+ row("⚠ Collection", s.collectionMessage);
78
+ console.log("\nMONEY");
79
+ row("Pending (owed now)", usd(s.accruedUsd));
80
+ row("Outstanding", usd(c.outstandingUsd));
81
+ row("Charged (lifetime)", usd(c.chargedUsd));
82
+ row("Collected (lifetime)", usd(c.collectedUsd));
83
+ if (s.creditsUsd > 0)
84
+ row("Credit remaining", usd(s.creditsUsd));
85
+ // Charges + payments span EVERY link; label the eras so a link change is visible
86
+ // rather than looking like history that vanished.
87
+ const eras = c.eras ?? [];
88
+ console.log(`\nCHARGES (${c.charges.length}${c.capped ? "+, capped" : ""})`);
89
+ if (c.charges.length === 0) {
90
+ console.log(" none yet");
91
+ }
92
+ else {
93
+ if (eras.length > 1)
94
+ console.log(` across ${eras.length} payment links`);
95
+ for (const ch of c.charges.slice(0, 10)) {
96
+ const era = eras.length > 1 ? ` [${ch.subscriptionId.slice(0, 8)}…]` : "";
97
+ console.log(` #${String(ch.chargeSeq).padEnd(4)} ${date(ch.bookedAt)} ${ch.status.padEnd(8)} ${usd(ch.amountUsd)}${era}`);
98
+ }
99
+ if (c.charges.length > 10)
100
+ console.log(` … ${c.charges.length - 10} more`);
101
+ }
102
+ console.log("\nPAYMENTS (confirmed on-chain)");
103
+ if (paymentsError) {
104
+ console.log(` unavailable — ${paymentsError}`);
105
+ }
106
+ else if (!payments || payments.payments.length === 0) {
107
+ console.log(" none yet");
108
+ }
109
+ else {
110
+ const ps = payments.payments;
111
+ const links = new Set(ps.map((p) => p.subscriptionId)).size;
112
+ if (links > 1)
113
+ console.log(` across ${links} payment links`);
114
+ for (const p of ps) {
115
+ const era = links > 1 ? ` [${p.subscriptionId.slice(0, 8)}…]` : "";
116
+ const block = p.blockNumber != null ? `block ${p.blockNumber}` : "";
117
+ console.log(` ${isoDate(p.createdAt)} ${usd(p.amountUsd).padEnd(10)} ${block}${era}`);
118
+ }
119
+ }
120
+ console.log("");
121
+ }
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ import { installGate } from "./commands/gate.js";
9
9
  import { claimAction } from "./commands/claim.js";
10
10
  import { bindAction } from "./commands/bind.js";
11
11
  import { doctorAction } from "./commands/doctor.js";
12
+ import { billingAction } from "./commands/billing.js";
12
13
  import { readLocalBinding, resolveBinding } from "./lib/binding.js";
13
14
  import { tidyAction, doneAction } from "./commands/tidy.js";
14
15
  import { runProxy } from "./proxy.js";
@@ -165,6 +166,15 @@ program
165
166
  .action(async () => {
166
167
  await doctorAction().catch(fail);
167
168
  });
169
+ program
170
+ .command("billing")
171
+ .description("Show the org's billing: subscription, what's owed now, and the charge + on-chain payment history across every payment link ever used (owner only).")
172
+ .option("--org-id <id>", "Which org (defaults to your only one).")
173
+ .option("--json", "Emit the raw payload instead of the summary.")
174
+ .action(async (opts) => {
175
+ requireLogin();
176
+ await billingAction({ orgId: opts.orgId, json: opts.json }).catch(fail);
177
+ });
168
178
  // --- org / project ---------------------------------------------------------
169
179
  const org = program.command("org").description("Manage orgs.");
170
180
  org
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@retasc/cli",
3
- "version": "1.3.2",
3
+ "version": "1.4.0",
4
4
  "description": "Retasc CLI — sign in with GitHub, create projects, mint agent API keys, and wire your agent to the Retasc MCP server in one command.",
5
5
  "type": "module",
6
6
  "bin": {