@aglyn/aglyn 1.0.0-beta.219 → 1.0.0-beta.220

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.
Files changed (175) hide show
  1. package/package.json +12 -12
  2. package/src/lib/app-utils/actions.d.ts +1 -59
  3. package/src/lib/app-utils/actions.js +7 -72
  4. package/src/lib/app-utils/actions.js.map +1 -1
  5. package/src/lib/app-utils/consent-groups.d.ts +1 -1
  6. package/src/lib/app-utils/consent-groups.js.map +1 -1
  7. package/src/lib/app-utils/console-routes.d.ts +0 -36
  8. package/src/lib/app-utils/console-routes.js +2 -37
  9. package/src/lib/app-utils/console-routes.js.map +1 -1
  10. package/src/lib/app-utils/contacts.d.ts +1 -1
  11. package/src/lib/app-utils/contacts.js.map +1 -1
  12. package/src/lib/app-utils/container-membership.d.ts +93 -0
  13. package/src/lib/app-utils/container-membership.js +165 -0
  14. package/src/lib/app-utils/container-membership.js.map +1 -0
  15. package/src/lib/app-utils/crm-task-reminders.d.ts +23 -6
  16. package/src/lib/app-utils/crm-task-reminders.js +32 -11
  17. package/src/lib/app-utils/crm-task-reminders.js.map +1 -1
  18. package/src/lib/app-utils/crm.d.ts +24 -4
  19. package/src/lib/app-utils/crm.js +23 -0
  20. package/src/lib/app-utils/crm.js.map +1 -1
  21. package/src/lib/app-utils/csv-upload.d.ts +1 -1
  22. package/src/lib/app-utils/csv-upload.js +1 -1
  23. package/src/lib/app-utils/csv-upload.js.map +1 -1
  24. package/src/lib/app-utils/{dataset-csv.d.ts → csv.d.ts} +10 -43
  25. package/src/lib/app-utils/{dataset-csv.js → csv.js} +10 -81
  26. package/src/lib/app-utils/csv.js.map +1 -0
  27. package/src/lib/app-utils/datasets.d.ts +0 -19
  28. package/src/lib/app-utils/datasets.js +0 -13
  29. package/src/lib/app-utils/datasets.js.map +1 -1
  30. package/src/lib/app-utils/docs-help.generated.d.ts +2 -2
  31. package/src/lib/app-utils/docs-help.generated.js +2 -0
  32. package/src/lib/app-utils/docs-help.generated.js.map +1 -1
  33. package/src/lib/app-utils/docs-index.generated.js +63 -35
  34. package/src/lib/app-utils/docs-index.generated.js.map +1 -1
  35. package/src/lib/app-utils/dynamic-list-rule.js +3 -3
  36. package/src/lib/app-utils/dynamic-list-rule.js.map +1 -1
  37. package/src/lib/app-utils/email-text.d.ts +80 -0
  38. package/src/lib/app-utils/email-text.js +189 -0
  39. package/src/lib/app-utils/email-text.js.map +1 -0
  40. package/src/lib/app-utils/{list-assignment-policy.d.ts → enrollment-basis.d.ts} +4 -0
  41. package/src/lib/app-utils/{list-assignment-policy.js → enrollment-basis.js} +5 -1
  42. package/src/lib/app-utils/enrollment-basis.js.map +1 -0
  43. package/src/lib/app-utils/expand-repeatables.js +4 -4
  44. package/src/lib/app-utils/expand-repeatables.js.map +1 -1
  45. package/src/lib/app-utils/forms.js +2 -2
  46. package/src/lib/app-utils/forms.js.map +1 -1
  47. package/src/lib/app-utils/health-report.d.ts +2 -3
  48. package/src/lib/app-utils/health-report.js +8 -6
  49. package/src/lib/app-utils/health-report.js.map +1 -1
  50. package/src/lib/app-utils/inbound-mail-domain.d.ts +27 -0
  51. package/src/lib/app-utils/inbound-mail-domain.js +31 -0
  52. package/src/lib/app-utils/inbound-mail-domain.js.map +1 -0
  53. package/src/lib/app-utils/lockdown.d.ts +11 -0
  54. package/src/lib/app-utils/lockdown.js +33 -3
  55. package/src/lib/app-utils/lockdown.js.map +1 -1
  56. package/src/lib/app-utils/member-email-aliases.d.ts +25 -0
  57. package/src/lib/app-utils/member-email-aliases.js +29 -0
  58. package/src/lib/app-utils/member-email-aliases.js.map +1 -1
  59. package/src/lib/app-utils/notifications.d.ts +69 -0
  60. package/src/lib/app-utils/notifications.js +119 -0
  61. package/src/lib/app-utils/notifications.js.map +1 -1
  62. package/src/lib/app-utils/operator-alerts.d.ts +15 -1
  63. package/src/lib/app-utils/operator-alerts.js +47 -10
  64. package/src/lib/app-utils/operator-alerts.js.map +1 -1
  65. package/src/lib/app-utils/plan-entitlements.d.ts +164 -178
  66. package/src/lib/app-utils/plan-entitlements.js +200 -224
  67. package/src/lib/app-utils/plan-entitlements.js.map +1 -1
  68. package/src/lib/app-utils/platform-marketing-consent.d.ts +56 -2
  69. package/src/lib/app-utils/platform-marketing-consent.js +6 -5
  70. package/src/lib/app-utils/platform-marketing-consent.js.map +1 -1
  71. package/src/lib/app-utils/plugin-starter-templates.generated.d.ts +12 -0
  72. package/src/lib/app-utils/plugin-starter-templates.generated.js +711 -0
  73. package/src/lib/app-utils/plugin-starter-templates.generated.js.map +1 -0
  74. package/src/lib/app-utils/repeat-query.d.ts +56 -0
  75. package/src/lib/app-utils/{dataset-query.js → repeat-query.js} +16 -16
  76. package/src/lib/app-utils/repeat-query.js.map +1 -0
  77. package/src/lib/app-utils/server.d.ts +7 -9
  78. package/src/lib/app-utils/server.js +18 -26
  79. package/src/lib/app-utils/server.js.map +1 -1
  80. package/src/lib/app-utils/starter-template-nodes.d.ts +137 -0
  81. package/src/lib/app-utils/starter-template-nodes.js +129 -0
  82. package/src/lib/app-utils/starter-template-nodes.js.map +1 -0
  83. package/src/lib/app-utils/starter-templates.d.ts +11 -34
  84. package/src/lib/app-utils/starter-templates.js +19 -230
  85. package/src/lib/app-utils/starter-templates.js.map +1 -1
  86. package/src/lib/app-utils/{email-topics.d.ts → subscription-topics.d.ts} +64 -48
  87. package/src/lib/app-utils/{email-topics.js → subscription-topics.js} +39 -109
  88. package/src/lib/app-utils/subscription-topics.js.map +1 -0
  89. package/src/lib/app-utils/usage-budget.d.ts +73 -188
  90. package/src/lib/app-utils/usage-budget.js +58 -194
  91. package/src/lib/app-utils/usage-budget.js.map +1 -1
  92. package/src/lib/app-utils/utm-attribution.d.ts +1 -1
  93. package/src/lib/app-utils/utm-attribution.js +1 -1
  94. package/src/lib/app-utils/utm-attribution.js.map +1 -1
  95. package/src/lib/app-utils/{campaign-touch.d.ts → utm-touch.d.ts} +21 -13
  96. package/src/lib/app-utils/{campaign-touch.js → utm-touch.js} +30 -22
  97. package/src/lib/app-utils/utm-touch.js.map +1 -0
  98. package/src/lib/app-utils/variables.d.ts +9 -9
  99. package/src/lib/app-utils/variables.js.map +1 -1
  100. package/src/lib/app-utils/visitor-record-ceiling.d.ts +32 -0
  101. package/src/lib/app-utils/visitor-record-ceiling.js +39 -0
  102. package/src/lib/app-utils/visitor-record-ceiling.js.map +1 -1
  103. package/src/lib/foundation/definitions/org-billing.types.d.ts +2 -2
  104. package/src/lib/foundation/definitions/org-billing.types.js.map +1 -1
  105. package/src/lib/plugin-manager/feature-plugins.d.ts +80 -79
  106. package/src/lib/plugin-manager/feature-plugins.js +22 -35
  107. package/src/lib/plugin-manager/feature-plugins.js.map +1 -1
  108. package/src/lib/plugin-manager/first-party-plugins.generated.d.ts +12 -1
  109. package/src/lib/plugin-manager/first-party-plugins.generated.js +58 -2
  110. package/src/lib/plugin-manager/first-party-plugins.generated.js.map +1 -1
  111. package/src/lib/plugin-manager/index.d.ts +0 -1
  112. package/src/lib/plugin-manager/index.js +0 -1
  113. package/src/lib/plugin-manager/index.js.map +1 -1
  114. package/src/lib/plugin-manager/plugin-contact-capture.d.ts +9 -4
  115. package/src/lib/plugin-manager/plugin-contact-capture.js.map +1 -1
  116. package/src/lib/plugin-manager/plugin-containers.d.ts +68 -0
  117. package/src/lib/plugin-manager/plugin-containers.generated.d.ts +10 -0
  118. package/src/lib/plugin-manager/plugin-containers.generated.js +20 -0
  119. package/src/lib/plugin-manager/plugin-containers.generated.js.map +1 -0
  120. package/src/lib/plugin-manager/plugin-containers.js +27 -0
  121. package/src/lib/plugin-manager/plugin-containers.js.map +1 -0
  122. package/src/lib/plugin-manager/plugin-entitlements.d.ts +8 -0
  123. package/src/lib/plugin-manager/plugin-entitlements.js.map +1 -1
  124. package/src/lib/plugin-manager/plugin-membership-detach.js +1 -1
  125. package/src/lib/plugin-manager/plugin-membership-detach.js.map +1 -1
  126. package/src/lib/plugin-manager/plugin-record-email-state.d.ts +69 -0
  127. package/src/lib/plugin-manager/plugin-record-email-state.js +32 -0
  128. package/src/lib/plugin-manager/plugin-record-email-state.js.map +1 -1
  129. package/src/lib/plugin-manager/plugin-record-lists.d.ts +115 -0
  130. package/src/lib/plugin-manager/plugin-record-lists.js +75 -0
  131. package/src/lib/plugin-manager/plugin-record-lists.js.map +1 -0
  132. package/src/lib/plugin-manager/plugin-record-timeline.d.ts +42 -8
  133. package/src/lib/plugin-manager/plugin-record-timeline.js.map +1 -1
  134. package/src/lib/plugin-manager/plugin-site-beacons.d.ts +71 -0
  135. package/src/lib/plugin-manager/plugin-site-beacons.js +54 -0
  136. package/src/lib/plugin-manager/plugin-site-beacons.js.map +1 -0
  137. package/src/lib/plugin-manager/plugin-subscription-topics.generated.d.ts +11 -0
  138. package/src/lib/plugin-manager/plugin-subscription-topics.generated.js +41 -0
  139. package/src/lib/plugin-manager/plugin-subscription-topics.generated.js.map +1 -0
  140. package/src/lib/plugin-manager/plugin-usage-axes.d.ts +212 -0
  141. package/src/lib/plugin-manager/plugin-usage-axes.js +82 -3
  142. package/src/lib/plugin-manager/plugin-usage-axes.js.map +1 -1
  143. package/src/lib/plugin-manager/plugin-usage-meters.d.ts +108 -0
  144. package/src/lib/plugin-manager/plugin-usage-meters.js +105 -0
  145. package/src/lib/plugin-manager/plugin-usage-meters.js.map +1 -0
  146. package/src/lib/plugin-manager/usage-alert-contributors.d.ts +76 -6
  147. package/src/lib/plugin-manager/usage-alert-contributors.js +23 -10
  148. package/src/lib/plugin-manager/usage-alert-contributors.js.map +1 -1
  149. package/src/lib/app-utils/assist-credits.d.ts +0 -482
  150. package/src/lib/app-utils/assist-credits.js +0 -510
  151. package/src/lib/app-utils/assist-credits.js.map +0 -1
  152. package/src/lib/app-utils/campaign-membership.d.ts +0 -121
  153. package/src/lib/app-utils/campaign-membership.js +0 -189
  154. package/src/lib/app-utils/campaign-membership.js.map +0 -1
  155. package/src/lib/app-utils/campaign-touch.js.map +0 -1
  156. package/src/lib/app-utils/console-record-links.d.ts +0 -84
  157. package/src/lib/app-utils/console-record-links.js +0 -76
  158. package/src/lib/app-utils/console-record-links.js.map +0 -1
  159. package/src/lib/app-utils/crm-booking.d.ts +0 -75
  160. package/src/lib/app-utils/crm-booking.js +0 -133
  161. package/src/lib/app-utils/crm-booking.js.map +0 -1
  162. package/src/lib/app-utils/crm-inbound.d.ts +0 -245
  163. package/src/lib/app-utils/crm-inbound.js +0 -407
  164. package/src/lib/app-utils/crm-inbound.js.map +0 -1
  165. package/src/lib/app-utils/crm-reports.d.ts +0 -479
  166. package/src/lib/app-utils/crm-reports.js +0 -680
  167. package/src/lib/app-utils/crm-reports.js.map +0 -1
  168. package/src/lib/app-utils/dataset-csv.js.map +0 -1
  169. package/src/lib/app-utils/dataset-query.d.ts +0 -57
  170. package/src/lib/app-utils/dataset-query.js.map +0 -1
  171. package/src/lib/app-utils/email-topics.js.map +0 -1
  172. package/src/lib/app-utils/list-assignment-policy.js.map +0 -1
  173. package/src/lib/plugin-manager/plugin-metered-lines.d.ts +0 -97
  174. package/src/lib/plugin-manager/plugin-metered-lines.js +0 -123
  175. package/src/lib/plugin-manager/plugin-metered-lines.js.map +0 -1
