@hanzo/app 0.3.93 → 0.3.94

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.
@@ -11,10 +11,19 @@ export declare function nextWeek(now: number): string;
11
11
  /** When a cap's period after `now` begins: calendar-aligned in UTC, as the platform counts it. */
12
12
  export declare function nextPeriod(now: number, period?: string): string;
13
13
  /**
14
- * A refusal about money, as a notice: the gateway's billing envelope (its
15
- * message, when it lifts, its ways past), the bot's own weekly budget spent, or
16
- * the run's 402 for an org with no balance with the credits page it names (when
17
- * that page is one of ours). Any other failure is not a notice.
14
+ * Where a reader adds credit: pay's amount step, for this browser's
15
+ * organization, coming back to this page — the address every other Add credits
16
+ * in the app opens (lib/limits.ts), and never the console page the platform
17
+ * names. Read at the moment of the refusal, since the organization moves with
18
+ * the switcher.
19
+ */
20
+ export declare function topup(t?: Say): LimitAction;
21
+ /**
22
+ * A refusal about money, as a notice: no money (an empty balance, no plan and
23
+ * no credit, a spent Free allowance) in the app's words with pay's Add credits,
24
+ * the gateway's billing envelope (its message, when it lifts, its ways past,
25
+ * any top-up among them sent to pay), or the bot's own weekly budget spent.
26
+ * Any other failure is not a notice.
18
27
  */
19
28
  export declare function refusal(e: unknown, name: string, now: number, t?: Say, period?: string): Notice | null;
20
29
  /**
@@ -1,6 +1,8 @@
1
1
  // A refusal about money, read from what the platform answered.
2
2
  import { english } from '@hanzo/i18n';
3
3
  import { billed, ours, say } from '../failure.js';
4
+ import { payUrl } from '../lib/pay.js';
5
+ import { org } from '../lib/session.js';
4
6
  /** The agents route's code for a bot whose own period cap is spent (apps/agents budget.go). */
5
7
  const BUDGET = 'budget_exceeded';
6
8
  /** Monday 00:00 UTC after `now`: a weekly cap's period is the calendar week in UTC. */
@@ -20,20 +22,70 @@ export function nextPeriod(now, period = 'week') {
20
22
  return nextWeek(now);
21
23
  }
