@clocknext/sdk 0.15.0 → 0.17.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 +94 -8
- package/dist/index.cjs +334 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +474 -30
- package/dist/index.d.ts +474 -30
- package/dist/index.js +334 -13
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +17 -1
- package/src/errors.ts +2 -1
- package/src/index.ts +26 -0
- package/src/resources/composites.ts +82 -0
- package/src/resources/credits.ts +81 -0
- package/src/resources/customers.ts +1 -1
- package/src/resources/outcomes.ts +81 -0
- package/src/resources/plans.ts +62 -4
- package/src/resources/signals.ts +3 -1
- package/src/resources/units.ts +76 -0
- package/src/types.ts +346 -22
package/dist/index.js
CHANGED
|
@@ -63,6 +63,7 @@ var FIELD_PATHS = [
|
|
|
63
63
|
[/\busage\.inputTokens\b/g, "tokens.input"],
|
|
64
64
|
[/\busage\.outputTokens\b/g, "tokens.output"],
|
|
65
65
|
[/\busage\.cacheTokens\b/g, "tokens.cache"],
|
|
66
|
+
[/\busage\.cacheWriteTokens\b/g, "tokens.cacheWrite"],
|
|
66
67
|
[/\busage\.model\b/g, "model"],
|
|
67
68
|
// A missing/invalid `usage` block as a whole: the caller's equivalent is
|
|
68
69
|
// `tokens` (plus `model`, which the SDK hoists to the top level).
|
|
@@ -330,7 +331,7 @@ function sleep(ms) {
|
|
|
330
331
|
}
|
|
331
332
|
|
|
332
333
|
// src/http.ts
|
|
333
|
-
var SDK_VERSION = "0.
|
|
334
|
+
var SDK_VERSION = "0.17.0" ;
|
|
334
335
|
var Transport = class {
|
|
335
336
|
constructor(cfg) {
|
|
336
337
|
this.cfg = cfg;
|
|
@@ -434,6 +435,143 @@ var Transport = class {
|
|
|
434
435
|
}
|
|
435
436
|
};
|
|
436
437
|
|
|
438
|
+
// src/filters.ts
|
|
439
|
+
function activeQuery(active) {
|
|
440
|
+
return active === true ? "true" : void 0;
|
|
441
|
+
}
|
|
442
|
+
function filterByActive(rows, active) {
|
|
443
|
+
return active === false ? rows.filter((row) => !row.isActive) : rows;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// src/resources/composites.ts
|
|
447
|
+
var Composites = class {
|
|
448
|
+
constructor(transport) {
|
|
449
|
+
this.transport = transport;
|
|
450
|
+
}
|
|
451
|
+
transport;
|
|
452
|
+
/** List them. `{ active: true }` returns only active ones, `{ active: false }`
|
|
453
|
+
* only archived ones; omit it for both. */
|
|
454
|
+
async list(params = {}) {
|
|
455
|
+
const res = await this.transport.request({
|
|
456
|
+
method: "GET",
|
|
457
|
+
path: "/api/v1/composites",
|
|
458
|
+
query: { active: activeQuery(params.active) }
|
|
459
|
+
});
|
|
460
|
+
return filterByActive(res.composites, params.active);
|
|
461
|
+
}
|
|
462
|
+
/** One composite's configuration. */
|
|
463
|
+
async get(id) {
|
|
464
|
+
const res = await this.transport.request({
|
|
465
|
+
method: "GET",
|
|
466
|
+
path: `/api/v1/composites/${encodeURIComponent(id)}`
|
|
467
|
+
});
|
|
468
|
+
return res.composite;
|
|
469
|
+
}
|
|
470
|
+
/** Create one. It starts active. Not retried on failure — a retry could
|
|
471
|
+
* create a second row. */
|
|
472
|
+
async create(input) {
|
|
473
|
+
const res = await this.transport.request({
|
|
474
|
+
method: "POST",
|
|
475
|
+
path: "/api/v1/composites",
|
|
476
|
+
body: input
|
|
477
|
+
});
|
|
478
|
+
return res.composite;
|
|
479
|
+
}
|
|
480
|
+
/** PARTIAL update: only the fields you send change. `entitlements`, when
|
|
481
|
+
* sent, is the complete new set. Renaming `refId` cuts live traffic over at
|
|
482
|
+
* once — ship the new tag to your callers first. */
|
|
483
|
+
async update(id, input) {
|
|
484
|
+
const res = await this.transport.request({
|
|
485
|
+
method: "PATCH",
|
|
486
|
+
path: `/api/v1/composites/${encodeURIComponent(id)}`,
|
|
487
|
+
body: input
|
|
488
|
+
});
|
|
489
|
+
return res.composite;
|
|
490
|
+
}
|
|
491
|
+
/** Retire it (`isActive` → false). Nothing is deleted. Safe to retry. */
|
|
492
|
+
async archive(id) {
|
|
493
|
+
const res = await this.transport.request({
|
|
494
|
+
method: "POST",
|
|
495
|
+
path: `/api/v1/composites/${encodeURIComponent(id)}/archive`,
|
|
496
|
+
retry: true
|
|
497
|
+
});
|
|
498
|
+
return res.composite;
|
|
499
|
+
}
|
|
500
|
+
/** Bring an archived one back (`isActive` → true). Safe to retry. */
|
|
501
|
+
async unarchive(id) {
|
|
502
|
+
const res = await this.transport.request({
|
|
503
|
+
method: "POST",
|
|
504
|
+
path: `/api/v1/composites/${encodeURIComponent(id)}/unarchive`,
|
|
505
|
+
retry: true
|
|
506
|
+
});
|
|
507
|
+
return res.composite;
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
// src/resources/credits.ts
|
|
512
|
+
var Credits = class {
|
|
513
|
+
constructor(transport) {
|
|
514
|
+
this.transport = transport;
|
|
515
|
+
}
|
|
516
|
+
transport;
|
|
517
|
+
/** List them. `{ active: true }` returns only active ones, `{ active: false }`
|
|
518
|
+
* only archived ones; omit it for both. */
|
|
519
|
+
async list(params = {}) {
|
|
520
|
+
const res = await this.transport.request({
|
|
521
|
+
method: "GET",
|
|
522
|
+
path: "/api/v1/credits",
|
|
523
|
+
query: { active: activeQuery(params.active) }
|
|
524
|
+
});
|
|
525
|
+
return filterByActive(res.credits, params.active);
|
|
526
|
+
}
|
|
527
|
+
/** One credit's configuration. */
|
|
528
|
+
async get(id) {
|
|
529
|
+
const res = await this.transport.request({
|
|
530
|
+
method: "GET",
|
|
531
|
+
path: `/api/v1/credits/${encodeURIComponent(id)}`
|
|
532
|
+
});
|
|
533
|
+
return res.credit;
|
|
534
|
+
}
|
|
535
|
+
/** Create one. It starts active. Not retried on failure — a retry could
|
|
536
|
+
* create a second row. */
|
|
537
|
+
async create(input) {
|
|
538
|
+
const res = await this.transport.request({
|
|
539
|
+
method: "POST",
|
|
540
|
+
path: "/api/v1/credits",
|
|
541
|
+
body: input
|
|
542
|
+
});
|
|
543
|
+
return res.credit;
|
|
544
|
+
}
|
|
545
|
+
/** PARTIAL update: only the fields you send change. Send `modelBundle` to
|
|
546
|
+
* re-price; leave it out to keep the stored base price. */
|
|
547
|
+
async update(id, input) {
|
|
548
|
+
const res = await this.transport.request({
|
|
549
|
+
method: "PATCH",
|
|
550
|
+
path: `/api/v1/credits/${encodeURIComponent(id)}`,
|
|
551
|
+
body: input
|
|
552
|
+
});
|
|
553
|
+
return res.credit;
|
|
554
|
+
}
|
|
555
|
+
/** Retire it (`isActive` → false). Nothing is deleted. Safe to retry. */
|
|
556
|
+
async archive(id) {
|
|
557
|
+
const res = await this.transport.request({
|
|
558
|
+
method: "POST",
|
|
559
|
+
path: `/api/v1/credits/${encodeURIComponent(id)}/archive`,
|
|
560
|
+
retry: true
|
|
561
|
+
});
|
|
562
|
+
return res.credit;
|
|
563
|
+
}
|
|
564
|
+
/** Bring an archived one back (`isActive` → true). Safe to retry. */
|
|
565
|
+
async unarchive(id) {
|
|
566
|
+
const res = await this.transport.request({
|
|
567
|
+
method: "POST",
|
|
568
|
+
path: `/api/v1/credits/${encodeURIComponent(id)}/unarchive`,
|
|
569
|
+
retry: true
|
|
570
|
+
});
|
|
571
|
+
return res.credit;
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
|
|
437
575
|
// src/idempotency.ts
|
|
438
576
|
function newKey() {
|
|
439
577
|
const c = globalThis.crypto;
|
|
@@ -662,7 +800,7 @@ var Customers = class {
|
|
|
662
800
|
* Top up (positive `delta`) or draw down (negative `delta`) a customer's
|
|
663
801
|
* wallet, in USD — allowed whatever the customer's plan. `remaining` is the
|
|
664
802
|
* resulting USD balance. No `agentKey` — a customer has exactly one wallet.
|
|
665
|
-
* Pass `pool: "
|
|
803
|
+
* Pass `pool: "reserved"` to move the Reserved Wallet instead (then
|
|
666
804
|
* `remaining` is its balance). `POST …/:id/wallet/adjust`.
|
|
667
805
|
*/
|
|
668
806
|
adjustWallet(id, input) {
|
|
@@ -793,6 +931,71 @@ var Invoices = class {
|
|
|
793
931
|
}
|
|
794
932
|
};
|
|
795
933
|
|
|
934
|
+
// src/resources/outcomes.ts
|
|
935
|
+
var Outcomes = class {
|
|
936
|
+
constructor(transport) {
|
|
937
|
+
this.transport = transport;
|
|
938
|
+
}
|
|
939
|
+
transport;
|
|
940
|
+
/** List them. `{ active: true }` returns only active ones, `{ active: false }`
|
|
941
|
+
* only archived ones; omit it for both. */
|
|
942
|
+
async list(params = {}) {
|
|
943
|
+
const res = await this.transport.request({
|
|
944
|
+
method: "GET",
|
|
945
|
+
path: "/api/v1/outcomes",
|
|
946
|
+
query: { active: activeQuery(params.active) }
|
|
947
|
+
});
|
|
948
|
+
return filterByActive(res.outcomes, params.active);
|
|
949
|
+
}
|
|
950
|
+
/** One outcome's configuration, with every step in full. */
|
|
951
|
+
async get(id) {
|
|
952
|
+
const res = await this.transport.request({
|
|
953
|
+
method: "GET",
|
|
954
|
+
path: `/api/v1/outcomes/${encodeURIComponent(id)}`
|
|
955
|
+
});
|
|
956
|
+
return res.outcome;
|
|
957
|
+
}
|
|
958
|
+
/** Create one. It starts active. Not retried on failure — a retry could
|
|
959
|
+
* create a second row. */
|
|
960
|
+
async create(input) {
|
|
961
|
+
const res = await this.transport.request({
|
|
962
|
+
method: "POST",
|
|
963
|
+
path: "/api/v1/outcomes",
|
|
964
|
+
body: input
|
|
965
|
+
});
|
|
966
|
+
return res.outcome;
|
|
967
|
+
}
|
|
968
|
+
/** PARTIAL update: only the fields you send change. Leave `steps` out to
|
|
969
|
+
* leave the steps alone; send it and it is the complete list — pass each
|
|
970
|
+
* existing step's `id` (or its same `agentKey`) to keep it. */
|
|
971
|
+
async update(id, input) {
|
|
972
|
+
const res = await this.transport.request({
|
|
973
|
+
method: "PATCH",
|
|
974
|
+
path: `/api/v1/outcomes/${encodeURIComponent(id)}`,
|
|
975
|
+
body: input
|
|
976
|
+
});
|
|
977
|
+
return res.outcome;
|
|
978
|
+
}
|
|
979
|
+
/** Retire it (`isActive` → false). Nothing is deleted. Safe to retry. */
|
|
980
|
+
async archive(id) {
|
|
981
|
+
const res = await this.transport.request({
|
|
982
|
+
method: "POST",
|
|
983
|
+
path: `/api/v1/outcomes/${encodeURIComponent(id)}/archive`,
|
|
984
|
+
retry: true
|
|
985
|
+
});
|
|
986
|
+
return res.outcome;
|
|
987
|
+
}
|
|
988
|
+
/** Bring an archived one back (`isActive` → true). Safe to retry. */
|
|
989
|
+
async unarchive(id) {
|
|
990
|
+
const res = await this.transport.request({
|
|
991
|
+
method: "POST",
|
|
992
|
+
path: `/api/v1/outcomes/${encodeURIComponent(id)}/unarchive`,
|
|
993
|
+
retry: true
|
|
994
|
+
});
|
|
995
|
+
return res.outcome;
|
|
996
|
+
}
|
|
997
|
+
};
|
|
998
|
+
|
|
796
999
|
// src/resources/payments.ts
|
|
797
1000
|
var Payments = class {
|
|
798
1001
|
constructor(transport) {
|
|
@@ -817,14 +1020,6 @@ var Payments = class {
|
|
|
817
1020
|
}
|
|
818
1021
|
};
|
|
819
1022
|
|
|
820
|
-
// src/filters.ts
|
|
821
|
-
function activeQuery(active) {
|
|
822
|
-
return active === true ? "true" : void 0;
|
|
823
|
-
}
|
|
824
|
-
function filterByActive(rows, active) {
|
|
825
|
-
return active === false ? rows.filter((row) => !row.isActive) : rows;
|
|
826
|
-
}
|
|
827
|
-
|
|
828
1023
|
// src/resources/plans.ts
|
|
829
1024
|
var Plans = class {
|
|
830
1025
|
constructor(transport) {
|
|
@@ -842,6 +1037,54 @@ var Plans = class {
|
|
|
842
1037
|
});
|
|
843
1038
|
return filterByActive(res.plans, params.active);
|
|
844
1039
|
}
|
|
1040
|
+
/** One plan's configuration. */
|
|
1041
|
+
async get(id) {
|
|
1042
|
+
const res = await this.transport.request({
|
|
1043
|
+
method: "GET",
|
|
1044
|
+
path: `/api/v1/plans/${encodeURIComponent(id)}`
|
|
1045
|
+
});
|
|
1046
|
+
return res.plan;
|
|
1047
|
+
}
|
|
1048
|
+
/** Create a plan. It starts active. Not retried on failure — a retry could
|
|
1049
|
+
* create a second plan. */
|
|
1050
|
+
async create(input) {
|
|
1051
|
+
const res = await this.transport.request({
|
|
1052
|
+
method: "POST",
|
|
1053
|
+
path: "/api/v1/plans",
|
|
1054
|
+
body: input
|
|
1055
|
+
});
|
|
1056
|
+
return res.plan;
|
|
1057
|
+
}
|
|
1058
|
+
/** PARTIAL update: only the fields you send change. `entitlements`, when
|
|
1059
|
+
* sent, is the complete new list — the list `get()` returned can be sent
|
|
1060
|
+
* back as it is. Changes apply to new purchases only. */
|
|
1061
|
+
async update(id, input) {
|
|
1062
|
+
const res = await this.transport.request({
|
|
1063
|
+
method: "PATCH",
|
|
1064
|
+
path: `/api/v1/plans/${encodeURIComponent(id)}`,
|
|
1065
|
+
body: input
|
|
1066
|
+
});
|
|
1067
|
+
return res.plan;
|
|
1068
|
+
}
|
|
1069
|
+
/** Retire a plan (`isActive` → false): it can't be bought any more; customers
|
|
1070
|
+
* already on it are unaffected. Safe to retry. */
|
|
1071
|
+
async archive(id) {
|
|
1072
|
+
const res = await this.transport.request({
|
|
1073
|
+
method: "POST",
|
|
1074
|
+
path: `/api/v1/plans/${encodeURIComponent(id)}/archive`,
|
|
1075
|
+
retry: true
|
|
1076
|
+
});
|
|
1077
|
+
return res.plan;
|
|
1078
|
+
}
|
|
1079
|
+
/** Make an archived plan purchasable again (`isActive` → true). Safe to retry. */
|
|
1080
|
+
async unarchive(id) {
|
|
1081
|
+
const res = await this.transport.request({
|
|
1082
|
+
method: "POST",
|
|
1083
|
+
path: `/api/v1/plans/${encodeURIComponent(id)}/unarchive`,
|
|
1084
|
+
retry: true
|
|
1085
|
+
});
|
|
1086
|
+
return res.plan;
|
|
1087
|
+
}
|
|
845
1088
|
};
|
|
846
1089
|
|
|
847
1090
|
// src/resources/portal.ts
|
|
@@ -947,12 +1190,14 @@ function signalToWire(signal) {
|
|
|
947
1190
|
const body = {
|
|
948
1191
|
...customerRefToWire(signal),
|
|
949
1192
|
// `usage` is required on all three model-priced kinds. input/output are
|
|
950
|
-
// always sent; `cacheTokens` only when provided (the
|
|
1193
|
+
// always sent; `cacheTokens` / `cacheWriteTokens` only when provided (the
|
|
1194
|
+
// server defaults each to 0).
|
|
951
1195
|
usage: {
|
|
952
1196
|
model: signal.model,
|
|
953
1197
|
inputTokens: t.input,
|
|
954
1198
|
outputTokens: t.output,
|
|
955
|
-
...t.cache != null ? { cacheTokens: t.cache } : {}
|
|
1199
|
+
...t.cache != null ? { cacheTokens: t.cache } : {},
|
|
1200
|
+
...t.cacheWrite != null ? { cacheWriteTokens: t.cacheWrite } : {}
|
|
956
1201
|
}
|
|
957
1202
|
};
|
|
958
1203
|
if (signal.member) body.member = signal.member;
|
|
@@ -1202,14 +1447,86 @@ var Signals = class {
|
|
|
1202
1447
|
}
|
|
1203
1448
|
};
|
|
1204
1449
|
|
|
1450
|
+
// src/resources/units.ts
|
|
1451
|
+
var Units = class {
|
|
1452
|
+
constructor(transport) {
|
|
1453
|
+
this.transport = transport;
|
|
1454
|
+
}
|
|
1455
|
+
transport;
|
|
1456
|
+
/** List them. `{ active: true }` returns only active ones, `{ active: false }`
|
|
1457
|
+
* only archived ones; omit it for both. */
|
|
1458
|
+
async list(params = {}) {
|
|
1459
|
+
const res = await this.transport.request({
|
|
1460
|
+
method: "GET",
|
|
1461
|
+
path: "/api/v1/units",
|
|
1462
|
+
query: { active: activeQuery(params.active) }
|
|
1463
|
+
});
|
|
1464
|
+
return filterByActive(res.units, params.active);
|
|
1465
|
+
}
|
|
1466
|
+
/** One unit's configuration. */
|
|
1467
|
+
async get(id) {
|
|
1468
|
+
const res = await this.transport.request({
|
|
1469
|
+
method: "GET",
|
|
1470
|
+
path: `/api/v1/units/${encodeURIComponent(id)}`
|
|
1471
|
+
});
|
|
1472
|
+
return res.unit;
|
|
1473
|
+
}
|
|
1474
|
+
/** Create one. It starts active. Not retried on failure — a retry could
|
|
1475
|
+
* create a second row. */
|
|
1476
|
+
async create(input) {
|
|
1477
|
+
const res = await this.transport.request({
|
|
1478
|
+
method: "POST",
|
|
1479
|
+
path: "/api/v1/units",
|
|
1480
|
+
body: input
|
|
1481
|
+
});
|
|
1482
|
+
return res.unit;
|
|
1483
|
+
}
|
|
1484
|
+
/** PARTIAL update: only the fields you send change. `tiers`, when sent, is
|
|
1485
|
+
* the complete new tier list. */
|
|
1486
|
+
async update(id, input) {
|
|
1487
|
+
const res = await this.transport.request({
|
|
1488
|
+
method: "PATCH",
|
|
1489
|
+
path: `/api/v1/units/${encodeURIComponent(id)}`,
|
|
1490
|
+
body: input
|
|
1491
|
+
});
|
|
1492
|
+
return res.unit;
|
|
1493
|
+
}
|
|
1494
|
+
/** Retire it (`isActive` → false). Nothing is deleted. Safe to retry. */
|
|
1495
|
+
async archive(id) {
|
|
1496
|
+
const res = await this.transport.request({
|
|
1497
|
+
method: "POST",
|
|
1498
|
+
path: `/api/v1/units/${encodeURIComponent(id)}/archive`,
|
|
1499
|
+
retry: true
|
|
1500
|
+
});
|
|
1501
|
+
return res.unit;
|
|
1502
|
+
}
|
|
1503
|
+
/** Bring an archived one back (`isActive` → true). Safe to retry. */
|
|
1504
|
+
async unarchive(id) {
|
|
1505
|
+
const res = await this.transport.request({
|
|
1506
|
+
method: "POST",
|
|
1507
|
+
path: `/api/v1/units/${encodeURIComponent(id)}/unarchive`,
|
|
1508
|
+
retry: true
|
|
1509
|
+
});
|
|
1510
|
+
return res.unit;
|
|
1511
|
+
}
|
|
1512
|
+
};
|
|
1513
|
+
|
|
1205
1514
|
// src/client.ts
|
|
1206
1515
|
var ClockNext = class {
|
|
1207
1516
|
/** Record usage signals (credit / wallet / outcome / unit) and read the usage log. */
|
|
1208
1517
|
signals;
|
|
1209
1518
|
/** Manage customers, their members, and balances. */
|
|
1210
1519
|
customers;
|
|
1211
|
-
/**
|
|
1520
|
+
/** Billing plans: list, get, create, update, archive, unarchive. */
|
|
1212
1521
|
plans;
|
|
1522
|
+
/** Credits: list, get, create, update, archive, unarchive (no delete). */
|
|
1523
|
+
credits;
|
|
1524
|
+
/** Outcomes: list, get, create, update, archive, unarchive (no delete). */
|
|
1525
|
+
outcomes;
|
|
1526
|
+
/** Units: list, get, create, update, archive, unarchive (no delete). */
|
|
1527
|
+
units;
|
|
1528
|
+
/** Composites: list, get, create, update, archive, unarchive (no delete). */
|
|
1529
|
+
composites;
|
|
1213
1530
|
/** Subscribe customers to plans (purchases). */
|
|
1214
1531
|
purchases;
|
|
1215
1532
|
/** Read invoices and mint hosted pay links. */
|
|
@@ -1232,6 +1549,10 @@ var ClockNext = class {
|
|
|
1232
1549
|
this.signals = new Signals(transport, this.flusher, this.cfg);
|
|
1233
1550
|
this.customers = new Customers(transport);
|
|
1234
1551
|
this.plans = new Plans(transport);
|
|
1552
|
+
this.credits = new Credits(transport);
|
|
1553
|
+
this.outcomes = new Outcomes(transport);
|
|
1554
|
+
this.units = new Units(transport);
|
|
1555
|
+
this.composites = new Composites(transport);
|
|
1235
1556
|
this.purchases = new Purchases(transport);
|
|
1236
1557
|
this.invoices = new Invoices(transport);
|
|
1237
1558
|
this.payments = new Payments(transport);
|