@@ -14,6 +14,60 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
+ /**
18
+ * WHAT A PLUGIN'S USAGE COSTS, AND HOW MUCH OF ITS BAND IT SPENT (AGL-3080).
19
+ *
20
+ * The platform keeps one cost model (`orgMonthlyCogsUsd`) and one
21
+ * utilization table (`margin-utilization.ts`), and both read the monthly
22
+ * usage rollup. Several of the rollup's meters are a plugin's: the AI
23
+ * plugin's provider spend, the CRM's records, the forms plugin's
24
+ * submissions, the workflows plugin's runs. Core used to name each of them —
25
+ * its rollup field, the unit rate that prices it, the band it is measured
26
+ * against — so a plugin that metered something new could not reach the
27
+ * discount guardrail without a core edit, and core could not be taken
28
+ * without the plugins it named.
29
+ *
30
+ * So a plugin declares its meters, and core composes them:
31
+ *
32
+ * - a COST AXIS names the rollup fields a meter is recorded under and the
33
+ * `ORG_COGS_UNIT_RATES_USD` key that prices one unit, or no key when the
34
+ * field is already dollars. `orgMonthlyCogsUsd` prices it beside core's
35
+ * own axes, and `orgCogsInputFrom` forwards its fields.
36
+ * - a BAND names the rollup fields that measure it and the entitlement that
37
+ * says what the plan includes. The utilization table reads it beside
38
+ * core's own bands. A METERED band is also billed past what the plan
39
+ * includes, at cost × `METERED_MARKUP`, beside storage and bandwidth: the
40
+ * invoice sweep, the Billing card's estimate, the monthly summary and the
41
+ * staff usage rows read it from here.
42
+ * - a SPEND LINE names the month document holding what the plugin's usage
43
+ * came to at the rates it bills, the deployment variable naming the month
44
+ * it is first charged for, and the unit a customer sees it in. The usage
45
+ * budget shows it beside the metered figure and counts it once charged.
46
+ * - a METER names what the plugin measures in the monthly usage sweep
47
+ * (`plugin-usage-meters.ts`), so the sweep can refuse to bill a month the
48
+ * registered meter is missing from.
49
+ *
50
+ * ## What core keeps
51
+ *
52
+ * The MONEY. Every unit rate stays in `ORG_COGS_UNIT_RATES_USD`, beside the
53
+ * billed table it is reconciled against (`METERED_UNIT_RATES_USD`, which a
54
+ * metered band names its rate in), and every included figure stays in
55
+ * `PLAN_ENTITLEMENTS`. A declaration names fields and keys; it carries no
56
+ * price. The one number a declaration may carry is a band's unit cost, where
57
+ * the band is sold in a unit OF cost (a credit is a fixed quantity of
58
+ * dollars), and that number is the plugin's own definition of its unit.
59
+ *
60
+ * ## Compiled, never registered
61
+ *
62
+ * The cost model's readers include the staff org page, which prices a rollup
63
+ * in the browser, and the discount guardrail, whose every wrong answer is in
64
+ * the approving direction: a cost axis a registry had not filled would price
65
+ * at nothing and approve a discount the margin cannot carry. So these are
66
+ * compiled from each plugin's `usage-axes` declaration by the manifest
67
+ * generator, like the plan figures, and a rate key core does not have is an
68
+ * error rather than a zero.
69
+ */
70
+ import type { ReleaseFlagKey } from '../app-utils/release-flags';
17
71
  /** A meter a plugin contributes to the platform's cost model. */
