@letterstory/cli 0.175.28 → 0.176.1

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/lib/cli.mjs CHANGED
@@ -80,7 +80,7 @@ import {
80
80
  import { fetchParkedSections, withoutParked } from "./parked.mjs";
81
81
 
82
82
  // Matches cli/package.json; publish-cli.yml rewrites both to the published version.
83
- export const VERSION = "0.175.28";
83
+ export const VERSION = "0.176.1";
84
84
 
85
85
  /** The exit code for a gate refusal (CARD_REQUIRED, PAYMENT_FAILED, OUT_OF_TOKENS): 1 is a plain error. */
86
86
  export const EXIT_PAYMENT_REQUIRED = 3;
@@ -1128,6 +1128,8 @@ Admin (STAFF key — --staff-key / LETTERSTORY_STAFF_API_KEY; acts before an org
1128
1128
  nightly reconcile, refusals, signups, grants
1129
1129
  admin billing free preflight [--json] Who the paywall stops the moment it is enforced:
1130
1130
  out of tokens, low, paying but covered, undecided
1131
+ admin billing org test <org-id> --why "…" [--off] Mark an org a test org (never gated or billed), or with
1132
+ --off not one; kept with who and why. Not for a payer.
1131
1133
  admin billing emails [--bounced] [--org <org-id>] [--limit N]
1132
1134
  The invoice and receipt emails we sent for Lago and
1133
1135
  what became of each, bounced first (--bounced: only
@@ -113,6 +113,7 @@ async function staffRequest(ctx, method, path, body) {
113
113
  // The server's code and sentence, for a command that prints a refusal as it was said.
114
114
  err.code = payload?.code ?? null;
115
115
  err.serverMessage = payload?.error ?? null;
116
+ err.details = payload?.details ?? null;
116
117
  throw err;
117
118
  }
118
119
  return payload ?? {};
@@ -4551,7 +4552,7 @@ async function cmdAdminSpend(ctx) {
4551
4552
  const result = await staffRequest(ctx, "GET", `${SPEND}/usage-margin${params.size ? `?${params}` : ""}`);
4552
4553
  printResult(io, flags, result, (m) =>
4553
4554
  [
4554
- `${m.range.from} → ${m.range.to}: ${m.totals.billableEvents} billable of ${m.totals.events} events · ${m.totals.tokens ?? "?"} tokens (${m.rateCards?.length > 1 ? `${m.rateCards.map((c) => c.version).join(" + ")}, each day on its own card` : (m.rateCard?.version ?? "no card")}) · cost ${usd(m.totals.cost?.totalUsd)} · margin ${m.totals.marginUsd === null ? "?" : usd(m.totals.marginUsd)}`,
4555
+ `${m.range.from} → ${m.range.to}: ${m.totals.billableEvents} billable of ${m.totals.events} events · ${m.totals.tokens ?? "?"} tokens (${m.rateCards?.length > 1 ? `${m.rateCards.map((c) => (c.effectiveFrom ? `${c.version} from ${c.effectiveFrom.slice(0, 10)}` : c.version)).join(" + ")}, each day on its own card` : (m.rateCard?.version ?? "no card")}) · cost ${usd(m.totals.cost?.totalUsd)} · margin ${m.totals.marginUsd === null ? "?" : usd(m.totals.marginUsd)}`,
4555
4556
  "",
4556
4557
  ...m.rows.map(
4557
4558
  (r) =>
@@ -5745,6 +5746,7 @@ async function cmdAdminRecipes(ctx) {
5745
5746
 
5746
5747
  const LAGO = "/api/superadmin/billing/lago";
5747
5748
  const LAGO_USAGE = `Usage: admin billing desk [--json] the Billing desk: switches, failed payments, reconcile, refusals, grants
5749
+ admin billing org test <org-id> --why "…" [--off] mark an org a test org (or not), kept with who and why
5748
5750
  admin billing lago <command>
5749
5751
  catalog [--apply] [--include-drafts] what the plan catalog would change in Lago; --apply makes it so
5750
5752
  provision <org-id> --plan starter [--ratio 4] [--why "…" above 4x] [--complimentary 20000] [--stripe-customer cus_…] [--start 2026-11-02T00:00:00Z] [--review 2027-05-01]
@@ -5803,6 +5805,9 @@ async function cmdAdminBillingLegacy(ctx) {
5803
5805
  decision,
5804
5806
  why,
5805
5807
  ...(tokens ? { tokens: Number(tokens.replace(/[,_]/g, "")) } : {}),
5808
+ }).catch((err) => {
5809
+ if (err?.code !== "DECIDE_FAILED") throw err;
5810
+ throw new CliError(err.serverMessage);
5806
5811
  });
5807
5812
  const words = { keep: "kept legacy", free: "on free tokens", test: "marked a test org" };
5808
5813
  printResult(io, flags, r, (x) => `${orgId}: ${words[x.decision]}${x.grantRef ? ` (${x.grantRef})` : ""}.`);
@@ -5965,6 +5970,13 @@ async function cmdAdminBillingFree(ctx) {
5965
5970
  tokens,
5966
5971
  why,
5967
5972
  ...(key ? { key } : {}),
5973
+ }).catch((err) => {
5974
+ // Didn't go through: the server's sentence and the exact retry, under the same key.
5975
+ if (err?.code !== "GRANT_FAILED") throw err;
5976
+ const retryKey = err.details?.key ?? key;
5977
+ throw new CliError(
5978
+ `${err.serverMessage}\nRetry: letterstory admin billing free grant ${orgId} ${tokens} --why ${JSON.stringify(why)}${retryKey ? ` --key ${retryKey}` : ""}`
5979
+ );
5968
5980
  });
5969
5981
  printResult(io, flags, r, (x) => {
5970
5982
  const what = x.kind === "wallet" ? "as a one-off wallet on Lago" : "as a free-token grant";
@@ -6115,6 +6127,26 @@ async function cmdAdminBillingEmails(ctx) {
6115
6127
  return 0;
6116
6128
  }
6117
6129
 
6130
+ const ORG_TEST_USAGE = `Usage: admin billing org test <org-id> --why "…" [--off]
6131
+ Mark an org a test org (never gated or billed, out of the fleet's customer numbers), or with --off not
6132
+ one any more. Kept with who and why, posted to #billing. Refused for an org that pays us. Billing admins.`;
6133
+
6134
+ /** admin billing org test <org-id> --why "…" [--off] — setOrgTest (lib/billing/org-test.ts). */
6135
+ async function cmdAdminBillingOrg(ctx) {
6136
+ const { flags, positionals, io } = ctx;
6137
+ const orgId = positionals[2];
6138
+ if (positionals[1] !== "test" || !orgId) throw new CliError(ORG_TEST_USAGE);
6139
+ const why = flagStr(flags.why);
6140
+ if (!why) throw new CliError(`Say why (--why "…"): it is kept with the change.\n${ORG_TEST_USAGE}`);
6141
+ const r = await staffPost(ctx, "/api/superadmin/billing/orgs/test", { orgId, test: !flagBool(flags.off), why });
6142
+ printResult(io, flags, r, (x) =>
6143
+ !x.changed
6144
+ ? `${orgId}: already ${x.isTest ? "a test org" : "not a test org"}; nothing changed.`
6145
+ : `${orgId}: ${x.isTest ? "now a test org (never gated or billed)" : "no longer a test org"}.`
6146
+ );
6147
+ return 0;
6148
+ }
6149
+
6118
6150
  /** admin billing lago … — staff operations on Lago (docs/billing/lago-integration.md). */
6119
6151
  async function cmdAdminBilling(ctx) {
6120
6152
  const { flags, positionals, io } = ctx;
@@ -6122,6 +6154,7 @@ async function cmdAdminBilling(ctx) {
6122
6154
  if (positionals[0] === "free") return cmdAdminBillingFree(ctx);
6123
6155
  if (positionals[0] === "desk") return cmdAdminBillingDesk(ctx);
6124
6156
  if (positionals[0] === "emails") return cmdAdminBillingEmails(ctx);
6157
+ if (positionals[0] === "org") return cmdAdminBillingOrg(ctx);
6125
6158
  if (positionals[0] !== "lago") throw new CliError(LAGO_USAGE);
6126
6159
  const sub = positionals[1];
6127
6160
  switch (sub) {
@@ -1,13 +1,14 @@
1
1
  // `visits` — Visitors from a terminal: which AI agents read your site, why they
2
2
  // came, which AI answers sent people to you, and two agents side by side. Thin
3
3
  // wrappers over the visitors_* tools (visitors_get_agent_visits,
4
- // visitors_get_citations, visitors_compare_agents, visitors_export, …), the same
4
+ // visitors_get_citations, visitors_get_human_visits, visitors_compare_agents, visitors_export, …), the same
5
5
  // lib functions the app and lettervisit.com/mcp call.
6
6
  //
7
7
  // letterstory visits --agent claude --since 7d
8
8
  // letterstory visits --kind agent_file --paths 100 --since 7d
9
9
  // letterstory visits compare chatgpt claude --path '/docs/*' --since 7d
10
10
  // letterstory visits citations --since 30d
11
+ // letterstory visits people --since 30d people who read the site, and how many were identified
11
12
  // letterstory visits export --since 30d --format csv
12
13
  // letterstory visits sites
13
14
  // letterstory visits pages --since 28d [--path '/docs/*'] [--agent openai-crawler]
@@ -35,6 +36,7 @@ export const ARGS = {
35
36
  paths_limit: { flag: "paths", type: "num" },
36
37
  },
37
38
  visitors_get_citations: { site_id: "site", days },
39
+ visitors_get_human_visits: { site_id: "site", days },
38
40
  visitors_admin_coverage: { site_id: "site" },
39
41
  visitors_add_connector: {
40
42
  site_id: "site",
@@ -89,6 +91,8 @@ export async function cmdVisits(ctx) {
89
91
  return visitsList(sub === undefined ? ctx : rest);
90
92
  case "citations":
91
93
  return visitsCitations(rest);
94
+ case "people":
95
+ return visitsPeople(rest);
92
96
  case "compare":
93
97
  return visitsCompare(rest);
94
98
  case "export":
@@ -107,7 +111,7 @@ export async function cmdVisits(ctx) {
107
111
  return visitsDisconnect(rest);
108
112
  default:
109
113
  throw new CliError(
110
- "Usage: visits [list|citations|compare|export|sites|pages|health|connect|connectors|disconnect]"
114
+ "Usage: visits [list|citations|people|compare|export|sites|pages|health|connect|connectors|disconnect]"
111
115
  );
112
116
  }
113
117
  }
@@ -279,6 +283,26 @@ async function visitsCitations(ctx) {
279
283
  return 0;
280
284
  }
281
285
 
286
+ async function visitsPeople(ctx) {
287
+ const { flags, io } = ctx;
288
+ const result = await call(ctx, "visitors_get_human_visits");
289
+ printResult(io, flags, result, (r) => {
290
+ if (!r.people_counted) return r.message ?? "People aren't counted for this site.";
291
+ const lines = [`${r.humans} page views by people, ${r.since} to ${r.until}`];
292
+ if (r.unique_visitors !== null)
293
+ lines.push(` ${r.unique_visitors} unique visitors${r.uniques_exact ? "" : " (a floor)"}`);
294
+ if (r.identified_people !== null) {
295
+ const share =
296
+ r.identified_share === null ? "" : ` (${(r.identified_share * 100).toFixed(1)}% of unique visitors)`;
297
+ lines.push(` ${r.identified_people} identified${share}`);
298
+ }
299
+ lines.push("", "Per day:");
300
+ for (const d of r.daily) lines.push(` ${d.day} ${String(d.humans).padStart(6)}`);
301
+ return lines.join("\n");
302
+ });
303
+ return 0;
304
+ }
305
+
282
306
  async function visitsCompare(ctx) {
283
307
  const { flags, io } = ctx;
284
308
  const result = await call(ctx, "visitors_compare_agents");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@letterstory/cli",
3
- "version": "0.175.28",
3
+ "version": "0.176.1",
4
4
  "description": "Spin up and manage Letter phantom blogs from your terminal.",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",