@contractspec/lib.metering 1.57.0 → 1.59.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.
Files changed (65) hide show
  1. package/dist/aggregation/index.d.ts +119 -122
  2. package/dist/aggregation/index.d.ts.map +1 -1
  3. package/dist/aggregation/index.js +257 -265
  4. package/dist/analytics/posthog-metering-reader.d.ts +32 -36
  5. package/dist/analytics/posthog-metering-reader.d.ts.map +1 -1
  6. package/dist/analytics/posthog-metering-reader.js +239 -179
  7. package/dist/analytics/posthog-metering.d.ts +30 -34
  8. package/dist/analytics/posthog-metering.d.ts.map +1 -1
  9. package/dist/analytics/posthog-metering.js +45 -45
  10. package/dist/browser/aggregation/index.js +265 -0
  11. package/dist/browser/analytics/posthog-metering-reader.js +266 -0
  12. package/dist/browser/analytics/posthog-metering.js +45 -0
  13. package/dist/browser/contracts/index.js +617 -0
  14. package/dist/browser/docs/index.js +62 -0
  15. package/dist/browser/docs/metering.docblock.js +62 -0
  16. package/dist/browser/entities/index.js +350 -0
  17. package/dist/browser/events.js +228 -0
  18. package/dist/browser/index.js +1879 -0
  19. package/dist/browser/metering.capability.js +28 -0
  20. package/dist/browser/metering.feature.js +52 -0
  21. package/dist/contracts/index.d.ts +1076 -1082
  22. package/dist/contracts/index.d.ts.map +1 -1
  23. package/dist/contracts/index.js +616 -1030
  24. package/dist/docs/index.d.ts +2 -1
  25. package/dist/docs/index.d.ts.map +1 -0
  26. package/dist/docs/index.js +63 -1
  27. package/dist/docs/metering.docblock.d.ts +2 -1
  28. package/dist/docs/metering.docblock.d.ts.map +1 -0
  29. package/dist/docs/metering.docblock.js +17 -22
  30. package/dist/entities/index.d.ts +186 -191
  31. package/dist/entities/index.d.ts.map +1 -1
  32. package/dist/entities/index.js +340 -407
  33. package/dist/events.d.ts +411 -417
  34. package/dist/events.d.ts.map +1 -1
  35. package/dist/events.js +226 -414
  36. package/dist/index.d.ts +9 -8
  37. package/dist/index.d.ts.map +1 -0
  38. package/dist/index.js +1880 -10
  39. package/dist/metering.capability.d.ts +2 -7
  40. package/dist/metering.capability.d.ts.map +1 -1
  41. package/dist/metering.capability.js +29 -33
  42. package/dist/metering.feature.d.ts +1 -6
  43. package/dist/metering.feature.d.ts.map +1 -1
  44. package/dist/metering.feature.js +51 -132
  45. package/dist/node/aggregation/index.js +265 -0
  46. package/dist/node/analytics/posthog-metering-reader.js +266 -0
  47. package/dist/node/analytics/posthog-metering.js +45 -0
  48. package/dist/node/contracts/index.js +617 -0
  49. package/dist/node/docs/index.js +62 -0
  50. package/dist/node/docs/metering.docblock.js +62 -0
  51. package/dist/node/entities/index.js +350 -0
  52. package/dist/node/events.js +228 -0
  53. package/dist/node/index.js +1879 -0
  54. package/dist/node/metering.capability.js +28 -0
  55. package/dist/node/metering.feature.js +52 -0
  56. package/package.json +133 -34
  57. package/dist/aggregation/index.js.map +0 -1
  58. package/dist/analytics/posthog-metering-reader.js.map +0 -1
  59. package/dist/analytics/posthog-metering.js.map +0 -1
  60. package/dist/contracts/index.js.map +0 -1
  61. package/dist/docs/metering.docblock.js.map +0 -1
  62. package/dist/entities/index.js.map +0 -1
  63. package/dist/events.js.map +0 -1
  64. package/dist/metering.capability.js.map +0 -1
  65. package/dist/metering.feature.js.map +0 -1