18
72
  export interface PluginCostAxisDeclaration {
19
73
  /** The breakdown key every staff reader shows it under. */
@@ -32,6 +86,12 @@ export interface PluginCostAxisDeclaration {
32
86
  * by `orgCogsInputFrom` and never priced.
33
87
  */
34
88
  recordedFields?: readonly string[];
89
+ /**
90
+ * Rollup fields the plugin's usage sweep writes beside the meter for its
91
+ * staff surfaces to read — served on the staff usage rows, `null` where a
92
+ * rollup never wrote one, and never forwarded to the cost model.
93
+ */
94
+ staffFields?: readonly string[];
35
95
  /**
36
96
  * The `ORG_COGS_UNIT_RATES_USD` key pricing one unit. Absent when the
37
97
  * fields are already dollars, which then enter the model at ×1.
@@ -67,6 +127,101 @@ export interface PluginUsageBandDeclaration {
67
127
  * made never reads as less of the band.
68
128
  */
69
129
  unitCostUsd?: number;
130
+ /**
131
+ * The per-site monthly counter the band is measured by:
132
+ * `hosts/{hostId}/counters/{hostCounter}`, field `{month}`, summed over the
133
+ * workspace's sites.
134
+ */
135
+ hostCounter?: string;
136
+ /**
137
+ * The workspace is WARNED as it approaches and reaches this band, by the
138
+ * usage-alerts sweep, from its `hostCounter` against what the plan includes
139
+ * of `entitlement`, once per threshold per month. `label` names the band in
140
+ * the title ("monthly workflow runs") and `noun` in the opening sentence
141
+ * ("workflow runs"); `outcome` is what happens at the band — `stops`,
142
+ * `bills` or `continues` — and `reached` and `approach` are the sentences
143
+ * that say so, at the band and approaching it.
144
+ */
145
+ alert?: {
146
+ label: string;
147
+ noun: string;
148
+ outcome: 'stops' | 'bills' | 'continues';
149
+ reached: string;
150
+ approach: string;
151
+ };
152
+ /**
153
+ * The band is one of the INFRASTRUCTURE meters (AGL-1280): what the
154
+ * workspace uses past it is billed at our cost × `METERED_MARKUP`, beside
155
+ * storage and bandwidth, on the same invoice line and the same estimate.
156
+ * A metered band names one rollup field and the `hostCounter` it is
157
+ * measured by.
158
+ */
159
+ metered?: PluginMeteredBand;
160
+ }
161
+ /** How a metered band is priced, quoted and withheld. */
162
+ export interface PluginMeteredBand {
163
+ /**
164
+ * The `METERED_UNIT_RATES_USD` key holding our cost per unit — never a
165
+ * number: the rate table is the platform's, beside the markup it is
166
+ * published with.
167
+ */
168
+ rate: string;
169
+ /** The count a published price is quoted per: `1000` reads "per 1,000". */
170
+ quotedPer: number;
171
+ /** The band in running prose, plural and lowercase: "form submissions". */
172
+ noun: string;
173
+ /**
174
+ * A release flag the overage waits behind: while it is off for the
175
+ * workspace, the units are counted and the charge is recorded as withheld
176
+ * rather than billed — nobody is charged for what they cannot reach
177
+ * (AGL-1604). The rollup records the verdict as `{field}Billed` and what
178
+ * was forgone, at cost, as `{field}OverageWithheldUsd`.
179
+ */
180
+ withheldUntil?: ReleaseFlagKey;
181
+ }
182
+ /**
183
+ * A line of a workspace's monthly SPEND a plugin contributes to its usage
184
+ * budget (`usage-budget.ts`): what the plugin's usage came to this month, in
185
+ * the dollars it is billed at, shown on the customer's budget card and
186
+ * counted toward the budget from the month the plugin starts charging for it.
187
+ */
188
+ export interface PluginSpendLineDeclaration {
189
+ /** The line's key. */
190
+ id: string;
191
+ /** What the budget card and the budget alert call it. */
192
+ label: string;
193
+ /**
194
+ * Where the month's figure is read: `orgs/{orgId}/{collection}/{month}`,
195
+ * field `field`, in dollars.
196
+ */
197
+ live: {
198
+ collection: string;
199
+ field: string;
200
+ };
201
+ /**
202
+ * The deployment variable naming the first month (`YYYY-MM`) the line is
203
+ * charged for. Until it names this month or an earlier one — and on any
204
+ * value that is not a month — the line is shown and never counted.
205
+ */
206
+ billedFromEnv: string;
207
+ /**
208
+ * The unit the customer is shown the line in, when the stored dollars are
209
+ * not theirs to see: `ceil(dollars / costUsd)` of `label`, and the dollar
210
+ * figure never crosses to the browser.
211
+ */
212
+ unit?: {
213
+ costUsd: number;
214
+ label: string;
215
+ };
216
+ }
217
+ /**
218
+ * A meter the plugin measures in the platform's monthly usage sweep and
219
+ * registers at runtime (`plugin-usage-meters.ts`). Declared as well as
220
+ * registered so the sweep can refuse to bill a workspace's month without it.
221
+ */
222
+ export interface PluginUsageMeterDeclaration {
223
+ /** As `registerPluginUsageMeter` names it. */
224
+ id: string;
70
225
  }
71
226
  export type ResolvedPluginCostAxis = PluginCostAxisDeclaration & {
72
227
  pluginId: string;
@@ -74,13 +229,55 @@ export type ResolvedPluginCostAxis = PluginCostAxisDeclaration & {
74
229
  export type ResolvedPluginUsageBand = PluginUsageBandDeclaration & {
75
230
  pluginId: string;
76
231
  };
232
+ export type ResolvedPluginSpendLine = PluginSpendLineDeclaration & {
233
+ pluginId: string;
234
+ };
235
+ export type ResolvedPluginUsageMeter = PluginUsageMeterDeclaration & {
236
+ pluginId: string;
237
+ };
77
238
  /** What a plugin's `usageAxes` function answers. */
78
239
  export interface PluginUsageAxesDeclaration {
79
240
  costAxes?: readonly PluginCostAxisDeclaration[];
80
241
  bands?: readonly PluginUsageBandDeclaration[];
242
+ spendLines?: readonly PluginSpendLineDeclaration[];
243
+ meters?: readonly PluginUsageMeterDeclaration[];
81
244
  }
82
245
  export declare function pluginCostAxes(): readonly ResolvedPluginCostAxis[];
83
246
  export declare function pluginUsageBands(): readonly ResolvedPluginUsageBand[];
247
+ /** A declared band that is metered, with its one field and its counter. */
248
+ export type ResolvedPluginMeteredBand = ResolvedPluginUsageBand & {
249
+ metered: PluginMeteredBand;
250
+ hostCounter: string;
251
+ };
252
+ /**
253
+ * Every band billed at cost past what the plan includes, in band order — the
254
+ * infrastructure meters a plugin adds beside storage and bandwidth.
255
+ */
256
+ export declare function meteredPluginBands(): readonly ResolvedPluginMeteredBand[];
257
+ /** A declared band measured by a per-site counter and never billed past it. */
258
+ export type ResolvedPluginCountedBand = ResolvedPluginUsageBand & {
259
+ hostCounter: string;
260
+ };
261
+ /**
262
+ * Every band measured by a per-site counter and not metered, in band order:
263
+ * the usage sweep sums each across the workspace's sites and records it under
264
+ * the band's first field, and the staff usage rows show the count. A metered
265
+ * band's counter is read with the infrastructure meters instead.
266
+ */
267
+ export declare function countedPluginBands(): readonly ResolvedPluginCountedBand[];
268
+ /** The rollup field a metered band records its count under. */
269
+ export declare function meteredBandField(band: ResolvedPluginMeteredBand): string;
270
+ /**
271
+ * Where the rollup records a withheld band's verdict (`metered.withheldUntil`):
272
+ * whether the month billed the band's overage, and what was forgone, at cost,
273
+ * when it did not. Without the second a withheld month reads as a month that
274
+ * stayed inside the band.
275
+ */
276
+ export declare function meteredBandVerdictFields(band: ResolvedPluginMeteredBand): {
277
+ billed: string;
278
+ withheldUsd: string;
279
+ };
280
+ export declare function pluginSpendLines(): readonly ResolvedPluginSpendLine[];
84
281
  /**
85
282
  * What a rollup recorded for a declared meter: `fields` summed, or
86
283
  * `fallbackFields` summed when the rollup carries none of `fields` at all.
@@ -105,3 +302,18 @@ export declare function liveMeterReading(document: {
105
302
  }): number | undefined;
106
303
  /** Every rollup field the declared cost axes read or record, in order. */
107
304
  export declare function pluginCostAxisFields(): string[];
305
+ /**
306
+ * A month's rollup as a staff reader is served it, one entry per field the
307
+ * declared cost axes read, record or serve to staff (`get` reads one rollup
308
+ * field).
309
+ *
310
+ * A field an axis prices reads zero where the rollup lacks it, as the cost
311
+ * model reads it — except where the axis falls back to an older basis when
312
+ * the rollup carries none of its fields: there absence is the answer that
313
+ * selects the fallback, so it is served as `null`, and a reader pricing the
314
+ * served row reads the month the way the sweep did. A field an axis only
315
+ * records or serves reads `null` when the rollup never wrote it: "not
316
+ * recorded" and "recorded zero" are different answers, and a projection
317
+ * that collapses them invents history (AGL-2321).
318
+ */
319
+ export declare function pluginCostAxisProjection(get: (field: string) => unknown): Record<string, number | null>;
@@ -34,12 +34,23 @@
34
34
  * own axes, and `orgCogsInputFrom` forwards its fields.
35
35
  * - a BAND names the rollup fields that measure it and the entitlement that
36
36
  * says what the plan includes. The utilization table reads it beside
37
- * core's own bands.
37
+ * core's own bands. A METERED band is also billed past what the plan
38
+ * includes, at cost × `METERED_MARKUP`, beside storage and bandwidth: the
39
+ * invoice sweep, the Billing card's estimate, the monthly summary and the
40
+ * staff usage rows read it from here.
41
+ * - a SPEND LINE names the month document holding what the plugin's usage
42
+ * came to at the rates it bills, the deployment variable naming the month
43
+ * it is first charged for, and the unit a customer sees it in. The usage
44
+ * budget shows it beside the metered figure and counts it once charged.
45
+ * - a METER names what the plugin measures in the monthly usage sweep
46
+ * (`plugin-usage-meters.ts`), so the sweep can refuse to bill a month the
47
+ * registered meter is missing from.
38
48
  *
39
49
  * ## What core keeps
40
50
  *
41
51
  * The MONEY. Every unit rate stays in `ORG_COGS_UNIT_RATES_USD`, beside the
42
- * billed table it is reconciled against, and every included figure stays in
52
+ * billed table it is reconciled against (`METERED_UNIT_RATES_USD`, which a
53
+ * metered band names its rate in), and every included figure stays in
43
54
  * `PLAN_ENTITLEMENTS`. A declaration names fields and keys; it carries no
44
55
  * price. The one number a declaration may carry is a band's unit cost, where
45
56
  * the band is sold in a unit OF cost (a credit is a fixed quantity of
@@ -54,13 +65,45 @@
54
65
  * compiled from each plugin's `usage-axes` declaration by the manifest
55
66
  * generator, like the plan figures, and a rate key core does not have is an
56
67
  * error rather than a zero.
57
- */ import { PLUGIN_USAGE_BANDS_DECLARED, PLUGIN_COST_AXES_DECLARED } from "./first-party-plugins.generated.js";
68
+ */ import { PLUGIN_COST_AXES_DECLARED, PLUGIN_SPEND_LINES_DECLARED, PLUGIN_USAGE_BANDS_DECLARED } from "./first-party-plugins.generated.js";
58
69
  export function pluginCostAxes() {
59
70
  return PLUGIN_COST_AXES_DECLARED;
60
71
  }
61
72
  export function pluginUsageBands() {
62
73
  return PLUGIN_USAGE_BANDS_DECLARED;
63
74
  }
75
+ /**
76
+ * Every band billed at cost past what the plan includes, in band order — the
77
+ * infrastructure meters a plugin adds beside storage and bandwidth.
78
+ */ export function meteredPluginBands() {
79
+ return PLUGIN_USAGE_BANDS_DECLARED.filter((band)=>Boolean(band.metered && band.hostCounter));
80
+ }
81
+ /**
82
+ * Every band measured by a per-site counter and not metered, in band order:
83
+ * the usage sweep sums each across the workspace's sites and records it under
84
+ * the band's first field, and the staff usage rows show the count. A metered
85
+ * band's counter is read with the infrastructure meters instead.
86
+ */ export function countedPluginBands() {
87
+ return PLUGIN_USAGE_BANDS_DECLARED.filter((band)=>Boolean(band.hostCounter && !band.metered));
88
+ }
89
+ /** The rollup field a metered band records its count under. */ export function meteredBandField(band) {
90
+ return band.fields[0];
91
+ }
92
+ /**
93
+ * Where the rollup records a withheld band's verdict (`metered.withheldUntil`):
94
+ * whether the month billed the band's overage, and what was forgone, at cost,
95
+ * when it did not. Without the second a withheld month reads as a month that
96
+ * stayed inside the band.
97
+ */ export function meteredBandVerdictFields(band) {
98
+ const field = meteredBandField(band);
99
+ return {
100
+ billed: `${field}Billed`,
101
+ withheldUsd: `${field}OverageWithheldUsd`
102
+ };
103
+ }
104
+ export function pluginSpendLines() {
105
+ return PLUGIN_SPEND_LINES_DECLARED;
106
+ }
64
107
  /** A positive finite number, or 0 — the reading every meter is priced from. */ function meterValue(value) {
65
108
  const parsed = Number(value != null ? value : 0);
66
109
  return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
@@ -103,5 +146,41 @@ export function pluginUsageBands() {
103
146
  }
104
147
  return fields;
105
148
  }
149
+ /**
150
+ * A month's rollup as a staff reader is served it, one entry per field the
151
+ * declared cost axes read, record or serve to staff (`get` reads one rollup
152
+ * field).
153
+ *
154
+ * A field an axis prices reads zero where the rollup lacks it, as the cost
155
+ * model reads it — except where the axis falls back to an older basis when
156
+ * the rollup carries none of its fields: there absence is the answer that
157
+ * selects the fallback, so it is served as `null`, and a reader pricing the
158
+ * served row reads the month the way the sweep did. A field an axis only
159
+ * records or serves reads `null` when the rollup never wrote it: "not
160
+ * recorded" and "recorded zero" are different answers, and a projection
161
+ * that collapses them invents history (AGL-2321).
162
+ */ export function pluginCostAxisProjection(get) {
163
+ const out = {};
164
+ for (const axis of PLUGIN_COST_AXES_DECLARED){
165
+ var _axis_recordedFields, _axis_staffFields, _axis_fallbackFields, _axis_recordedFields1, _axis_staffFields1;
166
+ var _axis_fallbackFields1;
167
+ const absentIsNull = [
168
+ ...((_axis_fallbackFields1 = axis.fallbackFields) == null ? void 0 : _axis_fallbackFields1.length) ? axis.fields : [],
169
+ ...(_axis_recordedFields = axis.recordedFields) != null ? _axis_recordedFields : [],
170
+ ...(_axis_staffFields = axis.staffFields) != null ? _axis_staffFields : []
171
+ ];
172
+ for (const field of [
173
+ ...axis.fields,
174
+ ...(_axis_fallbackFields = axis.fallbackFields) != null ? _axis_fallbackFields : [],
175
+ ...(_axis_recordedFields1 = axis.recordedFields) != null ? _axis_recordedFields1 : [],
176
+ ...(_axis_staffFields1 = axis.staffFields) != null ? _axis_staffFields1 : []
177
+ ]){
178
+ if (field in out) continue;
179
+ const value = get(field);
180
+ out[field] = value != null ? Number(value) : absentIsNull.includes(field) ? null : 0;
181
+ }
182
+ }
183
+ return out;
184
+ }
106
185
 
107
186
  //# sourceMappingURL=plugin-usage-axes.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../libs/aglyn/src/lib/plugin-manager/plugin-usage-axes.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * WHAT A PLUGIN'S USAGE COSTS, AND HOW MUCH OF ITS BAND IT SPENT (AGL-3080).\n *\n * The platform keeps one cost model (`orgMonthlyCogsUsd`) and one\n * utilization table (`margin-utilization.ts`), and both read the monthly\n * usage rollup. Several of the rollup's meters are a plugin's: the AI\n * plugin's provider spend, the CRM's records, the forms plugin's\n * submissions, the workflows plugin's runs. Core used to name each of them —\n * its rollup field, the unit rate that prices it, the band it is measured\n * against — so a plugin that metered something new could not reach the\n * discount guardrail without a core edit, and core could not be taken\n * without the plugins it named.\n *\n * So a plugin declares its meters, and core composes them:\n *\n * - a COST AXIS names the rollup fields a meter is recorded under and the\n * `ORG_COGS_UNIT_RATES_USD` key that prices one unit, or no key when the\n * field is already dollars. `orgMonthlyCogsUsd` prices it beside core's\n * own axes, and `orgCogsInputFrom` forwards its fields.\n * - a BAND names the rollup fields that measure it and the entitlement that\n * says what the plan includes. The utilization table reads it beside\n * core's own bands.\n *\n * ## What core keeps\n *\n * The MONEY. Every unit rate stays in `ORG_COGS_UNIT_RATES_USD`, beside the\n * billed table it is reconciled against, and every included figure stays in\n * `PLAN_ENTITLEMENTS`. A declaration names fields and keys; it carries no\n * price. The one number a declaration may carry is a band's unit cost, where\n * the band is sold in a unit OF cost (a credit is a fixed quantity of\n * dollars), and that number is the plugin's own definition of its unit.\n *\n * ## Compiled, never registered\n *\n * The cost model's readers include the staff org page, which prices a rollup\n * in the browser, and the discount guardrail, whose every wrong answer is in\n * the approving direction: a cost axis a registry had not filled would price\n * at nothing and approve a discount the margin cannot carry. So these are\n * compiled from each plugin's `usage-axes` declaration by the manifest\n * generator, like the plan figures, and a rate key core does not have is an\n * error rather than a zero.\n */\n\nimport {\n PLUGIN_USAGE_BANDS_DECLARED,\n PLUGIN_COST_AXES_DECLARED,\n} from './first-party-plugins.generated'\n\n/** A meter a plugin contributes to the platform's cost model. */\nexport interface PluginCostAxisDeclaration {\n /** The breakdown key every staff reader shows it under. */\n id: string\n /** Where it sorts among the cost model's axes; core's own are multiples of 10. */\n order: number\n /** Rollup fields summed for the axis. */\n fields: readonly string[]\n /**\n * Read only when every one of `fields` is absent from a rollup — the\n * narrower field an older rollup recorded the same meter under.\n */\n fallbackFields?: readonly string[]\n /**\n * Rollup fields recorded beside the meter so its sum is legible, forwarded\n * by `orgCogsInputFrom` and never priced.\n */\n recordedFields?: readonly string[]\n /**\n * The `ORG_COGS_UNIT_RATES_USD` key pricing one unit. Absent when the\n * fields are already dollars, which then enter the model at ×1.\n */\n rate?: string\n /**\n * The month's LIVE reading, when the rollup's figure is a snapshot a meter\n * keeps adding to: `orgs/{orgId}/{collection}/{month}`, the first of\n * `fields` that is a positive number. It replaces the axis's first rollup\n * field wherever a reader fetches it.\n */\n live?: { collection: string; fields: readonly string[] }\n}\n\n/** A band a plugin contributes to the utilization table. */\nexport interface PluginUsageBandDeclaration {\n /** The band's key, and the column the staff table shows it under. */\n id: string\n label: string\n order: number\n /** Rollup fields summed for what the org used. */\n fields: readonly string[]\n fallbackFields?: readonly string[]\n /** The resolved entitlement holding what the plan includes. */\n entitlement: string\n /** The entitlement is per SITE and the org-wide band is it × `hostLimit`. */\n perHost?: boolean\n /**\n * The band is sold in a unit of cost and the rollup records dollars: what\n * was used is `ceil(dollars / unitCostUsd)`, rounded UP so spend already\n * made never reads as less of the band.\n */\n unitCostUsd?: number\n}\n\nexport type ResolvedPluginCostAxis = PluginCostAxisDeclaration & { pluginId: string }\nexport type ResolvedPluginUsageBand = PluginUsageBandDeclaration & { pluginId: string }\n\n/** What a plugin's `usageAxes` function answers. */\nexport interface PluginUsageAxesDeclaration {\n costAxes?: readonly PluginCostAxisDeclaration[]\n bands?: readonly PluginUsageBandDeclaration[]\n}\n\nexport function pluginCostAxes(): readonly ResolvedPluginCostAxis[] {\n return PLUGIN_COST_AXES_DECLARED\n}\n\nexport function pluginUsageBands(): readonly ResolvedPluginUsageBand[] {\n return PLUGIN_USAGE_BANDS_DECLARED\n}\n\n/** A positive finite number, or 0 — the reading every meter is priced from. */\nfunction meterValue(value: unknown): number {\n const parsed = Number(value ?? 0)\n return Number.isFinite(parsed) && parsed > 0 ? parsed : 0\n}\n\n/**\n * What a rollup recorded for a declared meter: `fields` summed, or\n * `fallbackFields` summed when the rollup carries none of `fields` at all.\n *\n * \"Carries none\" is `null` or absent, never zero. A rollup that measured\n * zero has answered, and must not fall back to a figure recorded under an\n * older, narrower basis.\n */\nexport function declaredMeterReading(\n source: Readonly<Record<string, unknown>> | null | undefined,\n declared: {\n fields: readonly string[]\n fallbackFields?: readonly string[]\n },\n): number {\n const present = declared.fields.some((field) => source?.[field] != null)\n const fields =\n !present && declared.fallbackFields?.length\n ? declared.fallbackFields\n : declared.fields\n return fields.reduce((sum, field) => sum + meterValue(source?.[field]), 0)\n}\n\n/**\n * The live reading a cost axis names: the first of its `live.fields` that is\n * a positive number on the month's document, or `undefined` when none is.\n * `undefined` is \"keep the rollup's snapshot\", never zero.\n */\nexport function liveMeterReading(\n document: { get(field: string): unknown } | null | undefined,\n live: { fields: readonly string[] },\n): number | undefined {\n for (const field of live.fields) {\n const value = meterValue(document?.get(field))\n if (value > 0) return value\n }\n return undefined\n}\n\n/** Every rollup field the declared cost axes read or record, in order. */\nexport function pluginCostAxisFields(): string[] {\n const fields: string[] = []\n for (const axis of PLUGIN_COST_AXES_DECLARED) {\n for (const field of [\n ...axis.fields,\n ...(axis.fallbackFields ?? []),\n ...(axis.recordedFields ?? []),\n ]) {\n if (!fields.includes(field)) fields.push(field)\n }\n }\n return fields\n}\n"],"names":["PLUGIN_USAGE_BANDS_DECLARED","PLUGIN_COST_AXES_DECLARED","pluginCostAxes","pluginUsageBands","meterValue","value","parsed","Number","isFinite","declaredMeterReading","source","declared","present","fields","some","field","fallbackFields","length","reduce","sum","liveMeterReading","document","live","get","undefined","pluginCostAxisFields","axis","recordedFields","includes","push"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyCC,GAED,SACEA,2BAA2B,EAC3BC,yBAAyB,QACpB,qCAAiC;AAgExC,OAAO,SAASC;IACd,OAAOD;AACT;AAEA,OAAO,SAASE;IACd,OAAOH;AACT;AAEA,6EAA6E,GAC7E,SAASI,WAAWC,KAAc;IAChC,MAAMC,SAASC,OAAOF,gBAAAA,QAAS;IAC/B,OAAOE,OAAOC,QAAQ,CAACF,WAAWA,SAAS,IAAIA,SAAS;AAC1D;AAEA;;;;;;;CAOC,GACD,OAAO,SAASG,qBACdC,MAA4D,EAC5DC,QAGC;QAIaA;IAFd,MAAMC,UAAUD,SAASE,MAAM,CAACC,IAAI,CAAC,CAACC,QAAUL,CAAAA,0BAAAA,MAAQ,CAACK,MAAM,KAAI;IACnE,MAAMF,SACJ,CAACD,aAAWD,2BAAAA,SAASK,cAAc,qBAAvBL,yBAAyBM,MAAM,IACvCN,SAASK,cAAc,GACvBL,SAASE,MAAM;IACrB,OAAOA,OAAOK,MAAM,CAAC,CAACC,KAAKJ,QAAUI,MAAMf,WAAWM,0BAAAA,MAAQ,CAACK,MAAM,GAAG;AAC1E;AAEA;;;;CAIC,GACD,OAAO,SAASK,iBACdC,QAA4D,EAC5DC,IAAmC;IAEnC,KAAK,MAAMP,SAASO,KAAKT,MAAM,CAAE;QAC/B,MAAMR,QAAQD,WAAWiB,4BAAAA,SAAUE,GAAG,CAACR;QACvC,IAAIV,QAAQ,GAAG,OAAOA;IACxB;IACA,OAAOmB;AACT;AAEA,wEAAwE,GACxE,OAAO,SAASC;IACd,MAAMZ,SAAmB,EAAE;IAC3B,KAAK,MAAMa,QAAQzB,0BAA2B;YAGtCyB,sBACAA;QAHN,KAAK,MAAMX,SAAS;eACfW,KAAKb,MAAM;gBACVa,uBAAAA,KAAKV,cAAc,YAAnBU,uBAAuB,EAAE;gBACzBA,uBAAAA,KAAKC,cAAc,YAAnBD,uBAAuB,EAAE;SAC9B,CAAE;YACD,IAAI,CAACb,OAAOe,QAAQ,CAACb,QAAQF,OAAOgB,IAAI,CAACd;QAC3C;IACF;IACA,OAAOF;AACT"}
1
+ {"version":3,"sources":["../../../../../../libs/aglyn/src/lib/plugin-manager/plugin-usage-axes.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * WHAT A PLUGIN'S USAGE COSTS, AND HOW MUCH OF ITS BAND IT SPENT (AGL-3080).\n *\n * The platform keeps one cost model (`orgMonthlyCogsUsd`) and one\n * utilization table (`margin-utilization.ts`), and both read the monthly\n * usage rollup. Several of the rollup's meters are a plugin's: the AI\n * plugin's provider spend, the CRM's records, the forms plugin's\n * submissions, the workflows plugin's runs. Core used to name each of them —\n * its rollup field, the unit rate that prices it, the band it is measured\n * against — so a plugin that metered something new could not reach the\n * discount guardrail without a core edit, and core could not be taken\n * without the plugins it named.\n *\n * So a plugin declares its meters, and core composes them:\n *\n * - a COST AXIS names the rollup fields a meter is recorded under and the\n * `ORG_COGS_UNIT_RATES_USD` key that prices one unit, or no key when the\n * field is already dollars. `orgMonthlyCogsUsd` prices it beside core's\n * own axes, and `orgCogsInputFrom` forwards its fields.\n * - a BAND names the rollup fields that measure it and the entitlement that\n * says what the plan includes. The utilization table reads it beside\n * core's own bands. A METERED band is also billed past what the plan\n * includes, at cost × `METERED_MARKUP`, beside storage and bandwidth: the\n * invoice sweep, the Billing card's estimate, the monthly summary and the\n * staff usage rows read it from here.\n * - a SPEND LINE names the month document holding what the plugin's usage\n * came to at the rates it bills, the deployment variable naming the month\n * it is first charged for, and the unit a customer sees it in. The usage\n * budget shows it beside the metered figure and counts it once charged.\n * - a METER names what the plugin measures in the monthly usage sweep\n * (`plugin-usage-meters.ts`), so the sweep can refuse to bill a month the\n * registered meter is missing from.\n *\n * ## What core keeps\n *\n * The MONEY. Every unit rate stays in `ORG_COGS_UNIT_RATES_USD`, beside the\n * billed table it is reconciled against (`METERED_UNIT_RATES_USD`, which a\n * metered band names its rate in), and every included figure stays in\n * `PLAN_ENTITLEMENTS`. A declaration names fields and keys; it carries no\n * price. The one number a declaration may carry is a band's unit cost, where\n * the band is sold in a unit OF cost (a credit is a fixed quantity of\n * dollars), and that number is the plugin's own definition of its unit.\n *\n * ## Compiled, never registered\n *\n * The cost model's readers include the staff org page, which prices a rollup\n * in the browser, and the discount guardrail, whose every wrong answer is in\n * the approving direction: a cost axis a registry had not filled would price\n * at nothing and approve a discount the margin cannot carry. So these are\n * compiled from each plugin's `usage-axes` declaration by the manifest\n * generator, like the plan figures, and a rate key core does not have is an\n * error rather than a zero.\n */\n\nimport type { ReleaseFlagKey } from '../app-utils/release-flags'\nimport {\n PLUGIN_COST_AXES_DECLARED,\n PLUGIN_SPEND_LINES_DECLARED,\n PLUGIN_USAGE_BANDS_DECLARED,\n} from './first-party-plugins.generated'\n\n/** A meter a plugin contributes to the platform's cost model. */\nexport interface PluginCostAxisDeclaration {\n /** The breakdown key every staff reader shows it under. */\n id: string\n /** Where it sorts among the cost model's axes; core's own are multiples of 10. */\n order: number\n /** Rollup fields summed for the axis. */\n fields: readonly string[]\n /**\n * Read only when every one of `fields` is absent from a rollup — the\n * narrower field an older rollup recorded the same meter under.\n */\n fallbackFields?: readonly string[]\n /**\n * Rollup fields recorded beside the meter so its sum is legible, forwarded\n * by `orgCogsInputFrom` and never priced.\n */\n recordedFields?: readonly string[]\n /**\n * Rollup fields the plugin's usage sweep writes beside the meter for its\n * staff surfaces to read — served on the staff usage rows, `null` where a\n * rollup never wrote one, and never forwarded to the cost model.\n */\n staffFields?: readonly string[]\n /**\n * The `ORG_COGS_UNIT_RATES_USD` key pricing one unit. Absent when the\n * fields are already dollars, which then enter the model at ×1.\n */\n rate?: string\n /**\n * The month's LIVE reading, when the rollup's figure is a snapshot a meter\n * keeps adding to: `orgs/{orgId}/{collection}/{month}`, the first of\n * `fields` that is a positive number. It replaces the axis's first rollup\n * field wherever a reader fetches it.\n */\n live?: { collection: string; fields: readonly string[] }\n}\n\n/** A band a plugin contributes to the utilization table. */\nexport interface PluginUsageBandDeclaration {\n /** The band's key, and the column the staff table shows it under. */\n id: string\n label: string\n order: number\n /** Rollup fields summed for what the org used. */\n fields: readonly string[]\n fallbackFields?: readonly string[]\n /** The resolved entitlement holding what the plan includes. */\n entitlement: string\n /** The entitlement is per SITE and the org-wide band is it × `hostLimit`. */\n perHost?: boolean\n /**\n * The band is sold in a unit of cost and the rollup records dollars: what\n * was used is `ceil(dollars / unitCostUsd)`, rounded UP so spend already\n * made never reads as less of the band.\n */\n unitCostUsd?: number\n /**\n * The per-site monthly counter the band is measured by:\n * `hosts/{hostId}/counters/{hostCounter}`, field `{month}`, summed over the\n * workspace's sites.\n */\n hostCounter?: string\n /**\n * The workspace is WARNED as it approaches and reaches this band, by the\n * usage-alerts sweep, from its `hostCounter` against what the plan includes\n * of `entitlement`, once per threshold per month. `label` names the band in\n * the title (\"monthly workflow runs\") and `noun` in the opening sentence\n * (\"workflow runs\"); `outcome` is what happens at the band — `stops`,\n * `bills` or `continues` — and `reached` and `approach` are the sentences\n * that say so, at the band and approaching it.\n */\n alert?: {\n label: string\n noun: string\n outcome: 'stops' | 'bills' | 'continues'\n reached: string\n approach: string\n }\n /**\n * The band is one of the INFRASTRUCTURE meters (AGL-1280): what the\n * workspace uses past it is billed at our cost × `METERED_MARKUP`, beside\n * storage and bandwidth, on the same invoice line and the same estimate.\n * A metered band names one rollup field and the `hostCounter` it is\n * measured by.\n */\n metered?: PluginMeteredBand\n}\n\n/** How a metered band is priced, quoted and withheld. */\nexport interface PluginMeteredBand {\n /**\n * The `METERED_UNIT_RATES_USD` key holding our cost per unit — never a\n * number: the rate table is the platform's, beside the markup it is\n * published with.\n */\n rate: string\n /** The count a published price is quoted per: `1000` reads \"per 1,000\". */\n quotedPer: number\n /** The band in running prose, plural and lowercase: \"form submissions\". */\n noun: string\n /**\n * A release flag the overage waits behind: while it is off for the\n * workspace, the units are counted and the charge is recorded as withheld\n * rather than billed — nobody is charged for what they cannot reach\n * (AGL-1604). The rollup records the verdict as `{field}Billed` and what\n * was forgone, at cost, as `{field}OverageWithheldUsd`.\n */\n withheldUntil?: ReleaseFlagKey\n}\n\n/**\n * A line of a workspace's monthly SPEND a plugin contributes to its usage\n * budget (`usage-budget.ts`): what the plugin's usage came to this month, in\n * the dollars it is billed at, shown on the customer's budget card and\n * counted toward the budget from the month the plugin starts charging for it.\n */\nexport interface PluginSpendLineDeclaration {\n /** The line's key. */\n id: string\n /** What the budget card and the budget alert call it. */\n label: string\n /**\n * Where the month's figure is read: `orgs/{orgId}/{collection}/{month}`,\n * field `field`, in dollars.\n */\n live: { collection: string; field: string }\n /**\n * The deployment variable naming the first month (`YYYY-MM`) the line is\n * charged for. Until it names this month or an earlier one — and on any\n * value that is not a month — the line is shown and never counted.\n */\n billedFromEnv: string\n /**\n * The unit the customer is shown the line in, when the stored dollars are\n * not theirs to see: `ceil(dollars / costUsd)` of `label`, and the dollar\n * figure never crosses to the browser.\n */\n unit?: { costUsd: number; label: string }\n}\n\n/**\n * A meter the plugin measures in the platform's monthly usage sweep and\n * registers at runtime (`plugin-usage-meters.ts`). Declared as well as\n * registered so the sweep can refuse to bill a workspace's month without it.\n */\nexport interface PluginUsageMeterDeclaration {\n /** As `registerPluginUsageMeter` names it. */\n id: string\n}\n\nexport type ResolvedPluginCostAxis = PluginCostAxisDeclaration & { pluginId: string }\nexport type ResolvedPluginUsageBand = PluginUsageBandDeclaration & { pluginId: string }\nexport type ResolvedPluginSpendLine = PluginSpendLineDeclaration & { pluginId: string }\nexport type ResolvedPluginUsageMeter = PluginUsageMeterDeclaration & { pluginId: string }\n\n/** What a plugin's `usageAxes` function answers. */\nexport interface PluginUsageAxesDeclaration {\n costAxes?: readonly PluginCostAxisDeclaration[]\n bands?: readonly PluginUsageBandDeclaration[]\n spendLines?: readonly PluginSpendLineDeclaration[]\n meters?: readonly PluginUsageMeterDeclaration[]\n}\n\nexport function pluginCostAxes(): readonly ResolvedPluginCostAxis[] {\n return PLUGIN_COST_AXES_DECLARED\n}\n\nexport function pluginUsageBands(): readonly ResolvedPluginUsageBand[] {\n return PLUGIN_USAGE_BANDS_DECLARED\n}\n\n/** A declared band that is metered, with its one field and its counter. */\nexport type ResolvedPluginMeteredBand = ResolvedPluginUsageBand & {\n metered: PluginMeteredBand\n hostCounter: string\n}\n\n/**\n * Every band billed at cost past what the plan includes, in band order — the\n * infrastructure meters a plugin adds beside storage and bandwidth.\n */\nexport function meteredPluginBands(): readonly ResolvedPluginMeteredBand[] {\n return PLUGIN_USAGE_BANDS_DECLARED.filter(\n (band): band is ResolvedPluginMeteredBand =>\n Boolean(band.metered && band.hostCounter),\n )\n}\n\n/** A declared band measured by a per-site counter and never billed past it. */\nexport type ResolvedPluginCountedBand = ResolvedPluginUsageBand & {\n hostCounter: string\n}\n\n/**\n * Every band measured by a per-site counter and not metered, in band order:\n * the usage sweep sums each across the workspace's sites and records it under\n * the band's first field, and the staff usage rows show the count. A metered\n * band's counter is read with the infrastructure meters instead.\n */\nexport function countedPluginBands(): readonly ResolvedPluginCountedBand[] {\n return PLUGIN_USAGE_BANDS_DECLARED.filter(\n (band): band is ResolvedPluginCountedBand =>\n Boolean(band.hostCounter && !band.metered),\n )\n}\n\n/** The rollup field a metered band records its count under. */\nexport function meteredBandField(band: ResolvedPluginMeteredBand): string {\n return band.fields[0]!\n}\n\n/**\n * Where the rollup records a withheld band's verdict (`metered.withheldUntil`):\n * whether the month billed the band's overage, and what was forgone, at cost,\n * when it did not. Without the second a withheld month reads as a month that\n * stayed inside the band.\n */\nexport function meteredBandVerdictFields(band: ResolvedPluginMeteredBand): {\n billed: string\n withheldUsd: string\n} {\n const field = meteredBandField(band)\n return { billed: `${field}Billed`, withheldUsd: `${field}OverageWithheldUsd` }\n}\n\nexport function pluginSpendLines(): readonly ResolvedPluginSpendLine[] {\n return PLUGIN_SPEND_LINES_DECLARED\n}\n\n/** A positive finite number, or 0 — the reading every meter is priced from. */\nfunction meterValue(value: unknown): number {\n const parsed = Number(value ?? 0)\n return Number.isFinite(parsed) && parsed > 0 ? parsed : 0\n}\n\n/**\n * What a rollup recorded for a declared meter: `fields` summed, or\n * `fallbackFields` summed when the rollup carries none of `fields` at all.\n *\n * \"Carries none\" is `null` or absent, never zero. A rollup that measured\n * zero has answered, and must not fall back to a figure recorded under an\n * older, narrower basis.\n */\nexport function declaredMeterReading(\n source: Readonly<Record<string, unknown>> | null | undefined,\n declared: {\n fields: readonly string[]\n fallbackFields?: readonly string[]\n },\n): number {\n const present = declared.fields.some((field) => source?.[field] != null)\n const fields =\n !present && declared.fallbackFields?.length\n ? declared.fallbackFields\n : declared.fields\n return fields.reduce((sum, field) => sum + meterValue(source?.[field]), 0)\n}\n\n/**\n * The live reading a cost axis names: the first of its `live.fields` that is\n * a positive number on the month's document, or `undefined` when none is.\n * `undefined` is \"keep the rollup's snapshot\", never zero.\n */\nexport function liveMeterReading(\n document: { get(field: string): unknown } | null | undefined,\n live: { fields: readonly string[] },\n): number | undefined {\n for (const field of live.fields) {\n const value = meterValue(document?.get(field))\n if (value > 0) return value\n }\n return undefined\n}\n\n/** Every rollup field the declared cost axes read or record, in order. */\nexport function pluginCostAxisFields(): string[] {\n const fields: string[] = []\n for (const axis of PLUGIN_COST_AXES_DECLARED) {\n for (const field of [\n ...axis.fields,\n ...(axis.fallbackFields ?? []),\n ...(axis.recordedFields ?? []),\n ]) {\n if (!fields.includes(field)) fields.push(field)\n }\n }\n return fields\n}\n\n/**\n * A month's rollup as a staff reader is served it, one entry per field the\n * declared cost axes read, record or serve to staff (`get` reads one rollup\n * field).\n *\n * A field an axis prices reads zero where the rollup lacks it, as the cost\n * model reads it — except where the axis falls back to an older basis when\n * the rollup carries none of its fields: there absence is the answer that\n * selects the fallback, so it is served as `null`, and a reader pricing the\n * served row reads the month the way the sweep did. A field an axis only\n * records or serves reads `null` when the rollup never wrote it: \"not\n * recorded\" and \"recorded zero\" are different answers, and a projection\n * that collapses them invents history (AGL-2321).\n */\nexport function pluginCostAxisProjection(\n get: (field: string) => unknown,\n): Record<string, number | null> {\n const out: Record<string, number | null> = {}\n for (const axis of PLUGIN_COST_AXES_DECLARED) {\n const absentIsNull = [\n ...(axis.fallbackFields?.length ? axis.fields : []),\n ...(axis.recordedFields ?? []),\n ...(axis.staffFields ?? []),\n ]\n for (const field of [\n ...axis.fields,\n ...(axis.fallbackFields ?? []),\n ...(axis.recordedFields ?? []),\n ...(axis.staffFields ?? []),\n ]) {\n if (field in out) continue\n const value = get(field)\n out[field] =\n value != null ? Number(value) : absentIsNull.includes(field) ? null : 0\n }\n }\n return out\n}\n"],"names":["PLUGIN_COST_AXES_DECLARED","PLUGIN_SPEND_LINES_DECLARED","PLUGIN_USAGE_BANDS_DECLARED","pluginCostAxes","pluginUsageBands","meteredPluginBands","filter","band","Boolean","metered","hostCounter","countedPluginBands","meteredBandField","fields","meteredBandVerdictFields","field","billed","withheldUsd","pluginSpendLines","meterValue","value","parsed","Number","isFinite","declaredMeterReading","source","declared","present","some","fallbackFields","length","reduce","sum","liveMeterReading","document","live","get","undefined","pluginCostAxisFields","axis","recordedFields","includes","push","pluginCostAxisProjection","out","absentIsNull","staffFields"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoDC,GAGD,SACEA,yBAAyB,EACzBC,2BAA2B,EAC3BC,2BAA2B,QACtB,qCAAiC;AAsKxC,OAAO,SAASC;IACd,OAAOH;AACT;AAEA,OAAO,SAASI;IACd,OAAOF;AACT;AAQA;;;CAGC,GACD,OAAO,SAASG;IACd,OAAOH,4BAA4BI,MAAM,CACvC,CAACC,OACCC,QAAQD,KAAKE,OAAO,IAAIF,KAAKG,WAAW;AAE9C;AAOA;;;;;CAKC,GACD,OAAO,SAASC;IACd,OAAOT,4BAA4BI,MAAM,CACvC,CAACC,OACCC,QAAQD,KAAKG,WAAW,IAAI,CAACH,KAAKE,OAAO;AAE/C;AAEA,6DAA6D,GAC7D,OAAO,SAASG,iBAAiBL,IAA+B;IAC9D,OAAOA,KAAKM,MAAM,CAAC,EAAE;AACvB;AAEA;;;;;CAKC,GACD,OAAO,SAASC,yBAAyBP,IAA+B;IAItE,MAAMQ,QAAQH,iBAAiBL;IAC/B,OAAO;QAAES,QAAQ,GAAGD,MAAM,MAAM,CAAC;QAAEE,aAAa,GAAGF,MAAM,kBAAkB,CAAC;IAAC;AAC/E;AAEA,OAAO,SAASG;IACd,OAAOjB;AACT;AAEA,6EAA6E,GAC7E,SAASkB,WAAWC,KAAc;IAChC,MAAMC,SAASC,OAAOF,gBAAAA,QAAS;IAC/B,OAAOE,OAAOC,QAAQ,CAACF,WAAWA,SAAS,IAAIA,SAAS;AAC1D;AAEA;;;;;;;CAOC,GACD,OAAO,SAASG,qBACdC,MAA4D,EAC5DC,QAGC;QAIaA;IAFd,MAAMC,UAAUD,SAASb,MAAM,CAACe,IAAI,CAAC,CAACb,QAAUU,CAAAA,0BAAAA,MAAQ,CAACV,MAAM,KAAI;IACnE,MAAMF,SACJ,CAACc,aAAWD,2BAAAA,SAASG,cAAc,qBAAvBH,yBAAyBI,MAAM,IACvCJ,SAASG,cAAc,GACvBH,SAASb,MAAM;IACrB,OAAOA,OAAOkB,MAAM,CAAC,CAACC,KAAKjB,QAAUiB,MAAMb,WAAWM,0BAAAA,MAAQ,CAACV,MAAM,GAAG;AAC1E;AAEA;;;;CAIC,GACD,OAAO,SAASkB,iBACdC,QAA4D,EAC5DC,IAAmC;IAEnC,KAAK,MAAMpB,SAASoB,KAAKtB,MAAM,CAAE;QAC/B,MAAMO,QAAQD,WAAWe,4BAAAA,SAAUE,GAAG,CAACrB;QACvC,IAAIK,QAAQ,GAAG,OAAOA;IACxB;IACA,OAAOiB;AACT;AAEA,wEAAwE,GACxE,OAAO,SAASC;IACd,MAAMzB,SAAmB,EAAE;IAC3B,KAAK,MAAM0B,QAAQvC,0BAA2B;YAGtCuC,sBACAA;QAHN,KAAK,MAAMxB,SAAS;eACfwB,KAAK1B,MAAM;gBACV0B,uBAAAA,KAAKV,cAAc,YAAnBU,uBAAuB,EAAE;gBACzBA,uBAAAA,KAAKC,cAAc,YAAnBD,uBAAuB,EAAE;SAC9B,CAAE;YACD,IAAI,CAAC1B,OAAO4B,QAAQ,CAAC1B,QAAQF,OAAO6B,IAAI,CAAC3B;QAC3C;IACF;IACA,OAAOF;AACT;AAEA;;;;;;;;;;;;;CAaC,GACD,OAAO,SAAS8B,yBACdP,GAA+B;IAE/B,MAAMQ,MAAqC,CAAC;IAC5C,KAAK,MAAML,QAAQvC,0BAA2B;YAGtCuC,sBACAA,mBAIAA,sBACAA,uBACAA;YARAA;QADN,MAAMM,eAAe;eACfN,EAAAA,wBAAAA,KAAKV,cAAc,qBAAnBU,sBAAqBT,MAAM,IAAGS,KAAK1B,MAAM,GAAG,EAAE;gBAC9C0B,uBAAAA,KAAKC,cAAc,YAAnBD,uBAAuB,EAAE;gBACzBA,oBAAAA,KAAKO,WAAW,YAAhBP,oBAAoB,EAAE;SAC3B;QACD,KAAK,MAAMxB,SAAS;eACfwB,KAAK1B,MAAM;gBACV0B,uBAAAA,KAAKV,cAAc,YAAnBU,uBAAuB,EAAE;gBACzBA,wBAAAA,KAAKC,cAAc,YAAnBD,wBAAuB,EAAE;gBACzBA,qBAAAA,KAAKO,WAAW,YAAhBP,qBAAoB,EAAE;SAC3B,CAAE;YACD,IAAIxB,SAAS6B,KAAK;YAClB,MAAMxB,QAAQgB,IAAIrB;YAClB6B,GAAG,CAAC7B,MAAM,GACRK,SAAS,OAAOE,OAAOF,SAASyB,aAAaJ,QAAQ,CAAC1B,SAAS,OAAO;QAC1E;IACF;IACA,OAAO6B;AACT"}
@@ -0,0 +1,108 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Aglyn LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ import type { ReleaseFlagKey } from '../app-utils/release-flags';
18
+ /** What the sweep tells a meter about one workspace's month. */
19
+ export interface PluginUsageMeterContext {
20
+ orgId: string;
21
+ /** The workspace document, as the sweep read it for every decision. */
22
+ org: Readonly<Record<string, unknown>>;
23
+ /** `YYYY-MM`, the month being measured. */
24
+ month: string;
25
+ /**
26
+ * Whether the month has CLOSED. A closed month is the one an invoice is
27
+ * about; an open one is measured daily for the budget and never billed.
28
+ */
29
+ closed: boolean;
30
+ /**
31
+ * The month's rollup as it stood before this run — for a stock meter, the
32
+ * last reading an earlier sweep stamped inside the month.
33
+ */
34
+ previous: Readonly<Record<string, unknown>>;
35
+ /**
36
+ * A release flag's verdict for this workspace, resolved as the sweep
37
+ * resolves every flag it gates billing on: bucketed by workspace, with its
38
+ * overrides and its tier.
39
+ */
40
+ releaseFlagOn: (key: ReleaseFlagKey) => boolean;
41
+ }
42
+ /** What a meter answers for one workspace's month. */
43
+ export interface PluginUsageMeterReading {
44
+ /**
45
+ * Written onto the month's rollup on every run, zeros included. A field the
46
+ * platform writes itself is never overwritten.
47
+ */
48
+ fields: Readonly<Record<string, unknown>>;
49
+ /**
50
+ * Written only while the month is OPEN — a stock meter's period-end reading,
51
+ * which a closed sweep then bills from. Never written for a closed month,
52
+ * so a re-run of a closed month reads the same input every time.
53
+ */
54
+ periodEndFields?: Readonly<Record<string, unknown>>;
55
+ /**
56
+ * Dollars that enter the month's billed figure. Rounded to cents per meter,
57
+ * like every plan-priced line beside it; 0 for a meter that bills nothing.
58
+ */
59
+ billedUsd: number;
60
+ /**
61
+ * For a stock meter on a CLOSED month: `true` when the reading came from a
62
+ * period-end stamp taken inside the month. The rollup's `stockBasis` is
63
+ * `period-end` when any stock reading was.
64
+ */
65
+ periodEndBasis?: boolean;
66
+ }
67
+ /** What a meter's close-out is told about one workspace's closed month. */
68
+ export interface PluginUsageMeterCloseContext extends PluginUsageMeterContext {
69
+ /**
70
+ * The workspace's Stripe customer, or `null` when it has none. Read only
71
+ * when asked, so a close-out with nothing to settle costs no read.
72
+ */
73
+ stripeCustomerId: () => Promise<string | null>;
74
+ }
75
+ /** A plugin's meter in the monthly usage sweep. */
76
+ export interface PluginUsageMeter {
77
+ pluginId: string;
78
+ /** As the plugin's `usageAxes` declaration names it under `meters`. */
79
+ id: string;
80
+ measure: (context: PluginUsageMeterContext) => Promise<PluginUsageMeterReading>;
81
+ /**
82
+ * Called once per workspace for a CLOSED month, every day the sweep runs,
83
+ * so a plugin that bills part of its usage itself can settle what is left
84
+ * of it. Idempotent by the plugin's own records; errors are logged.
85
+ */
86
+ closeMonth?: (context: PluginUsageMeterCloseContext) => Promise<void>;
87
+ }
88
+ /**
89
+ * Registers a meter. Idempotent per plugin and id: the same pair again
90
+ * replaces the earlier registration, so a declarations module evaluated twice
91
+ * measures once. A meter with no owner, no id or no `measure` throws.
92
+ */
93
+ export declare function registerPluginUsageMeter(meter: Omit<PluginUsageMeter, 'pluginId'> & {
94
+ pluginId?: string;
95
+ }): void;
96
+ /**
97
+ * Every registered meter, in the order the sweep runs them: declared meters in
98
+ * catalog order, then any other in registration order. The order is fixed so
99
+ * two processes write one workspace's rollup the same way.
100
+ */
101
+ export declare function listPluginUsageMeters(): PluginUsageMeter[];
102
+ /**
103
+ * Every DECLARED meter nothing registered, as `pluginId:id`. The sweep refuses
104
+ * to report a workspace's month while this is not empty.
105
+ */
106
+ export declare function unregisteredPluginUsageMeters(): string[];
107
+ /** Test seam: forget every meter. */
108
+ export declare function resetPluginUsageMetersForTests(): void;
@@ -0,0 +1,105 @@
1
+ import { _ as _extends } from "@swc/helpers/_/_extends";
2
+ /**
3
+ * @license
4
+ * Copyright 2026 Aglyn LLC
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */ /**
18
+ * A METER A PLUGIN MEASURES IN THE MONTHLY USAGE SWEEP (AGL-3080).
19
+ *
20
+ * The usage sweep (`/api/billing/report-usage`) writes one rollup per
21
+ * workspace and month — `orgs/{orgId}/usage/{month}` — and posts the month's
22
+ * billed figure as one Stripe meter event. The platform's own meters (storage,
23
+ * bandwidth, API requests, email) it measures itself. A plugin's meter it
24
+ * cannot: the plugin owns the collection the usage is recorded in, the band
25
+ * it is measured against and the price past the band.
26
+ *
27
+ * So the plugin registers a meter, and the sweep asks it once per workspace:
28
+ * `measure` answers the fields the plugin writes onto the rollup — under the
29
+ * names its usage axes declare, so the cost model and the utilization table
30
+ * read them — and the dollars that enter the month's billed figure. The sweep
31
+ * never learns what the meter counts or how it prices it, and the plugin
32
+ * never edits the sweep.
33
+ *
34
+ * ## Declared, then registered, and refused when missing
35
+ *
36
+ * A meter is a runtime registration because measuring is code — a read of the
37
+ * plugin's own storage, a band, a price. But a meter that failed to register
38
+ * is money the sweep would silently stop billing, so the plugin also DECLARES
39
+ * it (`usageAxes` → `meters`, compiled into the catalog file), and the sweep
40
+ * refuses to report a workspace's month while a declared meter is missing:
41
+ * the workspace fails loudly for this pass and is swept again tomorrow, which
42
+ * is late and visible, where a month billed without the meter would be short
43
+ * and final.
44
+ *
45
+ * ## A throw is the workspace's, not the sweep's
46
+ *
47
+ * `measure` throwing fails that workspace's pass the same way — a meter that
48
+ * could not measure has no figure worth billing. `closeMonth` is different:
49
+ * it settles what the plugin bills on its own, is logged when it fails, and
50
+ * never costs the platform's own metering.
51
+ */ import { getRegisteringPluginId } from "../app-utils/registering-plugin.js";
52
+ import { PLUGIN_USAGE_METERS_DECLARED } from "./first-party-plugins.generated.js";
53
+ const meters = new Map();
54
+ const keyOf = (pluginId, id)=>`${pluginId}:${id}`;
55
+ /**
56
+ * Registers a meter. Idempotent per plugin and id: the same pair again
57
+ * replaces the earlier registration, so a declarations module evaluated twice
58
+ * measures once. A meter with no owner, no id or no `measure` throws.
59
+ */ export function registerPluginUsageMeter(meter) {
60
+ var _ref, _getRegisteringPluginId, _meter_id;
61
+ const pluginId = ((_ref = (_getRegisteringPluginId = getRegisteringPluginId()) != null ? _getRegisteringPluginId : meter.pluginId) != null ? _ref : '').trim();
62
+ const id = String((_meter_id = meter.id) != null ? _meter_id : '').trim();
63
+ if (!pluginId || !id) {
64
+ throw new Error('a usage meter needs a pluginId and an id');
65
+ }
66
+ if (typeof meter.measure !== 'function') {
67
+ throw new Error(`usage meter "${keyOf(pluginId, id)}" has no measure function`);
68
+ }
69
+ meters.set(keyOf(pluginId, id), _extends({
70
+ pluginId,
71
+ id,
72
+ measure: meter.measure
73
+ }, typeof meter.closeMonth === 'function' ? {
74
+ closeMonth: meter.closeMonth
75
+ } : {}));
76
+ }
77
+ /**
78
+ * Every registered meter, in the order the sweep runs them: declared meters in
79
+ * catalog order, then any other in registration order. The order is fixed so
80
+ * two processes write one workspace's rollup the same way.
81
+ */ export function listPluginUsageMeters() {
82
+ const declared = PLUGIN_USAGE_METERS_DECLARED.map((one)=>keyOf(one.pluginId, one.id));
83
+ const rank = (key)=>{
84
+ const index = declared.indexOf(key);
85
+ return index === -1 ? Number.MAX_SAFE_INTEGER : index;
86
+ };
87
+ return [
88
+ ...meters.entries()
89
+ ].map(([key, meter], sequence)=>({
90
+ key,
91
+ meter,
92
+ sequence
93
+ })).sort((a, b)=>rank(a.key) - rank(b.key) || a.sequence - b.sequence).map(({ meter })=>_extends({}, meter));
94
+ }
95
+ /**
96
+ * Every DECLARED meter nothing registered, as `pluginId:id`. The sweep refuses
97
+ * to report a workspace's month while this is not empty.
98
+ */ export function unregisteredPluginUsageMeters() {
99
+ return PLUGIN_USAGE_METERS_DECLARED.map((one)=>keyOf(one.pluginId, one.id)).filter((key)=>!meters.has(key));
100
+ }
101
+ /** Test seam: forget every meter. */ export function resetPluginUsageMetersForTests() {
102
+ meters.clear();
103
+ }
104
+
105
+ //# sourceMappingURL=plugin-usage-meters.js.map