@clocknext/sdk 0.1.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +140 -23
- package/dist/index.cjs +602 -58
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +915 -62
- package/dist/index.d.ts +915 -62
- package/dist/index.js +603 -58
- package/dist/index.js.map +1 -1
- package/package.json +5 -2
package/dist/index.d.ts
CHANGED
|
@@ -8,12 +8,44 @@
|
|
|
8
8
|
*/
|
|
9
9
|
type SignalType = "wallet" | "credit" | "outcome";
|
|
10
10
|
type SignalStatus = "SUCCESS" | "FAILURE" | "PENDING";
|
|
11
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* Token counts for a signal. `input` and `output` are required — every metered
|
|
13
|
+
* call has them, and the server requires them. `cache` (provider-specific
|
|
14
|
+
* prompt-cache reads) is optional and defaults to 0 server-side when omitted.
|
|
15
|
+
*/
|
|
12
16
|
interface Tokens {
|
|
13
|
-
input
|
|
14
|
-
output
|
|
17
|
+
input: number;
|
|
18
|
+
output: number;
|
|
15
19
|
cache?: number;
|
|
16
20
|
}
|
|
21
|
+
/** The organisation + key behind a request — `cnk.workspace.me()`. */
|
|
22
|
+
interface WorkspaceIdentity {
|
|
23
|
+
organization: {
|
|
24
|
+
id: string;
|
|
25
|
+
name: string;
|
|
26
|
+
slug: string | null;
|
|
27
|
+
/** `sandbox` is a disposable staging twin; `live` bills for real. Check
|
|
28
|
+
* this before sending production traffic. */
|
|
29
|
+
environment: "sandbox" | "live";
|
|
30
|
+
};
|
|
31
|
+
apiKey: {
|
|
32
|
+
prefix: string;
|
|
33
|
+
lastFour: string;
|
|
34
|
+
expiresAt: string | null;
|
|
35
|
+
} | null;
|
|
36
|
+
}
|
|
37
|
+
/** One model the org has enabled — `cnk.workspace.models()`. The `modelId` is
|
|
38
|
+
* what you put in a signal's `model`. Prices are USD per 1,000,000 tokens. */
|
|
39
|
+
interface Model {
|
|
40
|
+
modelId: string;
|
|
41
|
+
modelName: string;
|
|
42
|
+
provider: string;
|
|
43
|
+
providerName: string;
|
|
44
|
+
isActive: boolean;
|
|
45
|
+
inputPrice: number;
|
|
46
|
+
outputPrice: number;
|
|
47
|
+
cachePrice: number;
|
|
48
|
+
}
|
|
17
49
|
interface SignalBase {
|
|
18
50
|
/** The customer this usage belongs to (required). */
|
|
19
51
|
customerId: string;
|
|
@@ -22,35 +54,37 @@ interface SignalBase {
|
|
|
22
54
|
* (Settings → Models). Matched case-insensitively against `OrgModel.modelId`.
|
|
23
55
|
*/
|
|
24
56
|
model: string;
|
|
25
|
-
/** Token counts for this call
|
|
26
|
-
|
|
57
|
+
/** Token counts for this call — required; every metered signal is priced on
|
|
58
|
+
* tokens. (`cache` within is optional; `input`/`output` are not.) */
|
|
59
|
+
tokens: Tokens;
|
|
27
60
|
/** Optional member (email) this usage is attributed to. */
|
|
28
61
|
member?: string;
|
|
29
|
-
/** Defaults to SUCCESS. */
|
|
30
|
-
status?: SignalStatus;
|
|
31
62
|
/** Free-form metadata. Credit rules can target `custom.<key>` paths. */
|
|
32
63
|
custom?: Record<string, unknown>;
|
|
33
64
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
65
|
+
* Idempotency key for safe retries. **Auto-generated (UUID v4) if omitted**
|
|
66
|
+
* and reused across every retry of this signal, so a retried send is
|
|
67
|
+
* deduplicated server-side instead of double-recording. Supply your own — a
|
|
68
|
+
* stable id for this logical event — to also dedup across process restarts or
|
|
69
|
+
* at-least-once redelivery, which an in-memory auto key can't survive.
|
|
36
70
|
*/
|
|
37
71
|
idempotencyKey?: string;
|
|
38
72
|
}
|
|
39
|
-
/** Meters a credit, identified by the credit's stable `
|
|
73
|
+
/** Meters a credit, identified by the credit's stable `agentKey`. */
|
|
40
74
|
interface CreditSignal extends SignalBase {
|
|
41
75
|
type: "credit";
|
|
42
|
-
/** The Credit's stable key (assigned when the credit was created). */
|
|
43
|
-
|
|
76
|
+
/** The Credit's stable agent key (assigned when the credit was created). */
|
|
77
|
+
agentKey: string;
|
|
44
78
|
}
|
|
45
79
|
/** Debits the customer's wallet in USD at the model's cost. */
|
|
46
80
|
interface WalletSignal extends SignalBase {
|
|
47
81
|
type: "wallet";
|
|
48
82
|
}
|
|
49
|
-
/** Advances one step of a multi-call outcome workflow, by the step's `
|
|
83
|
+
/** Advances one step of a multi-call outcome workflow, by the step's `agentKey`. */
|
|
50
84
|
interface OutcomeSignal extends SignalBase {
|
|
51
85
|
type: "outcome";
|
|
52
|
-
/** The OutcomeStep's stable key — identifies the outcome AND the step. */
|
|
53
|
-
|
|
86
|
+
/** The OutcomeStep's stable agent key — identifies the outcome AND the step. */
|
|
87
|
+
agentKey: string;
|
|
54
88
|
}
|
|
55
89
|
type Signal = CreditSignal | WalletSignal | OutcomeSignal;
|
|
56
90
|
/** Helper input shapes for the per-type convenience methods. */
|
|
@@ -91,8 +125,6 @@ interface SerializedUsageLog {
|
|
|
91
125
|
}
|
|
92
126
|
/** Result of a single signal send (sync mode, or `{ wait: true }`). */
|
|
93
127
|
interface TrackResult {
|
|
94
|
-
/** The idempotency key the event was sent with. */
|
|
95
|
-
idempotencyKey: string;
|
|
96
128
|
/** True when the signal was buffered (async mode) and not yet sent. */
|
|
97
129
|
queued: boolean;
|
|
98
130
|
/** The computed usage log — present only once the signal has been sent. */
|
|
@@ -157,9 +189,10 @@ interface CustomerProfileInput {
|
|
|
157
189
|
/** ISO 4217 (3 letters); must be enabled for the org. */
|
|
158
190
|
currencyCode?: string;
|
|
159
191
|
}
|
|
160
|
-
/** `name`
|
|
192
|
+
/** `name` and `email` are required on create; every other field is optional. */
|
|
161
193
|
type CreateCustomerInput = CustomerProfileInput & {
|
|
162
194
|
name: string;
|
|
195
|
+
email: string;
|
|
163
196
|
};
|
|
164
197
|
interface CustomerList {
|
|
165
198
|
customers: ApiCustomer[];
|
|
@@ -218,6 +251,589 @@ interface PortalToken {
|
|
|
218
251
|
/** Seconds until expiry. */
|
|
219
252
|
expiresIn: number;
|
|
220
253
|
}
|
|
254
|
+
/** How often a plan bills. `FREE` is a one-time grant; `EVERY_5_MIN` is a test cadence. */
|
|
255
|
+
type BillingCycle = "MONTHLY" | "QUARTERLY" | "SEMI_ANNUAL" | "YEARLY" | "EVERY_5_MIN" | "FREE";
|
|
256
|
+
type PlanType = "WALLET" | "BUNDLE";
|
|
257
|
+
type PlanKind = "FLAT_PRICE" | "PAY_AS_YOU_GO" | "PAY_PER_SEAT";
|
|
258
|
+
/** A plan component's meter type. */
|
|
259
|
+
type ComponentType = "WALLET" | "CREDIT" | "OUTCOME" | "UNIT" | "FLAT";
|
|
260
|
+
/** ADVANCE bills up-front for the cycle; ARREAR bills for what was consumed. */
|
|
261
|
+
type BillingMode = "ADVANCE" | "ARREAR";
|
|
262
|
+
/** Prepaid USD wallet. `amount` is the funded balance (required). Max one
|
|
263
|
+
* wallet component per plan. */
|
|
264
|
+
interface WalletComponentInput {
|
|
265
|
+
type: "WALLET";
|
|
266
|
+
billingMode: BillingMode;
|
|
267
|
+
amount: number;
|
|
268
|
+
}
|
|
269
|
+
/** A one-off flat fee — `amount` is required. Max one flat component per plan;
|
|
270
|
+
* not allowed on FREE plans. */
|
|
271
|
+
interface FlatComponentInput {
|
|
272
|
+
type: "FLAT";
|
|
273
|
+
billingMode: BillingMode;
|
|
274
|
+
amount: number;
|
|
275
|
+
}
|
|
276
|
+
/** A credit entitlement, referenced by `creditId`. Billed ADVANCE it needs a
|
|
277
|
+
* granted `quantity`; billed ARREAR it meters usage with no upfront grant. */
|
|
278
|
+
type CreditComponentInput = {
|
|
279
|
+
type: "CREDIT";
|
|
280
|
+
billingMode: "ADVANCE";
|
|
281
|
+
creditId: string;
|
|
282
|
+
quantity: number;
|
|
283
|
+
} | {
|
|
284
|
+
type: "CREDIT";
|
|
285
|
+
billingMode: "ARREAR";
|
|
286
|
+
creditId: string;
|
|
287
|
+
quantity?: number | null;
|
|
288
|
+
};
|
|
289
|
+
/** An outcome entitlement, referenced by `outcomeId`. ADVANCE needs a granted
|
|
290
|
+
* `quantity`; ARREAR meters usage. */
|
|
291
|
+
type OutcomeComponentInput = {
|
|
292
|
+
type: "OUTCOME";
|
|
293
|
+
billingMode: "ADVANCE";
|
|
294
|
+
outcomeId: string;
|
|
295
|
+
quantity: number;
|
|
296
|
+
} | {
|
|
297
|
+
type: "OUTCOME";
|
|
298
|
+
billingMode: "ARREAR";
|
|
299
|
+
outcomeId: string;
|
|
300
|
+
quantity?: number | null;
|
|
301
|
+
};
|
|
302
|
+
/** A unit entitlement, referenced by `unitId`. ADVANCE needs a granted
|
|
303
|
+
* `quantity`; ARREAR meters usage. */
|
|
304
|
+
type UnitComponentInput = {
|
|
305
|
+
type: "UNIT";
|
|
306
|
+
billingMode: "ADVANCE";
|
|
307
|
+
unitId: string;
|
|
308
|
+
quantity: number;
|
|
309
|
+
} | {
|
|
310
|
+
type: "UNIT";
|
|
311
|
+
billingMode: "ARREAR";
|
|
312
|
+
unitId: string;
|
|
313
|
+
quantity?: number | null;
|
|
314
|
+
};
|
|
315
|
+
/**
|
|
316
|
+
* One entitlement line in a plan — a discriminated union on `type` (and, where
|
|
317
|
+
* it matters, `billingMode`) so the per-type fields the server requires are
|
|
318
|
+
* enforced at compile time instead of surfacing as runtime 422s:
|
|
319
|
+
* - CREDIT / OUTCOME / UNIT require their matching id;
|
|
320
|
+
* - CREDIT / OUTCOME / UNIT require `quantity` when billed ADVANCE;
|
|
321
|
+
* - WALLET / FLAT require `amount`.
|
|
322
|
+
*/
|
|
323
|
+
type PlanComponentInput = WalletComponentInput | FlatComponentInput | CreditComponentInput | OutcomeComponentInput | UnitComponentInput;
|
|
324
|
+
interface CreatePlanInput {
|
|
325
|
+
name: string;
|
|
326
|
+
description?: string | null;
|
|
327
|
+
billingCycle: BillingCycle;
|
|
328
|
+
/** Carry an unused balance into the next cycle. Default true. */
|
|
329
|
+
carryForward?: boolean;
|
|
330
|
+
/** ISO 4217 (3 letters). Default "USD". */
|
|
331
|
+
currencyCode?: string;
|
|
332
|
+
isActive?: boolean;
|
|
333
|
+
/** At least one component. FREE plans must be ADVANCE-only with no FLAT component. */
|
|
334
|
+
components: PlanComponentInput[];
|
|
335
|
+
}
|
|
336
|
+
/** A full-rewrite update carries the complete plan (same shape as create). */
|
|
337
|
+
type UpdatePlanInput = CreatePlanInput;
|
|
338
|
+
interface PlanComponent {
|
|
339
|
+
id: string;
|
|
340
|
+
type: ComponentType;
|
|
341
|
+
billingMode: BillingMode;
|
|
342
|
+
creditId: string | null;
|
|
343
|
+
outcomeId: string | null;
|
|
344
|
+
unitId: string | null;
|
|
345
|
+
targetName: string | null;
|
|
346
|
+
quantity: number | null;
|
|
347
|
+
amount: number | null;
|
|
348
|
+
}
|
|
349
|
+
interface Plan {
|
|
350
|
+
id: string;
|
|
351
|
+
name: string;
|
|
352
|
+
description: string | null;
|
|
353
|
+
type: PlanType;
|
|
354
|
+
kind: PlanKind;
|
|
355
|
+
billingCycle: BillingCycle;
|
|
356
|
+
cost: number;
|
|
357
|
+
currencyCode: string;
|
|
358
|
+
isActive: boolean;
|
|
359
|
+
components: PlanComponent[];
|
|
360
|
+
}
|
|
361
|
+
/** The richer plan returned by `plans.get()` (adds aggregate stats). */
|
|
362
|
+
interface PlanDetail extends Plan {
|
|
363
|
+
createdAt: string;
|
|
364
|
+
createdByName: string | null;
|
|
365
|
+
customerCount: number;
|
|
366
|
+
invoiceCount: number;
|
|
367
|
+
stats: {
|
|
368
|
+
totalInvoiced: number;
|
|
369
|
+
totalCollected: number;
|
|
370
|
+
openBalance: number;
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
type PurchaseStatus = "SCHEDULED" | "ACTIVE" | "CANCELLED";
|
|
374
|
+
interface CreatePurchaseInput {
|
|
375
|
+
customerId: string;
|
|
376
|
+
planId: string;
|
|
377
|
+
/** `YYYY-MM-DD`. Defaults to today; must be a valid non-past date. */
|
|
378
|
+
billingDate?: string;
|
|
379
|
+
notes?: string;
|
|
380
|
+
autoPayment?: boolean;
|
|
381
|
+
/** Auto-void the purchase if unpaid after N minutes. */
|
|
382
|
+
voidAfterMinutes?: number;
|
|
383
|
+
}
|
|
384
|
+
interface Purchase {
|
|
385
|
+
id: string;
|
|
386
|
+
purchaseNumber: string;
|
|
387
|
+
customerId: string;
|
|
388
|
+
customerName: string;
|
|
389
|
+
planId: string | null;
|
|
390
|
+
planName: string;
|
|
391
|
+
planType: PlanType;
|
|
392
|
+
hasWallet: boolean;
|
|
393
|
+
hasCredit: boolean;
|
|
394
|
+
hasOutcome: boolean;
|
|
395
|
+
hasUnit: boolean;
|
|
396
|
+
billingCycle: BillingCycle;
|
|
397
|
+
cost: number;
|
|
398
|
+
currencyCode: string;
|
|
399
|
+
billingDate: string;
|
|
400
|
+
status: PurchaseStatus;
|
|
401
|
+
autoPayment: boolean;
|
|
402
|
+
autoPaymentPauseReason: string | null;
|
|
403
|
+
createdAt: string;
|
|
404
|
+
}
|
|
405
|
+
/** The purchase returned by `purchases.get()` — a different, detail-oriented shape. */
|
|
406
|
+
interface PurchaseDetail {
|
|
407
|
+
id: string;
|
|
408
|
+
purchaseNumber: string;
|
|
409
|
+
status: PurchaseStatus;
|
|
410
|
+
cost: number;
|
|
411
|
+
billingCycle: BillingCycle;
|
|
412
|
+
planName: string;
|
|
413
|
+
billingDate: string;
|
|
414
|
+
nextBillingDate: string;
|
|
415
|
+
createdAt: string;
|
|
416
|
+
createdByName: string;
|
|
417
|
+
customer: {
|
|
418
|
+
id: string;
|
|
419
|
+
name: string;
|
|
420
|
+
};
|
|
421
|
+
autoPayment: boolean;
|
|
422
|
+
autoPaymentPauseReason: string | null;
|
|
423
|
+
autoPaymentPausedAt: string | null;
|
|
424
|
+
plan: {
|
|
425
|
+
id: string;
|
|
426
|
+
} | null;
|
|
427
|
+
stats: {
|
|
428
|
+
invoiceCount: number;
|
|
429
|
+
paidCount: number;
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
type InvoiceStatus = "OPEN" | "PAID" | "VOID";
|
|
433
|
+
/** Trimmed row from `invoices.list()`. */
|
|
434
|
+
interface InvoiceListRow {
|
|
435
|
+
id: string;
|
|
436
|
+
invoiceNumber: string;
|
|
437
|
+
purchaseNumber: string | null;
|
|
438
|
+
purchaseId: string | null;
|
|
439
|
+
customerId: string;
|
|
440
|
+
customerName: string;
|
|
441
|
+
planName: string;
|
|
442
|
+
billingCycle: BillingCycle;
|
|
443
|
+
cycleIndex: number;
|
|
444
|
+
amount: number;
|
|
445
|
+
currencyCode: string;
|
|
446
|
+
status: InvoiceStatus;
|
|
447
|
+
issuedAt: string;
|
|
448
|
+
paidAt: string | null;
|
|
449
|
+
}
|
|
450
|
+
/** A billing party (your org, or the customer) on an invoice. */
|
|
451
|
+
interface InvoiceParty {
|
|
452
|
+
name: string;
|
|
453
|
+
logoUrl: string | null;
|
|
454
|
+
legalName: string | null;
|
|
455
|
+
email: string | null;
|
|
456
|
+
phone: string | null;
|
|
457
|
+
website: string | null;
|
|
458
|
+
taxId: string | null;
|
|
459
|
+
addressLine1: string | null;
|
|
460
|
+
city: string | null;
|
|
461
|
+
state: string | null;
|
|
462
|
+
country: string | null;
|
|
463
|
+
pincode: string | null;
|
|
464
|
+
}
|
|
465
|
+
interface InvoiceLineItem {
|
|
466
|
+
kind: "credit" | "outcome";
|
|
467
|
+
label: string;
|
|
468
|
+
quantity: number;
|
|
469
|
+
}
|
|
470
|
+
/** The plan snapshot embedded in a full invoice. */
|
|
471
|
+
interface InvoicePlanSnapshot {
|
|
472
|
+
name: string;
|
|
473
|
+
type: PlanType;
|
|
474
|
+
hasWallet: boolean;
|
|
475
|
+
hasCredit: boolean;
|
|
476
|
+
hasOutcome: boolean;
|
|
477
|
+
hasUnit: boolean;
|
|
478
|
+
kind: PlanKind;
|
|
479
|
+
billingCycle: BillingCycle;
|
|
480
|
+
cost: number;
|
|
481
|
+
currencyCode: string;
|
|
482
|
+
spendingLimit: number | null;
|
|
483
|
+
seatScope: "CUSTOMER" | "MEMBER" | null;
|
|
484
|
+
carryForward: boolean;
|
|
485
|
+
billingDate: string;
|
|
486
|
+
status: "SCHEDULED" | "ACTIVE";
|
|
487
|
+
}
|
|
488
|
+
/** The full invoice from `invoices.get()` (also the `payment` in `payments.get()`). */
|
|
489
|
+
interface Invoice {
|
|
490
|
+
id: string;
|
|
491
|
+
invoiceNumber: string;
|
|
492
|
+
paymentNumber: string | null;
|
|
493
|
+
purchaseNumber: string | null;
|
|
494
|
+
customerId: string;
|
|
495
|
+
customerName: string;
|
|
496
|
+
customerSlug: string;
|
|
497
|
+
customerParty: InvoiceParty;
|
|
498
|
+
orgParty: InvoiceParty;
|
|
499
|
+
purchaseId: string | null;
|
|
500
|
+
stream: BillingMode;
|
|
501
|
+
planId: string | null;
|
|
502
|
+
planName: string;
|
|
503
|
+
planType: PlanType;
|
|
504
|
+
plan: InvoicePlanSnapshot | null;
|
|
505
|
+
billingCycle: BillingCycle;
|
|
506
|
+
cycleIndex: number;
|
|
507
|
+
lineItems: InvoiceLineItem[];
|
|
508
|
+
hasWallet: boolean;
|
|
509
|
+
hasCredit: boolean;
|
|
510
|
+
hasOutcome: boolean;
|
|
511
|
+
hasUnit: boolean;
|
|
512
|
+
amount: number;
|
|
513
|
+
currencyCode: string;
|
|
514
|
+
currencyRateUsd: number;
|
|
515
|
+
status: InvoiceStatus;
|
|
516
|
+
issuedAt: string;
|
|
517
|
+
paidAt: string | null;
|
|
518
|
+
voidedAt: string | null;
|
|
519
|
+
/** One of the server `PaymentMode` values (e.g. "CASH", "STRIPE", …). */
|
|
520
|
+
paymentMode: string | null;
|
|
521
|
+
paymentReference: string | null;
|
|
522
|
+
stripePaymentIntentId: string | null;
|
|
523
|
+
autoPaid: boolean;
|
|
524
|
+
notes: string | null;
|
|
525
|
+
createdAt: string;
|
|
526
|
+
createdByName: string;
|
|
527
|
+
paidByName: string | null;
|
|
528
|
+
voidedByName: string | null;
|
|
529
|
+
}
|
|
530
|
+
/** A short `{ id, name }` plan reference alongside a full invoice/payment. */
|
|
531
|
+
interface PlanRef {
|
|
532
|
+
id: string;
|
|
533
|
+
name: string;
|
|
534
|
+
}
|
|
535
|
+
interface InvoiceDetail {
|
|
536
|
+
invoice: Invoice;
|
|
537
|
+
plan: PlanRef | null;
|
|
538
|
+
}
|
|
539
|
+
/** A hosted pay link for an OPEN invoice. */
|
|
540
|
+
interface PayLink {
|
|
541
|
+
url: string;
|
|
542
|
+
invoiceId: string;
|
|
543
|
+
invoiceNumber: string;
|
|
544
|
+
}
|
|
545
|
+
/** Trimmed row from `payments.list()` (a payment is a PAID invoice). */
|
|
546
|
+
interface PaymentRow {
|
|
547
|
+
id: string;
|
|
548
|
+
paymentNumber: string;
|
|
549
|
+
invoiceNumber: string;
|
|
550
|
+
purchaseNumber: string | null;
|
|
551
|
+
customerId: string;
|
|
552
|
+
customerName: string;
|
|
553
|
+
planName: string;
|
|
554
|
+
amount: number;
|
|
555
|
+
currencyCode: string;
|
|
556
|
+
paymentMode: string | null;
|
|
557
|
+
paymentReference: string | null;
|
|
558
|
+
paidAt: string;
|
|
559
|
+
paidByName: string | null;
|
|
560
|
+
}
|
|
561
|
+
interface PaymentDetail {
|
|
562
|
+
payment: Invoice;
|
|
563
|
+
plan: PlanRef | null;
|
|
564
|
+
}
|
|
565
|
+
type MemberRole = "OWNER" | "ADMIN" | "MEMBER";
|
|
566
|
+
interface Member {
|
|
567
|
+
id: string;
|
|
568
|
+
role: MemberRole;
|
|
569
|
+
name: string;
|
|
570
|
+
email: string | null;
|
|
571
|
+
employeeId: string | null;
|
|
572
|
+
phone: string | null;
|
|
573
|
+
/** True once the member has linked a login. */
|
|
574
|
+
isLinked: boolean;
|
|
575
|
+
joinedAt: string;
|
|
576
|
+
}
|
|
577
|
+
interface AddMemberInput {
|
|
578
|
+
name: string;
|
|
579
|
+
role: "ADMIN" | "MEMBER";
|
|
580
|
+
email?: string;
|
|
581
|
+
employeeId?: string;
|
|
582
|
+
phone?: string;
|
|
583
|
+
}
|
|
584
|
+
/** Role is fixed after creation; omit it here. */
|
|
585
|
+
interface UpdateMemberInput {
|
|
586
|
+
name: string;
|
|
587
|
+
email?: string;
|
|
588
|
+
employeeId?: string;
|
|
589
|
+
phone?: string;
|
|
590
|
+
}
|
|
591
|
+
type WalletTransactionType = "CREDIT" | "DEBIT";
|
|
592
|
+
interface WalletTransaction {
|
|
593
|
+
id: string;
|
|
594
|
+
type: WalletTransactionType;
|
|
595
|
+
amount: number;
|
|
596
|
+
amountUsd: number;
|
|
597
|
+
currencyCode: string;
|
|
598
|
+
currencyRateUsd: number;
|
|
599
|
+
notes: string | null;
|
|
600
|
+
batchId: string | null;
|
|
601
|
+
createdAt: string;
|
|
602
|
+
createdByName: string | null;
|
|
603
|
+
}
|
|
604
|
+
interface WalletList {
|
|
605
|
+
balanceUsd: number;
|
|
606
|
+
transactions: WalletTransaction[];
|
|
607
|
+
}
|
|
608
|
+
interface WalletEntryInput {
|
|
609
|
+
/** DEBIT spends, CREDIT tops up. */
|
|
610
|
+
type: WalletTransactionType;
|
|
611
|
+
/** Always positive; the sign comes from `type`. */
|
|
612
|
+
amount: number;
|
|
613
|
+
notes?: string;
|
|
614
|
+
}
|
|
615
|
+
/** Grant (positive) or claw back (negative) a credit balance. `delta` must be a non-zero whole number. */
|
|
616
|
+
interface CreditAdjustInput {
|
|
617
|
+
creditId: string;
|
|
618
|
+
delta: number;
|
|
619
|
+
notes?: string;
|
|
620
|
+
}
|
|
621
|
+
/** Grant/claw an outcome balance. `delta` is a non-zero whole number; floored at 0. */
|
|
622
|
+
interface OutcomeAdjustInput {
|
|
623
|
+
outcomeId: string;
|
|
624
|
+
delta: number;
|
|
625
|
+
notes?: string;
|
|
626
|
+
}
|
|
627
|
+
/** Grant/claw a unit balance. `delta` is non-zero (fractional allowed); floored at 0. */
|
|
628
|
+
interface UnitAdjustInput {
|
|
629
|
+
unitId: string;
|
|
630
|
+
delta: number;
|
|
631
|
+
notes?: string;
|
|
632
|
+
}
|
|
633
|
+
/** A per-model token bundle attached to a credit or outcome step. */
|
|
634
|
+
interface ModelBundleEntry {
|
|
635
|
+
orgModelId: string;
|
|
636
|
+
/** Display label; the server defaults it from the model when omitted. */
|
|
637
|
+
modelName?: string;
|
|
638
|
+
tokens: number;
|
|
639
|
+
input: number;
|
|
640
|
+
output: number;
|
|
641
|
+
cache: number;
|
|
642
|
+
}
|
|
643
|
+
interface Credit {
|
|
644
|
+
id: string;
|
|
645
|
+
name: string;
|
|
646
|
+
agentKey: string;
|
|
647
|
+
description: string | null;
|
|
648
|
+
tokensPerCredit: number;
|
|
649
|
+
basePrice: number;
|
|
650
|
+
marginPercent: number;
|
|
651
|
+
pricePerCredit: number;
|
|
652
|
+
isActive: boolean;
|
|
653
|
+
createdAt: string;
|
|
654
|
+
}
|
|
655
|
+
/** `credits.get()` — adds the model bundle plus usage stats/analytics. */
|
|
656
|
+
interface CreditDetail extends Credit {
|
|
657
|
+
modelBundle: ModelBundleEntry[];
|
|
658
|
+
createdByName: string;
|
|
659
|
+
stats: {
|
|
660
|
+
customerCount: number;
|
|
661
|
+
planCount: number;
|
|
662
|
+
totalGranted: number;
|
|
663
|
+
totalUsed: number;
|
|
664
|
+
revenue: number;
|
|
665
|
+
usageLogCount: number;
|
|
666
|
+
};
|
|
667
|
+
/** Cost-over-time analytics; shape is server-defined. */
|
|
668
|
+
analytics: Record<string, unknown>;
|
|
669
|
+
}
|
|
670
|
+
interface CreateCreditInput {
|
|
671
|
+
name: string;
|
|
672
|
+
/** Lowercased stable key used by `signals.credit({ agentKey })`. */
|
|
673
|
+
agentKey: string;
|
|
674
|
+
basePrice: number;
|
|
675
|
+
marginPercent: number;
|
|
676
|
+
pricePerCredit: number;
|
|
677
|
+
description?: string;
|
|
678
|
+
tokensPerCredit?: number;
|
|
679
|
+
modelBundle?: ModelBundleEntry[];
|
|
680
|
+
}
|
|
681
|
+
type UpdateCreditInput = CreateCreditInput;
|
|
682
|
+
interface OutcomeStepInput {
|
|
683
|
+
id?: string;
|
|
684
|
+
name: string;
|
|
685
|
+
/** Lowercased stable key used by `signals.outcome({ agentKey })`. */
|
|
686
|
+
agentKey: string;
|
|
687
|
+
basePrice: number;
|
|
688
|
+
modelBundle?: ModelBundleEntry[];
|
|
689
|
+
}
|
|
690
|
+
interface Outcome {
|
|
691
|
+
id: string;
|
|
692
|
+
name: string;
|
|
693
|
+
description: string | null;
|
|
694
|
+
isActive: boolean;
|
|
695
|
+
marginPercent: number;
|
|
696
|
+
pricePerOutcome: number;
|
|
697
|
+
createdAt: string;
|
|
698
|
+
steps?: {
|
|
699
|
+
name: string;
|
|
700
|
+
agentKey: string;
|
|
701
|
+
}[];
|
|
702
|
+
}
|
|
703
|
+
/** `outcomes.get()` — adds steps plus in-flight/completed stats. */
|
|
704
|
+
interface OutcomeDetail extends Outcome {
|
|
705
|
+
createdByName: string | null;
|
|
706
|
+
steps: {
|
|
707
|
+
name: string;
|
|
708
|
+
agentKey: string;
|
|
709
|
+
}[];
|
|
710
|
+
stats: {
|
|
711
|
+
stepCount: number;
|
|
712
|
+
customerCount: number;
|
|
713
|
+
inFlight: number;
|
|
714
|
+
completed: number;
|
|
715
|
+
totalRemaining: number;
|
|
716
|
+
};
|
|
717
|
+
analytics: Record<string, unknown>;
|
|
718
|
+
}
|
|
719
|
+
interface CreateOutcomeInput {
|
|
720
|
+
name: string;
|
|
721
|
+
basePrice: number;
|
|
722
|
+
marginPercent: number;
|
|
723
|
+
pricePerOutcome: number;
|
|
724
|
+
description?: string | null;
|
|
725
|
+
isActive?: boolean;
|
|
726
|
+
/** 1–50 steps; step names and agent keys must be unique. */
|
|
727
|
+
steps: OutcomeStepInput[];
|
|
728
|
+
}
|
|
729
|
+
type UpdateOutcomeInput = CreateOutcomeInput;
|
|
730
|
+
type UnitPricingType = "FLAT" | "SLAB" | "VOLUME";
|
|
731
|
+
/** A tier for SLAB/VOLUME pricing; only the last tier may have `upTo: null`. */
|
|
732
|
+
interface UnitTierInput {
|
|
733
|
+
id?: string;
|
|
734
|
+
upTo: number | null;
|
|
735
|
+
price: number;
|
|
736
|
+
}
|
|
737
|
+
interface UnitTier {
|
|
738
|
+
id: string;
|
|
739
|
+
upTo: number | null;
|
|
740
|
+
price: number;
|
|
741
|
+
order: number;
|
|
742
|
+
}
|
|
743
|
+
interface Unit {
|
|
744
|
+
id: string;
|
|
745
|
+
name: string;
|
|
746
|
+
description: string | null;
|
|
747
|
+
pricingType: UnitPricingType;
|
|
748
|
+
flatPrice: number;
|
|
749
|
+
tiers: UnitTier[];
|
|
750
|
+
isActive: boolean;
|
|
751
|
+
createdAt: string;
|
|
752
|
+
}
|
|
753
|
+
/** `units.get()` — adds linked-customer sample plus stats. */
|
|
754
|
+
interface UnitDetail extends Unit {
|
|
755
|
+
createdByName: string | null;
|
|
756
|
+
stats: {
|
|
757
|
+
planCount: number;
|
|
758
|
+
customerCount: number;
|
|
759
|
+
totalRemaining: number;
|
|
760
|
+
totalUsed: number;
|
|
761
|
+
};
|
|
762
|
+
/** First page (≤10) of customers currently metered on this unit. */
|
|
763
|
+
customers: Record<string, unknown>[];
|
|
764
|
+
}
|
|
765
|
+
/** Flat per-event pricing — a single `flatPrice` (defaults to 0), no tiers. */
|
|
766
|
+
interface FlatUnitInput {
|
|
767
|
+
name: string;
|
|
768
|
+
/** Stable per-org report key (`signals.unit({ agentKey })`); `[a-z0-9._-]`. */
|
|
769
|
+
agentKey: string;
|
|
770
|
+
pricingType: "FLAT";
|
|
771
|
+
/** Price per event; defaults to 0. */
|
|
772
|
+
flatPrice?: number;
|
|
773
|
+
description?: string | null;
|
|
774
|
+
isActive?: boolean;
|
|
775
|
+
}
|
|
776
|
+
/** Tiered pricing (SLAB or VOLUME) — at least one tier is required. */
|
|
777
|
+
interface TieredUnitInput {
|
|
778
|
+
name: string;
|
|
779
|
+
/** Stable per-org report key (`signals.unit({ agentKey })`); `[a-z0-9._-]`. */
|
|
780
|
+
agentKey: string;
|
|
781
|
+
pricingType: "SLAB" | "VOLUME";
|
|
782
|
+
/** At least one tier (≤50). Only the last tier may have `upTo: null`. */
|
|
783
|
+
tiers: UnitTierInput[];
|
|
784
|
+
description?: string | null;
|
|
785
|
+
isActive?: boolean;
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* Create a unit type — a discriminated union on `pricingType`: `FLAT` takes an
|
|
789
|
+
* optional `flatPrice` (defaults to 0); `SLAB`/`VOLUME` require `tiers`, so the
|
|
790
|
+
* server's "add at least one tier" rule is enforced at compile time.
|
|
791
|
+
*/
|
|
792
|
+
type CreateUnitInput = FlatUnitInput | TieredUnitInput;
|
|
793
|
+
type UpdateUnitInput = CreateUnitInput;
|
|
794
|
+
/**
|
|
795
|
+
* Record one unit-metered event. The unit is addressed by its `agentKey` (the
|
|
796
|
+
* stable key set when the unit was created) — the ONLY accepted identifier;
|
|
797
|
+
* the display name is deliberately not accepted, so a rename can never redirect
|
|
798
|
+
* a live integration. Keys are matched exactly (they are stored lowercased).
|
|
799
|
+
*/
|
|
800
|
+
interface UnitInput {
|
|
801
|
+
customerId: string;
|
|
802
|
+
agentKey: string;
|
|
803
|
+
member?: string;
|
|
804
|
+
}
|
|
805
|
+
interface UnitUsageRow {
|
|
806
|
+
unit: string;
|
|
807
|
+
/** The unit's stable agent key — what to send to report against it. */
|
|
808
|
+
agentKey: string | null;
|
|
809
|
+
billingMode: BillingMode;
|
|
810
|
+
granted?: number;
|
|
811
|
+
used: number;
|
|
812
|
+
remaining?: number;
|
|
813
|
+
}
|
|
814
|
+
interface UnitUsageList {
|
|
815
|
+
customer: {
|
|
816
|
+
id: string;
|
|
817
|
+
name: string;
|
|
818
|
+
};
|
|
819
|
+
units: UnitUsageRow[];
|
|
820
|
+
events: unknown[];
|
|
821
|
+
}
|
|
822
|
+
type ThresholdKind = "CREDIT" | "OUTCOME" | "WALLET";
|
|
823
|
+
interface Webhook {
|
|
824
|
+
kind: ThresholdKind;
|
|
825
|
+
threshold: number;
|
|
826
|
+
url: string;
|
|
827
|
+
isActive: boolean;
|
|
828
|
+
}
|
|
829
|
+
interface UpsertWebhookInput {
|
|
830
|
+
kind: ThresholdKind;
|
|
831
|
+
/** Fire when the balance crosses this value. */
|
|
832
|
+
threshold: number;
|
|
833
|
+
/** http(s) URL to POST the alert to. */
|
|
834
|
+
url: string;
|
|
835
|
+
isActive?: boolean;
|
|
836
|
+
}
|
|
221
837
|
type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
|
|
222
838
|
interface Logger {
|
|
223
839
|
debug?: (...args: unknown[]) => void;
|
|
@@ -228,7 +844,7 @@ type DropReason = "queue_full" | "send_failed";
|
|
|
228
844
|
interface ClockNextConfig {
|
|
229
845
|
/** The org API key (`cnk_…`). Required. */
|
|
230
846
|
apiKey: string;
|
|
231
|
-
/** API base URL. Defaults to https://
|
|
847
|
+
/** API base URL. Defaults to https://payments.clocknext.com */
|
|
232
848
|
baseUrl?: string;
|
|
233
849
|
/**
|
|
234
850
|
* "async" (default) buffers signals and flushes in the background.
|
|
@@ -303,20 +919,23 @@ interface ResolvedConfig {
|
|
|
303
919
|
}
|
|
304
920
|
|
|
305
921
|
interface RequestOptions {
|
|
306
|
-
method: "GET" | "POST" | "PATCH" | "DELETE";
|
|
922
|
+
method: "GET" | "POST" | "PATCH" | "PUT" | "DELETE";
|
|
307
923
|
path: string;
|
|
308
924
|
query?: Record<string, string | number | undefined>;
|
|
309
925
|
body?: unknown;
|
|
310
|
-
/**
|
|
311
|
-
|
|
312
|
-
|
|
926
|
+
/**
|
|
927
|
+
* Whether this call may be auto-retried on a transient failure. Defaults to
|
|
928
|
+
* method-based safety: reads (GET), idempotent replaces (PUT), and DELETE are
|
|
929
|
+
* replay-safe. Non-idempotent writes (POST/PATCH) default to NOT retried so a
|
|
930
|
+
* transient failure can't duplicate the write; callers that tolerate a retry
|
|
931
|
+
* opt in explicitly.
|
|
932
|
+
*/
|
|
313
933
|
retry?: boolean;
|
|
314
934
|
}
|
|
315
935
|
/**
|
|
316
936
|
* The single HTTP seam. Builds the authenticated request, enforces a timeout,
|
|
317
937
|
* unwraps the `{ ok, ... }` envelope, maps failures to typed errors, and retries
|
|
318
|
-
* transient ones with backoff + jitter
|
|
319
|
-
* every attempt.
|
|
938
|
+
* transient ones with backoff + jitter.
|
|
320
939
|
*/
|
|
321
940
|
declare class Transport {
|
|
322
941
|
private readonly cfg;
|
|
@@ -328,6 +947,30 @@ declare class Transport {
|
|
|
328
947
|
private buildUrl;
|
|
329
948
|
}
|
|
330
949
|
|
|
950
|
+
/**
|
|
951
|
+
* The `credits` resource — manage the org's credit *types* (the catalog). A
|
|
952
|
+
* credit's stable `agentKey` is what you pass to `signals.credit()` when
|
|
953
|
+
* recording usage against it. `/api/v1/credits`.
|
|
954
|
+
*/
|
|
955
|
+
declare class Credits {
|
|
956
|
+
private readonly transport;
|
|
957
|
+
constructor(transport: Transport);
|
|
958
|
+
/** List credit types. Pass `{ active: true }` to only return active ones. */
|
|
959
|
+
list(params?: {
|
|
960
|
+
active?: boolean;
|
|
961
|
+
}): Promise<Credit[]>;
|
|
962
|
+
/** Create a credit type. `POST /api/v1/credits`. */
|
|
963
|
+
create(input: CreateCreditInput): Promise<void>;
|
|
964
|
+
/** Fetch one credit type with usage stats. `GET /api/v1/credits/:id`. */
|
|
965
|
+
get(id: string): Promise<CreditDetail>;
|
|
966
|
+
/** Update a credit type's definition. `PATCH /api/v1/credits/:id`. */
|
|
967
|
+
update(id: string, input: UpdateCreditInput): Promise<void>;
|
|
968
|
+
/** Activate or deactivate a credit type. */
|
|
969
|
+
setActive(id: string, isActive: boolean): Promise<Credit>;
|
|
970
|
+
/** Delete a credit type. Refused while a customer is linked to it. */
|
|
971
|
+
delete(id: string): Promise<void>;
|
|
972
|
+
}
|
|
973
|
+
|
|
331
974
|
/**
|
|
332
975
|
* The `customers` resource — full CRUD over the org's customers plus the
|
|
333
976
|
* read-only insight endpoints (usage / balances / plan / revenue). Every call
|
|
@@ -371,6 +1014,123 @@ declare class Customers {
|
|
|
371
1014
|
revenue(id: string, params?: {
|
|
372
1015
|
range?: RevenueRange;
|
|
373
1016
|
}): Promise<CustomerRevenue>;
|
|
1017
|
+
/** List a customer's members. `GET …/:id/members`. */
|
|
1018
|
+
listMembers(id: string): Promise<Member[]>;
|
|
1019
|
+
/** Add a member to a customer. Duplicate `employeeId` → ConflictError. */
|
|
1020
|
+
addMember(id: string, input: AddMemberInput): Promise<Member>;
|
|
1021
|
+
/** Update a member's profile (role is fixed). `PATCH …/:id/members/:memberId`. */
|
|
1022
|
+
updateMember(id: string, memberId: string, input: UpdateMemberInput): Promise<Member>;
|
|
1023
|
+
/** Remove a member from a customer. `DELETE …/:id/members/:memberId`. */
|
|
1024
|
+
removeMember(id: string, memberId: string): Promise<void>;
|
|
1025
|
+
/** The customer's wallet balance + transactions. `GET …/:id/wallet`. */
|
|
1026
|
+
wallet(id: string, params?: {
|
|
1027
|
+
limit?: number;
|
|
1028
|
+
}): Promise<WalletList>;
|
|
1029
|
+
/**
|
|
1030
|
+
* Record a manual wallet entry (top-up or deduction). `amount` is always
|
|
1031
|
+
* positive; the sign comes from `type`. `POST …/:id/wallet`.
|
|
1032
|
+
*/
|
|
1033
|
+
addWalletEntry(id: string, input: WalletEntryInput): Promise<{
|
|
1034
|
+
transaction: WalletTransaction;
|
|
1035
|
+
balanceUsd: number;
|
|
1036
|
+
}>;
|
|
1037
|
+
/** Grant or claw back a credit balance. `POST …/:id/credits/adjust`. */
|
|
1038
|
+
adjustCredit(id: string, input: CreditAdjustInput): Promise<void>;
|
|
1039
|
+
/** Grant or claw back an outcome balance. Returns the remaining balance. */
|
|
1040
|
+
adjustOutcome(id: string, input: OutcomeAdjustInput): Promise<{
|
|
1041
|
+
remaining: number;
|
|
1042
|
+
}>;
|
|
1043
|
+
/** Grant or claw back a unit balance. Returns the remaining balance. */
|
|
1044
|
+
adjustUnit(id: string, input: UnitAdjustInput): Promise<{
|
|
1045
|
+
remaining: number;
|
|
1046
|
+
}>;
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
/**
|
|
1050
|
+
* The `invoices` resource — read-only. Invoices are generated by the billing
|
|
1051
|
+
* engine from purchases; you list them, read one, or mint a hosted pay link for
|
|
1052
|
+
* an OPEN invoice. `/api/v1/invoices`.
|
|
1053
|
+
*/
|
|
1054
|
+
declare class Invoices {
|
|
1055
|
+
private readonly transport;
|
|
1056
|
+
constructor(transport: Transport);
|
|
1057
|
+
/** List invoices, most-recent first. Filter by customer and/or status. */
|
|
1058
|
+
list(params?: {
|
|
1059
|
+
customerId?: string;
|
|
1060
|
+
status?: InvoiceStatus;
|
|
1061
|
+
limit?: number;
|
|
1062
|
+
}): Promise<InvoiceListRow[]>;
|
|
1063
|
+
/** Fetch one full invoice + its plan snapshot. `GET /api/v1/invoices/:id`. */
|
|
1064
|
+
get(id: string): Promise<InvoiceDetail>;
|
|
1065
|
+
/**
|
|
1066
|
+
* Mint a hosted pay link for an OPEN invoice. PAID/VOID invoices are rejected
|
|
1067
|
+
* with a `ConflictError`. `GET /api/v1/invoices/:id/pay-link`.
|
|
1068
|
+
*/
|
|
1069
|
+
payLink(id: string): Promise<PayLink>;
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
/**
|
|
1073
|
+
* The `outcomes` resource — manage the org's outcome *types* (the catalog). An
|
|
1074
|
+
* outcome is a multi-step workflow; each step has a stable `agentKey` you pass
|
|
1075
|
+
* to `signals.outcome()`. `/api/v1/outcomes`.
|
|
1076
|
+
*/
|
|
1077
|
+
declare class Outcomes {
|
|
1078
|
+
private readonly transport;
|
|
1079
|
+
constructor(transport: Transport);
|
|
1080
|
+
/** List outcome types. Pass `{ active: true }` to only return active ones. */
|
|
1081
|
+
list(params?: {
|
|
1082
|
+
active?: boolean;
|
|
1083
|
+
}): Promise<Outcome[]>;
|
|
1084
|
+
/** Create an outcome type with its steps. `POST /api/v1/outcomes`. */
|
|
1085
|
+
create(input: CreateOutcomeInput): Promise<Outcome>;
|
|
1086
|
+
/** Fetch one outcome type with steps + stats. `GET /api/v1/outcomes/:id`. */
|
|
1087
|
+
get(id: string): Promise<OutcomeDetail>;
|
|
1088
|
+
/** Update an outcome type's definition. `PATCH /api/v1/outcomes/:id`. */
|
|
1089
|
+
update(id: string, input: UpdateOutcomeInput): Promise<Outcome>;
|
|
1090
|
+
/** Activate or deactivate an outcome type. */
|
|
1091
|
+
setActive(id: string, isActive: boolean): Promise<void>;
|
|
1092
|
+
/** Delete an outcome type. */
|
|
1093
|
+
delete(id: string): Promise<void>;
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
/**
|
|
1097
|
+
* The `payments` resource — read-only. A payment is a PAID invoice; the `id`
|
|
1098
|
+
* used here is that underlying invoice id. `/api/v1/payments`.
|
|
1099
|
+
*/
|
|
1100
|
+
declare class Payments {
|
|
1101
|
+
private readonly transport;
|
|
1102
|
+
constructor(transport: Transport);
|
|
1103
|
+
/** List payments (paid invoices), most-recent first. Filter by customer. */
|
|
1104
|
+
list(params?: {
|
|
1105
|
+
customerId?: string;
|
|
1106
|
+
limit?: number;
|
|
1107
|
+
}): Promise<PaymentRow[]>;
|
|
1108
|
+
/** Fetch one payment + its plan snapshot. `GET /api/v1/payments/:id`. */
|
|
1109
|
+
get(id: string): Promise<PaymentDetail>;
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
/**
|
|
1113
|
+
* The `plans` resource — define and manage the billing plans customers
|
|
1114
|
+
* subscribe to. Each plan is a set of entitlement components (wallet / credit /
|
|
1115
|
+
* outcome / unit / flat) billed on a cycle. `/api/v1/plans`.
|
|
1116
|
+
*/
|
|
1117
|
+
declare class Plans {
|
|
1118
|
+
private readonly transport;
|
|
1119
|
+
constructor(transport: Transport);
|
|
1120
|
+
/** List plans, oldest-first. Pass `{ active: true }` to only return active plans. */
|
|
1121
|
+
list(params?: {
|
|
1122
|
+
active?: boolean;
|
|
1123
|
+
}): Promise<Plan[]>;
|
|
1124
|
+
/** Create a plan. `POST /api/v1/plans`. */
|
|
1125
|
+
create(input: CreatePlanInput): Promise<Plan>;
|
|
1126
|
+
/** Fetch one plan with aggregate subscriber/invoice stats. `GET /api/v1/plans/:id`. */
|
|
1127
|
+
get(id: string): Promise<PlanDetail>;
|
|
1128
|
+
/** Replace a plan's definition (components + settings). `PATCH /api/v1/plans/:id`. */
|
|
1129
|
+
update(id: string, input: UpdatePlanInput): Promise<Plan>;
|
|
1130
|
+
/** Activate or deactivate a plan without rewriting it. */
|
|
1131
|
+
setActive(id: string, isActive: boolean): Promise<Plan>;
|
|
1132
|
+
/** Delete a plan. Refused once any customer has purchased it. */
|
|
1133
|
+
delete(id: string): Promise<void>;
|
|
374
1134
|
}
|
|
375
1135
|
|
|
376
1136
|
/**
|
|
@@ -385,25 +1145,44 @@ declare class Portal {
|
|
|
385
1145
|
createToken(input: PortalTokenInput): Promise<PortalToken>;
|
|
386
1146
|
}
|
|
387
1147
|
|
|
1148
|
+
/**
|
|
1149
|
+
* The `purchases` resource — a purchase is a customer's subscription to a plan.
|
|
1150
|
+
* Creating one auto-cancels the customer's existing scheduled/active purchase
|
|
1151
|
+
* (this is also how you "change plan"). `/api/v1/purchases`.
|
|
1152
|
+
*/
|
|
1153
|
+
declare class Purchases {
|
|
1154
|
+
private readonly transport;
|
|
1155
|
+
constructor(transport: Transport);
|
|
1156
|
+
/** List purchases, most-recent first. Filter by customer and/or status. */
|
|
1157
|
+
list(params?: {
|
|
1158
|
+
customerId?: string;
|
|
1159
|
+
status?: PurchaseStatus;
|
|
1160
|
+
limit?: number;
|
|
1161
|
+
}): Promise<Purchase[]>;
|
|
1162
|
+
/** Subscribe a customer to a plan. `POST /api/v1/purchases`. */
|
|
1163
|
+
create(input: CreatePurchaseInput): Promise<Purchase>;
|
|
1164
|
+
/** Fetch one purchase. `GET /api/v1/purchases/:id`. */
|
|
1165
|
+
get(id: string): Promise<PurchaseDetail>;
|
|
1166
|
+
/**
|
|
1167
|
+
* Turn automatic billing on or off for a purchase. When pausing, an optional
|
|
1168
|
+
* `pauseReason` is recorded. `PATCH /api/v1/purchases/:id`.
|
|
1169
|
+
*/
|
|
1170
|
+
setAutoPayment(id: string, autoPayment: boolean, opts?: {
|
|
1171
|
+
pauseReason?: string;
|
|
1172
|
+
}): Promise<{
|
|
1173
|
+
autoPayment: boolean;
|
|
1174
|
+
}>;
|
|
1175
|
+
/** Cancel a purchase (voids any open invoice). `POST /api/v1/purchases/:id/cancel`. */
|
|
1176
|
+
cancel(id: string): Promise<void>;
|
|
1177
|
+
}
|
|
1178
|
+
|
|
388
1179
|
/** One buffered, wire-ready signal awaiting a background flush. */
|
|
389
1180
|
interface QueuedSignal {
|
|
390
1181
|
/** The exact JSON body to POST to /api/v1/usage. */
|
|
391
1182
|
body: Record<string, unknown>;
|
|
392
|
-
/** Reused across retries. */
|
|
393
|
-
idempotencyKey: string;
|
|
394
1183
|
/** The original signal — passed to onError/onDrop hooks. */
|
|
395
1184
|
signal: Signal;
|
|
396
1185
|
}
|
|
397
|
-
/**
|
|
398
|
-
* Pluggable buffer. The default is in-memory; a host that needs to survive a
|
|
399
|
-
* crash *before* send can implement this against disk/Redis/SQLite.
|
|
400
|
-
*/
|
|
401
|
-
interface QueueStore {
|
|
402
|
-
push(item: QueuedSignal): void;
|
|
403
|
-
/** Remove and return up to `n` items (FIFO). */
|
|
404
|
-
take(n: number): QueuedSignal[];
|
|
405
|
-
size(): number;
|
|
406
|
-
}
|
|
407
1186
|
|
|
408
1187
|
/**
|
|
409
1188
|
* Background send engine for async-mode signals. Buffers wire-ready signals and
|
|
@@ -455,6 +1234,12 @@ declare class Signals {
|
|
|
455
1234
|
track(signal: Signal, opts?: {
|
|
456
1235
|
wait?: boolean;
|
|
457
1236
|
}): Promise<TrackResult>;
|
|
1237
|
+
/**
|
|
1238
|
+
* Validate + price a signal WITHOUT recording it (server `dryRun`). Returns
|
|
1239
|
+
* the projected usage log so an integration can preflight before sending real
|
|
1240
|
+
* traffic — no inbox row, wallet / outcome writes, or webhooks fire.
|
|
1241
|
+
*/
|
|
1242
|
+
verify(signal: Signal): Promise<SerializedUsageLog | null>;
|
|
458
1243
|
/** Meter a named credit. */
|
|
459
1244
|
credit(input: CreditInput, opts?: {
|
|
460
1245
|
wait?: boolean;
|
|
@@ -472,6 +1257,75 @@ declare class Signals {
|
|
|
472
1257
|
customerId: string;
|
|
473
1258
|
limit?: number;
|
|
474
1259
|
}): Promise<UsageList>;
|
|
1260
|
+
/**
|
|
1261
|
+
* Record one unit-metered event (a fixed-price meter with no tokens),
|
|
1262
|
+
* addressed to a catalog unit by its `agentKey`. Sent immediately — unit
|
|
1263
|
+
* events are not buffered. `POST /api/v1/units`.
|
|
1264
|
+
*/
|
|
1265
|
+
unit(input: UnitInput): Promise<{
|
|
1266
|
+
unitUsage: Record<string, unknown>;
|
|
1267
|
+
}>;
|
|
1268
|
+
/** A customer's unit balances + recent unit events. `GET /api/v1/units`. */
|
|
1269
|
+
unitUsage(params: {
|
|
1270
|
+
customerId: string;
|
|
1271
|
+
limit?: number;
|
|
1272
|
+
}): Promise<UnitUsageList>;
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
/**
|
|
1276
|
+
* The `units` resource — manage the org's unit *types* (the catalog): named,
|
|
1277
|
+
* quantity-priced meters (FLAT / SLAB / VOLUME). Record consumption against a
|
|
1278
|
+
* unit with `signals.unit()`. `/api/v1/units-catalog`.
|
|
1279
|
+
*/
|
|
1280
|
+
declare class Units {
|
|
1281
|
+
private readonly transport;
|
|
1282
|
+
constructor(transport: Transport);
|
|
1283
|
+
/** List unit types. Pass `{ active: true }` to only return active ones. */
|
|
1284
|
+
list(params?: {
|
|
1285
|
+
active?: boolean;
|
|
1286
|
+
}): Promise<Unit[]>;
|
|
1287
|
+
/** Create a unit type. `POST /api/v1/units-catalog`. */
|
|
1288
|
+
create(input: CreateUnitInput): Promise<Unit>;
|
|
1289
|
+
/** Fetch one unit type with stats. `GET /api/v1/units-catalog/:id`. */
|
|
1290
|
+
get(id: string): Promise<UnitDetail>;
|
|
1291
|
+
/** Update a unit type's definition. `PATCH /api/v1/units-catalog/:id`. */
|
|
1292
|
+
update(id: string, input: UpdateUnitInput): Promise<Unit>;
|
|
1293
|
+
/** Activate or deactivate a unit type. */
|
|
1294
|
+
setActive(id: string, isActive: boolean): Promise<Unit>;
|
|
1295
|
+
/** Delete a unit type. */
|
|
1296
|
+
delete(id: string): Promise<void>;
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
/**
|
|
1300
|
+
* The `webhooks` resource — threshold alerts. At most one webhook per kind
|
|
1301
|
+
* (CREDIT / OUTCOME / WALLET); posting a kind again upserts it. `/api/v1/webhooks`.
|
|
1302
|
+
*/
|
|
1303
|
+
declare class Webhooks {
|
|
1304
|
+
private readonly transport;
|
|
1305
|
+
constructor(transport: Transport);
|
|
1306
|
+
/** List configured webhooks. `GET /api/v1/webhooks`. */
|
|
1307
|
+
list(): Promise<Webhook[]>;
|
|
1308
|
+
/** Create or replace the webhook for a kind. `POST /api/v1/webhooks`. */
|
|
1309
|
+
upsert(input: UpsertWebhookInput): Promise<Webhook>;
|
|
1310
|
+
/** Delete the webhook for a kind (idempotent). `DELETE /api/v1/webhooks/:kind`. */
|
|
1311
|
+
delete(kind: ThresholdKind): Promise<void>;
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
/**
|
|
1315
|
+
* The `workspace` resource — read-only introspection of the org behind the API
|
|
1316
|
+
* key. Grounds an integration (which workspace, and is it `sandbox` or `live`?)
|
|
1317
|
+
* and lists the models it may meter against. `/api/v1/me`, `/api/v1/models`.
|
|
1318
|
+
*/
|
|
1319
|
+
declare class Workspace {
|
|
1320
|
+
private readonly transport;
|
|
1321
|
+
constructor(transport: Transport);
|
|
1322
|
+
/** Identify the org + key behind the request, incl. `sandbox` | `live`. */
|
|
1323
|
+
me(): Promise<WorkspaceIdentity>;
|
|
1324
|
+
/** The org's enabled models with live prices. `{ active: true }` returns only
|
|
1325
|
+
* the models that can be metered right now. */
|
|
1326
|
+
models(params?: {
|
|
1327
|
+
active?: boolean;
|
|
1328
|
+
}): Promise<Model[]>;
|
|
475
1329
|
}
|
|
476
1330
|
|
|
477
1331
|
/**
|
|
@@ -483,7 +1337,7 @@ declare class Signals {
|
|
|
483
1337
|
* await cnk.signals.credit({
|
|
484
1338
|
* customerId: "cus_123",
|
|
485
1339
|
* model: "gpt-4o", // catalog model id
|
|
486
|
-
*
|
|
1340
|
+
* agentKey: "api_credit", // the credit's stable agent key
|
|
487
1341
|
* tokens: { input: 1200, output: 340 },
|
|
488
1342
|
* });
|
|
489
1343
|
*
|
|
@@ -492,12 +1346,30 @@ declare class Signals {
|
|
|
492
1346
|
* ```
|
|
493
1347
|
*/
|
|
494
1348
|
declare class ClockNext {
|
|
495
|
-
/** Record + read usage signals. */
|
|
1349
|
+
/** Record + read usage signals (credit / wallet / outcome / unit). */
|
|
496
1350
|
readonly signals: Signals;
|
|
497
|
-
/** Manage customers
|
|
1351
|
+
/** Manage customers, their members, wallet, and balances. */
|
|
498
1352
|
readonly customers: Customers;
|
|
1353
|
+
/** Define and manage billing plans. */
|
|
1354
|
+
readonly plans: Plans;
|
|
1355
|
+
/** Subscribe customers to plans (purchases). */
|
|
1356
|
+
readonly purchases: Purchases;
|
|
1357
|
+
/** Read invoices and mint hosted pay links. */
|
|
1358
|
+
readonly invoices: Invoices;
|
|
1359
|
+
/** Read payments (paid invoices). */
|
|
1360
|
+
readonly payments: Payments;
|
|
1361
|
+
/** Manage the credit-type catalog. */
|
|
1362
|
+
readonly credits: Credits;
|
|
1363
|
+
/** Manage the outcome-type catalog. */
|
|
1364
|
+
readonly outcomes: Outcomes;
|
|
1365
|
+
/** Manage the unit-type catalog. */
|
|
1366
|
+
readonly units: Units;
|
|
1367
|
+
/** Configure threshold webhooks. */
|
|
1368
|
+
readonly webhooks: Webhooks;
|
|
499
1369
|
/** Mint customer-portal embed tokens. */
|
|
500
1370
|
readonly portal: Portal;
|
|
1371
|
+
/** Identify the workspace (sandbox/live) + list its models. `/me`, `/models`. */
|
|
1372
|
+
readonly workspace: Workspace;
|
|
501
1373
|
private readonly cfg;
|
|
502
1374
|
private readonly flusher;
|
|
503
1375
|
constructor(config: ClockNextConfig);
|
|
@@ -518,13 +1390,10 @@ declare class ClockNext {
|
|
|
518
1390
|
declare class ClockNextError extends Error {
|
|
519
1391
|
/** HTTP status, when the failure came from a server response. */
|
|
520
1392
|
readonly status?: number;
|
|
521
|
-
/** The idempotency key of the offending signal, when applicable. */
|
|
522
|
-
readonly idempotencyKey?: string;
|
|
523
1393
|
/** Whether the SDK considers this failure worth retrying. */
|
|
524
1394
|
readonly retryable: boolean;
|
|
525
1395
|
constructor(message: string, opts?: {
|
|
526
1396
|
status?: number;
|
|
527
|
-
idempotencyKey?: string;
|
|
528
1397
|
retryable?: boolean;
|
|
529
1398
|
});
|
|
530
1399
|
}
|
|
@@ -543,14 +1412,13 @@ declare class PlanError extends ClockNextError {
|
|
|
543
1412
|
/** 422 — insufficient credits / units / outcomes for the call. */
|
|
544
1413
|
declare class AllowanceError extends ClockNextError {
|
|
545
1414
|
}
|
|
546
|
-
/** 409 — conflict
|
|
1415
|
+
/** 409 — conflict. */
|
|
547
1416
|
declare class ConflictError extends ClockNextError {
|
|
548
1417
|
}
|
|
549
1418
|
/** 429 — rate limited. */
|
|
550
1419
|
declare class RateLimitError extends ClockNextError {
|
|
551
1420
|
readonly retryAfterMs?: number;
|
|
552
1421
|
constructor(message: string, opts?: {
|
|
553
|
-
idempotencyKey?: string;
|
|
554
1422
|
retryAfterMs?: number;
|
|
555
1423
|
});
|
|
556
1424
|
}
|
|
@@ -559,22 +1427,7 @@ declare class ServerError extends ClockNextError {
|
|
|
559
1427
|
}
|
|
560
1428
|
/** fetch threw / request timed out — request never completed. */
|
|
561
1429
|
declare class NetworkError extends ClockNextError {
|
|
562
|
-
constructor(message: string
|
|
563
|
-
idempotencyKey?: string;
|
|
564
|
-
});
|
|
1430
|
+
constructor(message: string);
|
|
565
1431
|
}
|
|
566
1432
|
|
|
567
|
-
|
|
568
|
-
* Generates a stable idempotency key for one logical event. Generated ONCE at
|
|
569
|
-
* enqueue/track time and reused across every retry of that event — regenerating
|
|
570
|
-
* per HTTP attempt would defeat dedup (the documented foot-gun in most usage
|
|
571
|
-
* SDKs).
|
|
572
|
-
*
|
|
573
|
-
* Uses the Web Crypto global (`globalThis.crypto`), which is present on Node 18+
|
|
574
|
-
* and every edge runtime — and, unlike a static `node:crypto` import, never
|
|
575
|
-
* fails to load on runtimes that lack Node built-ins. Degrades through
|
|
576
|
-
* `getRandomValues` to `Math.random` so it always returns a v4 UUID.
|
|
577
|
-
*/
|
|
578
|
-
declare function newIdempotencyKey(): string;
|
|
579
|
-
|
|
580
|
-
export { AllowanceError, type ApiCustomer, AuthError, ClockNext, type ClockNextConfig, ClockNextError, ConflictError, type CreateCustomerInput, type CreditInput, type CreditSignal, type CustomerBalances, type CustomerList, type CustomerPlan, type CustomerProfileInput, type CustomerRevenue, type DropReason, type FetchLike, type Logger, NetworkError, NotFoundError, type OutcomeInput, type OutcomeSignal, PlanError, type PortalToken, type PortalTokenInput, type QueueStore, type QueuedSignal, RateLimitError, type RevenueRange, type SerializedOutcomeAttribution, type SerializedRuleApplication, type SerializedUsageLog, ServerError, type Signal, type SignalStatus, type SignalType, type Tokens, type TrackResult, type UsageList, ValidationError, type WalletInput, type WalletSignal, newIdempotencyKey, signalToWire };
|
|
1433
|
+
export { type AddMemberInput, AllowanceError, type ApiCustomer, AuthError, type BillingCycle, type BillingMode, ClockNext, type ClockNextConfig, ClockNextError, type ComponentType, ConflictError, type CreateCreditInput, type CreateCustomerInput, type CreateOutcomeInput, type CreatePlanInput, type CreatePurchaseInput, type CreateUnitInput, type Credit, type CreditAdjustInput, type CreditComponentInput, type CreditDetail, type CreditInput, type CreditSignal, type CustomerBalances, type CustomerList, type CustomerPlan, type CustomerProfileInput, type CustomerRevenue, type DropReason, type FetchLike, type FlatComponentInput, type FlatUnitInput, type Invoice, type InvoiceDetail, type InvoiceLineItem, type InvoiceListRow, type InvoiceParty, type InvoicePlanSnapshot, type InvoiceStatus, type Logger, type Member, type MemberRole, type Model, type ModelBundleEntry, NetworkError, NotFoundError, type Outcome, type OutcomeAdjustInput, type OutcomeComponentInput, type OutcomeDetail, type OutcomeInput, type OutcomeSignal, type OutcomeStepInput, type PayLink, type PaymentDetail, type PaymentRow, type Plan, type PlanComponent, type PlanComponentInput, type PlanDetail, PlanError, type PlanKind, type PlanRef, type PlanType, type PortalToken, type PortalTokenInput, type Purchase, type PurchaseDetail, type PurchaseStatus, RateLimitError, type RevenueRange, type SerializedOutcomeAttribution, type SerializedRuleApplication, type SerializedUsageLog, ServerError, type Signal, type SignalStatus, type SignalType, type ThresholdKind, type TieredUnitInput, type Tokens, type TrackResult, type Unit, type UnitAdjustInput, type UnitComponentInput, type UnitDetail, type UnitInput, type UnitPricingType, type UnitTier, type UnitTierInput, type UnitUsageList, type UnitUsageRow, type UpdateCreditInput, type UpdateMemberInput, type UpdateOutcomeInput, type UpdatePlanInput, type UpdateUnitInput, type UpsertWebhookInput, type UsageList, ValidationError, type WalletComponentInput, type WalletEntryInput, type WalletInput, type WalletList, type WalletSignal, type WalletTransaction, type WalletTransactionType, type Webhook, type WorkspaceIdentity, signalToWire };
|