22
24
  /**
23
- * A refusal about money, as a notice: the gateway's billing envelope (its
24
- * message, when it lifts, its ways past), the bot's own weekly budget spent, or
25
- * the run's 402 for an org with no balance with the credits page it names (when
26
- * that page is one of ours). Any other failure is not a notice.
25
+ * The refusals money clears (cloud sdk/errmap.go `funded`): no balance, no
26
+ * subscription with no credit, and a spent Free-plan allowance. The platform
27
+ * says each with its console's credits page in the sentence and as
28
+ * `billingUrl`; a reader is told in the app's own words instead, with the one
29
+ * way past it, adding credit.
30
+ */
31
+ const EMPTY = new Set(['insufficient_balance', 'payment_required']);
32
+ const SPENT = 'allowance_spent';
33
+ /**
34
+ * Where a reader adds credit: pay's amount step, for this browser's
35
+ * organization, coming back to this page — the address every other Add credits
36
+ * in the app opens (lib/limits.ts), and never the console page the platform
37
+ * names. Read at the moment of the refusal, since the organization moves with
38
+ * the switcher.
39
+ */
40
+ export function topup(t = english) {
41
+ const here = typeof window === 'undefined' ? undefined : window.location.href;
42
+ return { kind: 'topup', label: t('Add credits'), url: payUrl('/amount/card', { org: org(), returnUrl: here }) };
43
+ }
44
+ /**
45
+ * Hanzo's console, as a reader reaches it: platform.hanzo.ai. The platform
46
+ * names console.hanzo.ai in the pages its refusals send a person to, in the
47
+ * sentence and as `billingUrl`; both are read with the host a reader knows.
48
+ */
49
+ const platform = (s) => s.replace(/https:\/\/console\.hanzo\.ai(?=[/?#\s.,;)]|$)/g, 'https://platform.hanzo.ai');
50
+ /** What an out-of-money refusal says, and its way past, or null for any other code. */
51
+ function broke(code, reason, resets, t) {
52
+ // An unresolved 402 is billing that could not be checked, not money missing: its cure is a retry.
53
+ if (typeof code !== 'string' || reason === 'unresolved')
54
+ return null;
55
+ if (EMPTY.has(code))
56
+ return { message: t('Your balance is empty.'), resets: null, actions: [topup(t)] };
57
+ if (code === SPENT)
58
+ return { message: t('Usage reached.'), resets, actions: [topup(t)] };
59
+ return null;
60
+ }
61
+ /**
62
+ * A refusal about money, as a notice: no money (an empty balance, no plan and
63
+ * no credit, a spent Free allowance) in the app's words with pay's Add credits,
64
+ * the gateway's billing envelope (its message, when it lifts, its ways past,
65
+ * any top-up among them sent to pay), or the bot's own weekly budget spent.
66
+ * Any other failure is not a notice.
27
67
  */
28
68
  export function refusal(e, name, now, t = english, period = 'week') {
29
69
  const no = billed(e);
70
+ // The gateway's envelope keeps its own words and ways, save an empty balance
71
+ // (whose sentence names the console) and a top-up, which is pay's.
30
72
  if (no)
31
- return { message: no.message || say(e, t('your message to {name}', { name }), 'save', t), resets: no.resets_at, actions: no.actions };
73
+ return EMPTY.has(no.code)
74
+ ? broke(no.code, undefined, no.resets_at, t)
75
+ : {
76
+ message: no.message ? platform(no.message) : say(e, t('your message to {name}', { name }), 'save', t),
77
+ resets: no.resets_at,
78
+ actions: no.actions.map((a) => (a.kind === 'topup' ? { ...a, url: topup(t).url } : a)),
79
+ };
32
80
  const status = e?.status;
33
81
  if (status !== 402 && status !== 429)
34
82
  return null;
35
83
  const raw = e?.body;
36
84
  const body = raw && typeof raw.error === 'object' && raw.error ? raw.error : raw ?? {};
85
+ const resets = typeof body.resets_at === 'string' ? body.resets_at : null;
86
+ const money = broke(body.code, body.reason, resets, t);
87
+ if (money)
88
+ return money;
37
89
  if (body.code === BUDGET) {
38
90
  const spent = period === 'day'
39
91
  ? t('{name} has spent today’s budget.', { name })
@@ -42,11 +94,13 @@ export function refusal(e, name, now, t = english, period = 'week') {
42
94
  : t('{name} has spent this week’s budget.', { name });
43
95
  return { message: spent, resets: nextPeriod(now, period), actions: [] };
44
96
  }
97
+ // Any other refusal is the platform's own words, and the console page it names
98
+ // (a spend cap's budgets) is offered as what it is, never as Add credits.
45
99
  const url = ours(body.billingUrl);
46
100
  return {
47
- message: typeof body.message === 'string' && body.message ? body.message : say(e, t('your message to {name}', { name }), 'save', t),
48
- resets: typeof body.resets_at === 'string' ? body.resets_at : null,
49
- actions: url ? [{ kind: 'topup', label: t('Add credits'), url }] : [],
101
+ message: typeof body.message === 'string' && body.message ? platform(body.message) : say(e, t('your message to {name}', { name }), 'save', t),
102
+ resets,
103
+ actions: url ? [{ kind: 'upgrade', label: body.code === 'spend_cap_exceeded' ? t('Raise the spend cap') : t('Open billing'), url: platform(url) }] : [],
50
104
  };
51
105
  }
52
106
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hanzo/app",
3
- "version": "0.3.93",
3
+ "version": "0.3.94",
4
4
  "description": "Hanzo's app: the shell and its apps (Home, Chat, Inbox, Contacts, Meet, Calendar, Drive, Board, Work, Bots), once, for every Hanzo surface.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",