@@ -0,0 +1,617 @@
1
+ // src/contracts/index.ts
2
+ import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
3
+ import { defineCommand, defineQuery } from "@contractspec/lib.contracts";
4
+ var OWNERS = ["platform.metering"];
5
+ var MetricDefinitionModel = defineSchemaModel({
6
+ name: "MetricDefinition",
7
+ description: "Represents a metric definition",
8
+ fields: {
9
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
10
+ key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
11
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
12
+ description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
13
+ unit: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
14
+ aggregationType: {
15
+ type: ScalarTypeEnum.String_unsecure(),
16
+ isOptional: false
17
+ },
18
+ resetPeriod: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
19
+ category: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
20
+ isActive: { type: ScalarTypeEnum.Boolean(), isOptional: false },
21
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
22
+ createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false },
23
+ updatedAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
24
+ }
25
+ });
26
+ var UsageRecordModel = defineSchemaModel({
27
+ name: "UsageRecord",
28
+ description: "Represents a usage record",
29
+ fields: {
30
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
31
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
32
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
33
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
34
+ quantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
35
+ source: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
36
+ resourceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
37
+ resourceType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
38
+ metadata: { type: ScalarTypeEnum.JSON(), isOptional: true },
39
+ timestamp: { type: ScalarTypeEnum.DateTime(), isOptional: false },
40
+ createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
41
+ }
42
+ });
43
+ var UsageSummaryModel = defineSchemaModel({
44
+ name: "UsageSummary",
45
+ description: "Represents aggregated usage",
46
+ fields: {
47
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
48
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
49
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
50
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
51
+ periodType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
52
+ periodStart: { type: ScalarTypeEnum.DateTime(), isOptional: false },
53
+ periodEnd: { type: ScalarTypeEnum.DateTime(), isOptional: false },
54
+ totalQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
55
+ recordCount: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
56
+ minQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
57
+ maxQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
58
+ avgQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true }
59
+ }
60
+ });
61
+ var UsageThresholdModel = defineSchemaModel({
62
+ name: "UsageThreshold",
63
+ description: "Represents a usage threshold",
64
+ fields: {
65
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
66
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
67
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
68
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
69
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
70
+ threshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
71
+ warnThreshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
72
+ periodType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
73
+ action: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
74
+ currentValue: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
75
+ isActive: { type: ScalarTypeEnum.Boolean(), isOptional: false },
76
+ createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
77
+ }
78
+ });
79
+ var DefineMetricInput = defineSchemaModel({
80
+ name: "DefineMetricInput",
81
+ description: "Input for defining a metric",
82
+ fields: {
83
+ key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
84
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
85
+ description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
86
+ unit: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
87
+ aggregationType: {
88
+ type: ScalarTypeEnum.String_unsecure(),
89
+ isOptional: true
90
+ },
91
+ resetPeriod: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
92
+ category: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
93
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
94
+ metadata: { type: ScalarTypeEnum.JSON(), isOptional: true }
95
+ }
96
+ });
97
+ var UpdateMetricInput = defineSchemaModel({
98
+ name: "UpdateMetricInput",
99
+ description: "Input for updating a metric",
100
+ fields: {
101
+ metricId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
102
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
103
+ description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
104
+ category: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
105
+ isActive: { type: ScalarTypeEnum.Boolean(), isOptional: true },
106
+ metadata: { type: ScalarTypeEnum.JSON(), isOptional: true }
107
+ }
108
+ });
109
+ var DeleteMetricInput = defineSchemaModel({
110
+ name: "DeleteMetricInput",
111
+ description: "Input for deleting a metric",
112
+ fields: {
113
+ metricId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
114
+ }
115
+ });
116
+ var GetMetricInput = defineSchemaModel({
117
+ name: "GetMetricInput",
118
+ description: "Input for getting a metric",
119
+ fields: {
120
+ key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
121
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
122
+ }
123
+ });
124
+ var ListMetricsInput = defineSchemaModel({
125
+ name: "ListMetricsInput",
126
+ description: "Input for listing metrics",
127
+ fields: {
128
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
129
+ category: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
130
+ isActive: { type: ScalarTypeEnum.Boolean(), isOptional: true },
131
+ limit: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true },
132
+ offset: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true }
133
+ }
134
+ });
135
+ var ListMetricsOutput = defineSchemaModel({
136
+ name: "ListMetricsOutput",
137
+ description: "Output for listing metrics",
138
+ fields: {
139
+ metrics: { type: MetricDefinitionModel, isArray: true, isOptional: false },
140
+ total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false }
141
+ }
142
+ });
143
+ var RecordUsageInput = defineSchemaModel({
144
+ name: "RecordUsageInput",
145
+ description: "Input for recording usage",
146
+ fields: {
147
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
148
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
149
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
150
+ quantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
151
+ timestamp: { type: ScalarTypeEnum.DateTime(), isOptional: true },
152
+ source: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
153
+ resourceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
154
+ resourceType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
155
+ metadata: { type: ScalarTypeEnum.JSON(), isOptional: true },
156
+ idempotencyKey: {
157
+ type: ScalarTypeEnum.String_unsecure(),
158
+ isOptional: true
159
+ }
160
+ }
161
+ });
162
+ var RecordBatchUsageInput = defineSchemaModel({
163
+ name: "RecordBatchUsageInput",
164
+ description: "Input for recording batch usage",
165
+ fields: {
166
+ records: { type: RecordUsageInput, isArray: true, isOptional: false }
167
+ }
168
+ });
169
+ var RecordBatchUsageOutput = defineSchemaModel({
170
+ name: "RecordBatchUsageOutput",
171
+ description: "Output for recording batch usage",
172
+ fields: {
173
+ recordedCount: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
174
+ skippedCount: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
175
+ recordIds: { type: ScalarTypeEnum.JSON(), isOptional: false }
176
+ }
177
+ });
178
+ var GetUsageInput = defineSchemaModel({
179
+ name: "GetUsageInput",
180
+ description: "Input for getting usage",
181
+ fields: {
182
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
183
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
184
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
185
+ startDate: { type: ScalarTypeEnum.DateTime(), isOptional: false },
186
+ endDate: { type: ScalarTypeEnum.DateTime(), isOptional: false },
187
+ limit: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true },
188
+ offset: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true }
189
+ }
190
+ });
191
+ var GetUsageOutput = defineSchemaModel({
192
+ name: "GetUsageOutput",
193
+ description: "Output for getting usage",
194
+ fields: {
195
+ records: { type: UsageRecordModel, isArray: true, isOptional: false },
196
+ total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
197
+ totalQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false }
198
+ }
199
+ });
200
+ var GetUsageSummaryInput = defineSchemaModel({
201
+ name: "GetUsageSummaryInput",
202
+ description: "Input for getting usage summary",
203
+ fields: {
204
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
205
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
206
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
207
+ periodType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
208
+ startDate: { type: ScalarTypeEnum.DateTime(), isOptional: false },
209
+ endDate: { type: ScalarTypeEnum.DateTime(), isOptional: true }
210
+ }
211
+ });
212
+ var GetUsageSummaryOutput = defineSchemaModel({
213
+ name: "GetUsageSummaryOutput",
214
+ description: "Output for getting usage summary",
215
+ fields: {
216
+ summaries: { type: UsageSummaryModel, isArray: true, isOptional: false },
217
+ total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false }
218
+ }
219
+ });
220
+ var CreateThresholdInput = defineSchemaModel({
221
+ name: "CreateThresholdInput",
222
+ description: "Input for creating a threshold",
223
+ fields: {
224
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
225
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
226
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
227
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
228
+ threshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
229
+ warnThreshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
230
+ periodType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
231
+ action: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
232
+ notifyEmails: { type: ScalarTypeEnum.JSON(), isOptional: true },
233
+ notifyWebhook: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
234
+ }
235
+ });
236
+ var UpdateThresholdInput = defineSchemaModel({
237
+ name: "UpdateThresholdInput",
238
+ description: "Input for updating a threshold",
239
+ fields: {
240
+ thresholdId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
241
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
242
+ threshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
243
+ warnThreshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
244
+ action: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
245
+ notifyEmails: { type: ScalarTypeEnum.JSON(), isOptional: true },
246
+ notifyWebhook: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
247
+ isActive: { type: ScalarTypeEnum.Boolean(), isOptional: true }
248
+ }
249
+ });
250
+ var DeleteThresholdInput = defineSchemaModel({
251
+ name: "DeleteThresholdInput",
252
+ description: "Input for deleting a threshold",
253
+ fields: {
254
+ thresholdId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
255
+ }
256
+ });
257
+ var ListThresholdsInput = defineSchemaModel({
258
+ name: "ListThresholdsInput",
259
+ description: "Input for listing thresholds",
260
+ fields: {
261
+ metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
262
+ subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
263
+ subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
264
+ isActive: { type: ScalarTypeEnum.Boolean(), isOptional: true }
265
+ }
266
+ });
267
+ var ListThresholdsOutput = defineSchemaModel({
268
+ name: "ListThresholdsOutput",
269
+ description: "Output for listing thresholds",
270
+ fields: {
271
+ thresholds: { type: UsageThresholdModel, isArray: true, isOptional: false },
272
+ total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false }
273
+ }
274
+ });
275
+ var SuccessOutput = defineSchemaModel({
276
+ name: "SuccessOutput",
277
+ description: "Generic success output",
278
+ fields: {
279
+ success: { type: ScalarTypeEnum.Boolean(), isOptional: false }
280
+ }
281
+ });
282
+ var DefineMetricContract = defineCommand({
283
+ meta: {
284
+ key: "metric.define",
285
+ version: "1.0.0",
286
+ stability: "stable",
287
+ owners: [...OWNERS],
288
+ tags: ["metering", "metric", "define"],
289
+ description: "Define a new usage metric.",
290
+ goal: "Create a new metric for tracking usage.",
291
+ context: "Called when setting up metering."
292
+ },
293
+ io: {
294
+ input: DefineMetricInput,
295
+ output: MetricDefinitionModel,
296
+ errors: {
297
+ METRIC_KEY_EXISTS: {
298
+ description: "Metric key already exists",
299
+ http: 409,
300
+ gqlCode: "METRIC_KEY_EXISTS",
301
+ when: "A metric with this key already exists"
302
+ }
303
+ }
304
+ },
305
+ policy: {
306
+ auth: "admin"
307
+ }
308
+ });
309
+ var UpdateMetricContract = defineCommand({
310
+ meta: {
311
+ key: "metric.update",
312
+ version: "1.0.0",
313
+ stability: "stable",
314
+ owners: [...OWNERS],
315
+ tags: ["metering", "metric", "update"],
316
+ description: "Update a metric definition.",
317
+ goal: "Modify metric configuration.",
318
+ context: "Called when updating metric settings."
319
+ },
320
+ io: {
321
+ input: UpdateMetricInput,
322
+ output: MetricDefinitionModel,
323
+ errors: {
324
+ METRIC_NOT_FOUND: {
325
+ description: "Metric does not exist",
326
+ http: 404,
327
+ gqlCode: "METRIC_NOT_FOUND",
328
+ when: "Metric ID is invalid"
329
+ }
330
+ }
331
+ },
332
+ policy: {
333
+ auth: "admin"
334
+ }
335
+ });
336
+ var DeleteMetricContract = defineCommand({
337
+ meta: {
338
+ key: "metric.delete",
339
+ version: "1.0.0",
340
+ stability: "stable",
341
+ owners: [...OWNERS],
342
+ tags: ["metering", "metric", "delete"],
343
+ description: "Delete a metric definition.",
344
+ goal: "Remove a metric and its data.",
345
+ context: "Called when removing a metric."
346
+ },
347
+ io: {
348
+ input: DeleteMetricInput,
349
+ output: SuccessOutput,
350
+ errors: {
351
+ METRIC_NOT_FOUND: {
352
+ description: "Metric does not exist",
353
+ http: 404,
354
+ gqlCode: "METRIC_NOT_FOUND",
355
+ when: "Metric ID is invalid"
356
+ }
357
+ }
358
+ },
359
+ policy: {
360
+ auth: "admin"
361
+ }
362
+ });
363
+ var GetMetricContract = defineQuery({
364
+ meta: {
365
+ key: "metric.get",
366
+ version: "1.0.0",
367
+ stability: "stable",
368
+ owners: [...OWNERS],
369
+ tags: ["metering", "metric", "get"],
370
+ description: "Get a metric by key.",
371
+ goal: "Retrieve metric definition.",
372
+ context: "Called to inspect metric details."
373
+ },
374
+ io: {
375
+ input: GetMetricInput,
376
+ output: MetricDefinitionModel,
377
+ errors: {
378
+ METRIC_NOT_FOUND: {
379
+ description: "Metric does not exist",
380
+ http: 404,
381
+ gqlCode: "METRIC_NOT_FOUND",
382
+ when: "Metric key is invalid"
383
+ }
384
+ }
385
+ },
386
+ policy: {
387
+ auth: "user"
388
+ }
389
+ });
390
+ var ListMetricsContract = defineQuery({
391
+ meta: {
392
+ key: "metric.list",
393
+ version: "1.0.0",
394
+ stability: "stable",
395
+ owners: [...OWNERS],
396
+ tags: ["metering", "metric", "list"],
397
+ description: "List all metrics.",
398
+ goal: "View configured metrics.",
399
+ context: "Called to browse metrics."
400
+ },
401
+ io: {
402
+ input: ListMetricsInput,
403
+ output: ListMetricsOutput
404
+ },
405
+ policy: {
406
+ auth: "user"
407
+ }
408
+ });
409
+ var RecordUsageContract = defineCommand({
410
+ meta: {
411
+ key: "usage.record",
412
+ version: "1.0.0",
413
+ stability: "stable",
414
+ owners: [...OWNERS],
415
+ tags: ["metering", "usage", "record"],
416
+ description: "Record a usage event.",
417
+ goal: "Track usage for billing and monitoring.",
418
+ context: "Called when usage occurs."
419
+ },
420
+ io: {
421
+ input: RecordUsageInput,
422
+ output: UsageRecordModel,
423
+ errors: {
424
+ METRIC_NOT_FOUND: {
425
+ description: "Metric does not exist",
426
+ http: 404,
427
+ gqlCode: "METRIC_NOT_FOUND",
428
+ when: "Metric key is invalid"
429
+ },
430
+ DUPLICATE_RECORD: {
431
+ description: "Record already exists",
432
+ http: 409,
433
+ gqlCode: "DUPLICATE_RECORD",
434
+ when: "Idempotency key already used"
435
+ }
436
+ }
437
+ },
438
+ policy: {
439
+ auth: "admin"
440
+ }
441
+ });
442
+ var RecordBatchUsageContract = defineCommand({
443
+ meta: {
444
+ key: "usage.recordBatch",
445
+ version: "1.0.0",
446
+ stability: "stable",
447
+ owners: [...OWNERS],
448
+ tags: ["metering", "usage", "batch"],
449
+ description: "Record multiple usage events.",
450
+ goal: "Efficiently track bulk usage.",
451
+ context: "Called for batch processing."
452
+ },
453
+ io: {
454
+ input: RecordBatchUsageInput,
455
+ output: RecordBatchUsageOutput
456
+ },
457
+ policy: {
458
+ auth: "admin"
459
+ }
460
+ });
461
+ var GetUsageContract = defineQuery({
462
+ meta: {
463
+ key: "usage.get",
464
+ version: "1.0.0",
465
+ stability: "stable",
466
+ owners: [...OWNERS],
467
+ tags: ["metering", "usage", "get"],
468
+ description: "Get usage records for a subject.",
469
+ goal: "View detailed usage history.",
470
+ context: "Called to analyze usage."
471
+ },
472
+ io: {
473
+ input: GetUsageInput,
474
+ output: GetUsageOutput
475
+ },
476
+ policy: {
477
+ auth: "user"
478
+ }
479
+ });
480
+ var GetUsageSummaryContract = defineQuery({
481
+ meta: {
482
+ key: "usage.getSummary",
483
+ version: "1.0.0",
484
+ stability: "stable",
485
+ owners: [...OWNERS],
486
+ tags: ["metering", "usage", "summary"],
487
+ description: "Get aggregated usage summary.",
488
+ goal: "View usage totals for billing.",
489
+ context: "Called for billing and reporting."
490
+ },
491
+ io: {
492
+ input: GetUsageSummaryInput,
493
+ output: GetUsageSummaryOutput
494
+ },
495
+ policy: {
496
+ auth: "user"
497
+ }
498
+ });
499
+ var CreateThresholdContract = defineCommand({
500
+ meta: {
501
+ key: "threshold.create",
502
+ version: "1.0.0",
503
+ stability: "stable",
504
+ owners: [...OWNERS],
505
+ tags: ["metering", "threshold", "create"],
506
+ description: "Create a usage threshold.",
507
+ goal: "Set up usage limits and alerts.",
508
+ context: "Called when configuring limits."
509
+ },
510
+ io: {
511
+ input: CreateThresholdInput,
512
+ output: UsageThresholdModel,
513
+ errors: {
514
+ METRIC_NOT_FOUND: {
515
+ description: "Metric does not exist",
516
+ http: 404,
517
+ gqlCode: "METRIC_NOT_FOUND",
518
+ when: "Metric key is invalid"
519
+ }
520
+ }
521
+ },
522
+ policy: {
523
+ auth: "admin"
524
+ }
525
+ });
526
+ var UpdateThresholdContract = defineCommand({
527
+ meta: {
528
+ key: "threshold.update",
529
+ version: "1.0.0",
530
+ stability: "stable",
531
+ owners: [...OWNERS],
532
+ tags: ["metering", "threshold", "update"],
533
+ description: "Update a threshold.",
534
+ goal: "Modify threshold configuration.",
535
+ context: "Called when adjusting limits."
536
+ },
537
+ io: {
538
+ input: UpdateThresholdInput,
539
+ output: UsageThresholdModel,
540
+ errors: {
541
+ THRESHOLD_NOT_FOUND: {
542
+ description: "Threshold does not exist",
543
+ http: 404,
544
+ gqlCode: "THRESHOLD_NOT_FOUND",
545
+ when: "Threshold ID is invalid"
546
+ }
547
+ }
548
+ },
549
+ policy: {
550
+ auth: "admin"
551
+ }
552
+ });
553
+ var DeleteThresholdContract = defineCommand({
554
+ meta: {
555
+ key: "threshold.delete",
556
+ version: "1.0.0",
557
+ stability: "stable",
558
+ owners: [...OWNERS],
559
+ tags: ["metering", "threshold", "delete"],
560
+ description: "Delete a threshold.",
561
+ goal: "Remove a usage threshold.",
562
+ context: "Called when removing limits."
563
+ },
564
+ io: {
565
+ input: DeleteThresholdInput,
566
+ output: SuccessOutput,
567
+ errors: {
568
+ THRESHOLD_NOT_FOUND: {
569
+ description: "Threshold does not exist",
570
+ http: 404,
571
+ gqlCode: "THRESHOLD_NOT_FOUND",
572
+ when: "Threshold ID is invalid"
573
+ }
574
+ }
575
+ },
576
+ policy: {
577
+ auth: "admin"
578
+ }
579
+ });
580
+ var ListThresholdsContract = defineQuery({
581
+ meta: {
582
+ key: "threshold.list",
583
+ version: "1.0.0",
584
+ stability: "stable",
585
+ owners: [...OWNERS],
586
+ tags: ["metering", "threshold", "list"],
587
+ description: "List usage thresholds.",
588
+ goal: "View configured limits.",
589
+ context: "Called to browse thresholds."
590
+ },
591
+ io: {
592
+ input: ListThresholdsInput,
593
+ output: ListThresholdsOutput
594
+ },
595
+ policy: {
596
+ auth: "user"
597
+ }
598
+ });
599
+ export {
600
+ UsageThresholdModel,
601
+ UsageSummaryModel,
602
+ UsageRecordModel,
603
+ UpdateThresholdContract,
604
+ UpdateMetricContract,
605
+ RecordUsageContract,
606
+ RecordBatchUsageContract,
607
+ MetricDefinitionModel,
608
+ ListThresholdsContract,
609
+ ListMetricsContract,
610
+ GetUsageSummaryContract,
611
+ GetUsageContract,
612
+ GetMetricContract,
613
+ DeleteThresholdContract,
614
+ DeleteMetricContract,
615
+ DefineMetricContract,
616
+ CreateThresholdContract
617
+ };
@@ -0,0 +1,62 @@
1
+ // src/docs/metering.docblock.ts
2
+ import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
3
+ var meteringDocBlocks = [
4
+ {
5
+ id: "docs.metering.usage",
6
+ title: "Usage Metering & Billing Core",
7
+ summary: "Reusable usage/metering layer with metric definitions, usage ingestion, aggregation, thresholds, and alerts for billing or quotas.",
8
+ kind: "reference",
9
+ visibility: "public",
10
+ route: "/docs/metering/usage",
11
+ tags: ["metering", "usage", "billing", "quotas"],
12
+ body: `## Capabilities
13
+
14
+ - **Entities**: MetricDefinition, UsageRecord, UsageSummary, UsageThreshold, UsageAlert.
15
+ - **Contracts**: define/list metrics; record usage (batch + idempotent); retrieve usage by subject; manage thresholds and alerts.
16
+ - **Aggregation**: hourly/daily/weekly/monthly rollups with SUM/COUNT/AVG/MIN/MAX/LAST strategies.
17
+ - **Events**: usage.recorded, usage.aggregated, threshold.exceeded (see events export).
18
+
19
+ ## Usage
20
+
21
+ 1) Compose schema
22
+ - Add \`meteringSchemaContribution\` to your schema composition.
23
+
24
+ 2) Register contracts/events
25
+ - Import from \`@contractspec/lib.metering\` into your spec registry.
26
+
27
+ 3) Ingest usage
28
+ - Use \`recordUsage\` contract (see contracts export) from application services whenever a billable/important action happens (e.g., agent run, API call).
29
+
30
+ 4) Aggregate + thresholds
31
+ - Run scheduled jobs using \`aggregateUsage\` helper to roll up summaries.
32
+ - Configure \`UsageThreshold\` to emit alerts or blocking actions; pipe alerts into Notifications/Audit.
33
+ - Use \`PosthogMeteringReader\` to backfill or validate usage from PostHog events.
34
+
35
+ ## Example
36
+
37
+ ${"```"}ts
38
+ import { meteringSchemaContribution } from '@contractspec/lib.metering';
39
+ import { aggregateUsage } from '@contractspec/lib.metering/aggregation';
40
+
41
+ // schema composition
42
+ const schema = {
43
+ modules: [meteringSchemaContribution],
44
+ };
45
+
46
+ // aggregation job
47
+ await aggregateUsage({
48
+ metrics: metricRepository,
49
+ usage: usageRepository,
50
+ period: 'DAILY',
51
+ });
52
+ ${"```"},
53
+
54
+ ## Guardrails
55
+
56
+ - Keep metric keys stable; store quantities as decimals for currency/units.
57
+ - Use idempotency keys for external ingestion; avoid PII in metric metadata.
58
+ - Scope by org/user for multi-tenant isolation; emit audit + analytics events on changes.
59
+ `
60
+ }
61
+ ];
62
+ registerDocBlocks(meteringDocBlocks);