@limitry/cli 1.0.0-rc.1 → 1.0.0-rc.11
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/CHANGELOG.md +196 -0
- package/README.md +57 -9
- package/dist/limitry.mjs +880 -615
- package/package.json +5 -4
package/dist/limitry.mjs
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __export = (target, all) => {
|
|
4
|
+
for (var name in all)
|
|
5
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
6
|
+
};
|
|
2
7
|
|
|
3
8
|
// src/main.ts
|
|
4
9
|
import { spawn } from "node:child_process";
|
|
@@ -12,7 +17,20 @@ var brand_default = {
|
|
|
12
17
|
productName: "Limitry",
|
|
13
18
|
legalName: "Omsome L.L.C-FZ",
|
|
14
19
|
releaseMirrors: true,
|
|
20
|
+
mirrorProfile: {
|
|
21
|
+
topics: [
|
|
22
|
+
"rate-limiting",
|
|
23
|
+
"usage-limits",
|
|
24
|
+
"credits",
|
|
25
|
+
"budgets",
|
|
26
|
+
"api-client",
|
|
27
|
+
"sdk"
|
|
28
|
+
]
|
|
29
|
+
},
|
|
15
30
|
domain: "limitry.com",
|
|
31
|
+
devDomain: "limitry.dev",
|
|
32
|
+
labs: ["lab"],
|
|
33
|
+
discordInvite: "https://discord.gg/Nb6QFd6ER4",
|
|
16
34
|
summary: "Limitry is the limits, credits and budgets layer for your software: give each of your users, teams, tenants, API keys or agents an allowance, and before any costly action one call tells your code whether it may happen.",
|
|
17
35
|
expectedAssets: [
|
|
18
36
|
"logo.svg",
|
|
@@ -39,6 +57,16 @@ var brand_default = {
|
|
|
39
57
|
display: "Archivo Variable",
|
|
40
58
|
body: "IBM Plex Sans"
|
|
41
59
|
},
|
|
60
|
+
layout: {
|
|
61
|
+
radius: "3px",
|
|
62
|
+
gutter: "1.5rem",
|
|
63
|
+
contentWidth: "64rem",
|
|
64
|
+
readableWidth: "42rem",
|
|
65
|
+
sectionGap: "2rem",
|
|
66
|
+
navGap: "1.5rem",
|
|
67
|
+
barPadding: "1rem",
|
|
68
|
+
navCurrentWeight: "400"
|
|
69
|
+
},
|
|
42
70
|
environments: [
|
|
43
71
|
{
|
|
44
72
|
name: "test",
|
|
@@ -57,7 +85,8 @@ var brand_default = {
|
|
|
57
85
|
signIn: {
|
|
58
86
|
providerOrder: ["github", "google"],
|
|
59
87
|
emailSignUp: "button"
|
|
60
|
-
}
|
|
88
|
+
},
|
|
89
|
+
devPortOffset: 100
|
|
61
90
|
};
|
|
62
91
|
|
|
63
92
|
// src/brand/index.ts
|
|
@@ -82,13 +111,39 @@ var brand = {
|
|
|
82
111
|
*/
|
|
83
112
|
legalName: optional.legalName ?? brand_default.productName,
|
|
84
113
|
domain: brand_default.domain,
|
|
114
|
+
/**
|
|
115
|
+
* The apex of everything that isn't prod: stage is `*.stage.<devDomain>`
|
|
116
|
+
* (docs/deployment.md "Domains for stage and lab"). `domain` unless
|
|
117
|
+
* brand.json sets one.
|
|
118
|
+
*/
|
|
119
|
+
devDomain: optional.devDomain ?? brand_default.domain,
|
|
120
|
+
/**
|
|
121
|
+
* Where the product's community talks: https://<domain>/discord, the
|
|
122
|
+
* site's redirect to brand.json `discordInvite` (pnpm brand:sync); null
|
|
123
|
+
* when the product has no Discord.
|
|
124
|
+
*/
|
|
125
|
+
discord: optional.discordInvite ? `https://${brand_default.domain}/discord` : null,
|
|
85
126
|
/** One sentence: what the product is — llms.txt and the agent skill lead with it. */
|
|
86
127
|
summary: optional.summary ?? `${brand_default.productName} documentation and API.`,
|
|
87
128
|
/** Where vulnerability reports go — /.well-known/security.txt and /security publish it. Defaults to security@<domain>. */
|
|
88
129
|
securityContact: optional.securityContact ?? `security@${brand_default.domain}`,
|
|
89
130
|
/** The support address the /contact page shows. Defaults to support@<domain>. */
|
|
90
131
|
supportContact: optional.supportContact ?? `support@${brand_default.domain}`,
|
|
132
|
+
/**
|
|
133
|
+
* Where operational alerts go (dead letters, failed crons, unhandled
|
|
134
|
+
* API errors, failed uptime probes — the worker's `sendOpsAlert`).
|
|
135
|
+
* Defaults to ops@<domain>; null when brand.json sets `false` (the
|
|
136
|
+
* platform's own deployment: no customers, alerts only logged).
|
|
137
|
+
*/
|
|
138
|
+
opsContact: optional.opsContact === false ? null : optional.opsContact ?? `ops@${brand_default.domain}`,
|
|
91
139
|
colors: brand_default.colors,
|
|
140
|
+
/**
|
|
141
|
+
* True once the product has launched (brand.json `launched`, set at
|
|
142
|
+
* launch, never back): its data is customers', so the pre-launch
|
|
143
|
+
* product reset's `/ops` route is not mounted at all
|
|
144
|
+
* (docs/deployment.md "Resetting product data"). False when absent.
|
|
145
|
+
*/
|
|
146
|
+
launched: optional.launched === true,
|
|
92
147
|
/** True once the product has its own brand (brand.json `status: "ready"`), not the platform's placeholders. */
|
|
93
148
|
ready: brand_default.status === "ready"
|
|
94
149
|
};
|
|
@@ -673,56 +728,283 @@ function apiErrorFrom(error, response) {
|
|
|
673
728
|
return new ApiError(response.status, envelope.error?.code ?? "http_error", envelope.error?.message ?? `HTTP ${response.status}`, envelope.error?.requestId);
|
|
674
729
|
}
|
|
675
730
|
|
|
731
|
+
// node_modules/@limitry/sdk/dist/product/index.js
|
|
732
|
+
var product_exports = {};
|
|
733
|
+
__export(product_exports, {
|
|
734
|
+
LimitReached: () => LimitReached,
|
|
735
|
+
Unavailable: () => Unavailable,
|
|
736
|
+
createLimits: () => createLimits,
|
|
737
|
+
productMethods: () => productMethods,
|
|
738
|
+
productRetry: () => productRetry
|
|
739
|
+
});
|
|
740
|
+
|
|
741
|
+
// node_modules/@limitry/sdk/dist/retry.js
|
|
742
|
+
var WRITES = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH", "DELETE"]);
|
|
743
|
+
var RETRY_STATUSES = /* @__PURE__ */ new Set([408, 429, 500, 502, 503, 504]);
|
|
744
|
+
var DEFAULT_MAX_RETRIES = 2;
|
|
745
|
+
var MAX_WAIT_MS = 2e4;
|
|
746
|
+
var MAX_BASE_KEY = 200;
|
|
747
|
+
var PRODUCT_RETRY_HEADER = "x-sdk-product-retry";
|
|
748
|
+
var productRetry2 = () => "productRetry" in product_exports ? productRetry ?? null : null;
|
|
749
|
+
var wait = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
750
|
+
function backoff(attempt) {
|
|
751
|
+
const base = Math.min(500 * 2 ** attempt, 8e3);
|
|
752
|
+
return base * (0.75 + Math.random() * 0.25);
|
|
753
|
+
}
|
|
754
|
+
function retryAfter(response) {
|
|
755
|
+
const value = response.headers.get("retry-after");
|
|
756
|
+
if (!value)
|
|
757
|
+
return void 0;
|
|
758
|
+
const seconds = Number(value);
|
|
759
|
+
if (Number.isFinite(seconds))
|
|
760
|
+
return Math.max(0, seconds * 1e3);
|
|
761
|
+
const date = Date.parse(value);
|
|
762
|
+
return Number.isNaN(date) ? void 0 : Math.max(0, date - Date.now());
|
|
763
|
+
}
|
|
764
|
+
async function retryable(response) {
|
|
765
|
+
if (RETRY_STATUSES.has(response.status))
|
|
766
|
+
return true;
|
|
767
|
+
if (response.status !== 409)
|
|
768
|
+
return false;
|
|
769
|
+
try {
|
|
770
|
+
const body = await response.clone().json();
|
|
771
|
+
return body.error?.code === "idempotency_in_progress";
|
|
772
|
+
} catch {
|
|
773
|
+
return false;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
function takeProductRetryHeader(request) {
|
|
777
|
+
if (!request.headers.has(PRODUCT_RETRY_HEADER))
|
|
778
|
+
return [request, false];
|
|
779
|
+
const returns = request.headers.get(PRODUCT_RETRY_HEADER) === "return";
|
|
780
|
+
const headers = new Headers(request.headers);
|
|
781
|
+
headers.delete(PRODUCT_RETRY_HEADER);
|
|
782
|
+
return [new Request(request, { headers }), returns];
|
|
783
|
+
}
|
|
784
|
+
function withoutProductRetryHeader(fetch2) {
|
|
785
|
+
return (request) => fetch2(takeProductRetryHeader(request)[0]);
|
|
786
|
+
}
|
|
787
|
+
function withRetries(fetch2, options = {}) {
|
|
788
|
+
const maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
789
|
+
const sleep = options.sleep ?? wait;
|
|
790
|
+
const later = options.product === void 0 ? productRetry2() : options.product;
|
|
791
|
+
return async (request) => {
|
|
792
|
+
let returnLater;
|
|
793
|
+
[request, returnLater] = takeProductRetryHeader(request);
|
|
794
|
+
if (WRITES.has(request.method) && !request.headers.has("idempotency-key")) {
|
|
795
|
+
const url = new URL(request.url);
|
|
796
|
+
const key = options.key ? await options.key(request.method, url.pathname) : crypto.randomUUID();
|
|
797
|
+
const headers = new Headers(request.headers);
|
|
798
|
+
headers.set("idempotency-key", key);
|
|
799
|
+
request = new Request(request, { headers });
|
|
800
|
+
}
|
|
801
|
+
let laterTries = 0;
|
|
802
|
+
let laterWaited = 0;
|
|
803
|
+
for (let attempt = 0; ; attempt++) {
|
|
804
|
+
const last = attempt >= maxRetries;
|
|
805
|
+
const sending = last && !later ? request : request.clone();
|
|
806
|
+
let response;
|
|
807
|
+
try {
|
|
808
|
+
response = await fetch2(sending);
|
|
809
|
+
} catch (error) {
|
|
810
|
+
if (last || request.signal.aborted)
|
|
811
|
+
throw error;
|
|
812
|
+
await sleep(backoff(attempt));
|
|
813
|
+
continue;
|
|
814
|
+
}
|
|
815
|
+
const laterMs = later ? await later.wait(response, laterTries) : null;
|
|
816
|
+
if (laterMs !== null && later) {
|
|
817
|
+
if (returnLater || laterWaited + laterMs > later.budgetMs) {
|
|
818
|
+
return response;
|
|
819
|
+
}
|
|
820
|
+
laterTries++;
|
|
821
|
+
laterWaited += laterMs;
|
|
822
|
+
await response.body?.cancel();
|
|
823
|
+
await sleep(laterMs);
|
|
824
|
+
attempt--;
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
827
|
+
if (last || !await retryable(response))
|
|
828
|
+
return response;
|
|
829
|
+
const delay = retryAfter(response) ?? backoff(attempt);
|
|
830
|
+
if (delay > MAX_WAIT_MS)
|
|
831
|
+
return response;
|
|
832
|
+
await response.body?.cancel();
|
|
833
|
+
await sleep(delay);
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
var hex = (bytes) => Array.from(new Uint8Array(bytes), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
838
|
+
function keysFrom(base) {
|
|
839
|
+
if (base !== void 0 && (base.length === 0 || base.length > MAX_BASE_KEY)) {
|
|
840
|
+
throw new Error(`the idempotency key must be 1\u2013${MAX_BASE_KEY} characters`);
|
|
841
|
+
}
|
|
842
|
+
if (base !== void 0 && !/^[\x21-\x7e]+$/.test(base)) {
|
|
843
|
+
throw new Error("the idempotency key must be printable ASCII with no spaces");
|
|
844
|
+
}
|
|
845
|
+
const seen = /* @__PURE__ */ new Map();
|
|
846
|
+
return async (method, path2) => {
|
|
847
|
+
if (base === void 0)
|
|
848
|
+
return crypto.randomUUID();
|
|
849
|
+
const target = `${method} ${path2}`;
|
|
850
|
+
const n = (seen.get(target) ?? 0) + 1;
|
|
851
|
+
seen.set(target, n);
|
|
852
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(`${target}#${n}`));
|
|
853
|
+
return `${base}:${hex(digest).slice(0, 16)}`;
|
|
854
|
+
};
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
// node_modules/@limitry/sdk/dist/product/moving.js
|
|
858
|
+
var MOVING_MS = 3e4;
|
|
859
|
+
var JITTER_BASE_MS = 250;
|
|
860
|
+
var JITTER_CAP_MS = 4e3;
|
|
861
|
+
function retryWaitMs(retryAfterMs2, attempt, random = Math.random) {
|
|
862
|
+
const floor = Math.max(1e3, retryAfterMs2 ?? 1e3);
|
|
863
|
+
const ceiling = Math.min(JITTER_CAP_MS, JITTER_BASE_MS * 2 ** attempt);
|
|
864
|
+
return floor + random() * ceiling;
|
|
865
|
+
}
|
|
866
|
+
async function retryLater(response) {
|
|
867
|
+
if (response.status !== 503)
|
|
868
|
+
return null;
|
|
869
|
+
try {
|
|
870
|
+
const body = await response.clone().json();
|
|
871
|
+
const code2 = body.error?.code;
|
|
872
|
+
return code2 === "moving" || code2 === "busy" ? code2 : null;
|
|
873
|
+
} catch {
|
|
874
|
+
return null;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
function retryAfter2(response) {
|
|
878
|
+
const value = response.headers.get("retry-after");
|
|
879
|
+
if (!value)
|
|
880
|
+
return void 0;
|
|
881
|
+
const seconds = Number(value);
|
|
882
|
+
if (Number.isFinite(seconds))
|
|
883
|
+
return Math.max(0, seconds * 1e3);
|
|
884
|
+
const date = Date.parse(value);
|
|
885
|
+
return Number.isNaN(date) ? void 0 : Math.max(0, date - Date.now());
|
|
886
|
+
}
|
|
887
|
+
function movingRetry(random = Math.random) {
|
|
888
|
+
return {
|
|
889
|
+
wait: async (response, tries) => await retryLater(response) ? retryWaitMs(retryAfter2(response), tries, random) : null,
|
|
890
|
+
budgetMs: MOVING_MS
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
|
|
676
894
|
// node_modules/@limitry/sdk/dist/product/limits.js
|
|
677
|
-
var
|
|
895
|
+
var MAX_COST = 1e9;
|
|
896
|
+
function checkedCost(units) {
|
|
897
|
+
if (!Number.isInteger(units) || units < 0 || units > MAX_COST) {
|
|
898
|
+
throw new RangeError(`reservation.cost(${units}): the cost must be a whole number from 0 to ${MAX_COST}`);
|
|
899
|
+
}
|
|
900
|
+
return units;
|
|
901
|
+
}
|
|
902
|
+
var LimitReached = class extends Error {
|
|
678
903
|
reason;
|
|
679
|
-
/** The limit that refused; null
|
|
680
|
-
|
|
904
|
+
/** The limit that refused; null for too many open reservations. */
|
|
905
|
+
limitId;
|
|
681
906
|
retryAfterSeconds;
|
|
682
907
|
decision;
|
|
683
908
|
constructor(decision) {
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
this.
|
|
687
|
-
this.
|
|
688
|
-
this.
|
|
689
|
-
this.retryAfterSeconds = decision?.retryAfterSeconds ?? null;
|
|
909
|
+
super(decision.reason?.message ?? "Refused");
|
|
910
|
+
this.name = "LimitReached";
|
|
911
|
+
this.reason = decision.reason?.code ?? "limit_reached";
|
|
912
|
+
this.limitId = decision.reason?.limitId ?? null;
|
|
913
|
+
this.retryAfterSeconds = decision.retryAfterSeconds ?? null;
|
|
690
914
|
this.decision = decision;
|
|
691
915
|
}
|
|
692
916
|
};
|
|
917
|
+
var Unavailable = class extends Error {
|
|
918
|
+
constructor() {
|
|
919
|
+
super("Limitry could not be reached and a limit refuses when unreachable");
|
|
920
|
+
this.name = "Unavailable";
|
|
921
|
+
}
|
|
922
|
+
};
|
|
693
923
|
var UNREACHABLE = /* @__PURE__ */ Symbol("unreachable");
|
|
694
924
|
var TIMED_OUT = /* @__PURE__ */ Symbol("timed out");
|
|
695
|
-
var
|
|
696
|
-
|
|
925
|
+
var FINISH_TIMEOUT_MS = 5e3;
|
|
926
|
+
var wait2 = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
927
|
+
function retryAfterMs(response) {
|
|
928
|
+
const value = response.headers.get("retry-after");
|
|
929
|
+
const seconds = value === null ? NaN : Number(value);
|
|
930
|
+
return Number.isFinite(seconds) ? seconds * 1e3 : void 0;
|
|
931
|
+
}
|
|
932
|
+
function createLimits(client, options = {}, internals = {}) {
|
|
697
933
|
const timeoutMs = options.timeoutMs ?? 1500;
|
|
934
|
+
const sleep = internals.sleep ?? wait2;
|
|
935
|
+
const random = internals.random ?? Math.random;
|
|
698
936
|
const remembered = /* @__PURE__ */ new Map();
|
|
699
937
|
const key = (s, action) => `${s.kind}\0${s.id}\0${action}`;
|
|
700
938
|
const failMode = (s, action) => options.failMode ?? remembered.get(key(s, action)) ?? "open";
|
|
939
|
+
const concurrentAmounts = /* @__PURE__ */ new Map();
|
|
940
|
+
const running = /* @__PURE__ */ new Map();
|
|
701
941
|
const remember = (s, action, decision) => {
|
|
942
|
+
const amounts = decision.limits.filter((r) => r.window === "concurrent" && typeof r.amount === "number").map((r) => r.amount);
|
|
943
|
+
if (amounts.length > 0) {
|
|
944
|
+
concurrentAmounts.set(key(s, action), Math.min(...amounts));
|
|
945
|
+
if (concurrentAmounts.size > 1e4) {
|
|
946
|
+
concurrentAmounts.delete(concurrentAmounts.keys().next().value);
|
|
947
|
+
}
|
|
948
|
+
} else {
|
|
949
|
+
concurrentAmounts.delete(key(s, action));
|
|
950
|
+
}
|
|
702
951
|
if (options.failMode)
|
|
703
952
|
return;
|
|
704
|
-
const closed = decision.
|
|
953
|
+
const closed = decision.limits.some((r) => r.failMode === "closed");
|
|
705
954
|
remembered.set(key(s, action), closed ? "closed" : "open");
|
|
706
955
|
if (remembered.size > 1e4) {
|
|
707
956
|
remembered.delete(remembered.keys().next().value);
|
|
708
957
|
}
|
|
709
958
|
};
|
|
710
|
-
|
|
959
|
+
function budget(budgetMs) {
|
|
960
|
+
const started = Date.now();
|
|
961
|
+
let slept = 0;
|
|
962
|
+
return {
|
|
963
|
+
// Whole ms: AbortSignal.timeout refuses a fraction (jitter makes them).
|
|
964
|
+
left: () => Math.ceil(budgetMs - Math.max(Date.now() - started, slept)),
|
|
965
|
+
async sleep(ms) {
|
|
966
|
+
slept += ms;
|
|
967
|
+
await sleep(ms);
|
|
968
|
+
}
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
async function check(body, idempotencyKey, budgetMs = timeoutMs, finishOnly = false) {
|
|
972
|
+
const time = budget(budgetMs);
|
|
973
|
+
for (let tries = 0; ; tries++) {
|
|
974
|
+
const answer2 = await checkOnce(body, idempotencyKey, Math.max(1, time.left()), finishOnly);
|
|
975
|
+
if (answer2.kind !== "later")
|
|
976
|
+
return answer2.value;
|
|
977
|
+
const pause = retryWaitMs(answer2.retryAfterMs, tries, random);
|
|
978
|
+
if (pause >= time.left())
|
|
979
|
+
return UNREACHABLE;
|
|
980
|
+
await time.sleep(pause);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
async function checkOnce(body, idempotencyKey, wait3, finishOnly) {
|
|
711
984
|
let timer;
|
|
712
985
|
const late = new Promise((resolve2) => {
|
|
713
|
-
timer = setTimeout(() => resolve2(TIMED_OUT),
|
|
986
|
+
timer = setTimeout(() => resolve2(TIMED_OUT), wait3);
|
|
714
987
|
});
|
|
715
988
|
const asked = (async () => {
|
|
716
989
|
try {
|
|
717
990
|
const { data, error, response } = await client.POST("/checks", {
|
|
718
991
|
body,
|
|
719
|
-
headers: {
|
|
720
|
-
|
|
992
|
+
headers: {
|
|
993
|
+
"idempotency-key": idempotencyKey,
|
|
994
|
+
// `moving` and `busy` come back here at once: this loop
|
|
995
|
+
// retries them.
|
|
996
|
+
[PRODUCT_RETRY_HEADER]: "return",
|
|
997
|
+
...finishOnly ? { "limitry-finish-only": "1" } : {}
|
|
998
|
+
},
|
|
999
|
+
signal: AbortSignal.timeout(wait3)
|
|
721
1000
|
});
|
|
722
1001
|
if (data)
|
|
723
1002
|
return data;
|
|
724
|
-
if (response
|
|
1003
|
+
if (isRetryLater(response, error))
|
|
1004
|
+
return { later: retryAfterMs(response) };
|
|
1005
|
+
if (response.status === 402 || response.status === 429) {
|
|
725
1006
|
return UNREACHABLE;
|
|
1007
|
+
}
|
|
726
1008
|
if (response.status < 500 && error) {
|
|
727
1009
|
return new Error(`Limitry refused the check (${response.status}): ${JSON.stringify(error)}`);
|
|
728
1010
|
}
|
|
@@ -735,30 +1017,44 @@ function createLimits(client, options = {}) {
|
|
|
735
1017
|
const answer2 = await Promise.race([asked, late]);
|
|
736
1018
|
if (answer2 instanceof Error)
|
|
737
1019
|
throw answer2;
|
|
738
|
-
|
|
1020
|
+
if (typeof answer2 === "object" && "later" in answer2)
|
|
1021
|
+
return { kind: "later", retryAfterMs: answer2.later };
|
|
1022
|
+
return { kind: "answer", value: answer2 };
|
|
739
1023
|
} finally {
|
|
740
1024
|
clearTimeout(timer);
|
|
741
1025
|
}
|
|
742
1026
|
}
|
|
743
1027
|
async function settle(id, action, cost) {
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
1028
|
+
const idempotencyKey = crypto.randomUUID();
|
|
1029
|
+
const time = budget(timeoutMs);
|
|
1030
|
+
for (let tries = 0; ; tries++) {
|
|
1031
|
+
let retryMs = null;
|
|
1032
|
+
try {
|
|
1033
|
+
const headers = {
|
|
1034
|
+
"idempotency-key": idempotencyKey,
|
|
1035
|
+
[PRODUCT_RETRY_HEADER]: "return"
|
|
1036
|
+
};
|
|
1037
|
+
const { error, response } = action === "commit" ? await client.POST("/reservations/{reservationId}/commit", {
|
|
747
1038
|
params: { path: { reservationId: id } },
|
|
748
1039
|
body: { cost: cost ?? 0 },
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
await client.POST("/reservations/{reservationId}/release", {
|
|
1040
|
+
headers,
|
|
1041
|
+
signal: AbortSignal.timeout(Math.max(1, time.left()))
|
|
1042
|
+
}) : await client.POST("/reservations/{reservationId}/release", {
|
|
753
1043
|
params: { path: { reservationId: id } },
|
|
754
|
-
|
|
1044
|
+
headers,
|
|
1045
|
+
signal: AbortSignal.timeout(Math.max(1, time.left()))
|
|
755
1046
|
});
|
|
1047
|
+
if (isRetryLater(response, error))
|
|
1048
|
+
retryMs = retryWaitMs(retryAfterMs(response), tries, random);
|
|
1049
|
+
} catch {
|
|
756
1050
|
}
|
|
757
|
-
|
|
1051
|
+
if (retryMs === null || retryMs >= time.left())
|
|
1052
|
+
return;
|
|
1053
|
+
await time.sleep(retryMs);
|
|
758
1054
|
}
|
|
759
1055
|
}
|
|
760
1056
|
async function releaseUnheard(body, idempotencyKey) {
|
|
761
|
-
const replay = await check(body, idempotencyKey
|
|
1057
|
+
const replay = await check(body, idempotencyKey);
|
|
762
1058
|
if (typeof replay === "object" && replay.reservation) {
|
|
763
1059
|
await settle(replay.reservation.id, "release");
|
|
764
1060
|
}
|
|
@@ -775,8 +1071,7 @@ function createLimits(client, options = {}) {
|
|
|
775
1071
|
const { data, error, response } = await client.GET("/usage/standing", {
|
|
776
1072
|
params: {
|
|
777
1073
|
query: {
|
|
778
|
-
|
|
779
|
-
id: request.subject.id,
|
|
1074
|
+
subject: `${request.subject.kind}:${request.subject.id}`,
|
|
780
1075
|
...request.shared?.length ? {
|
|
781
1076
|
shared: request.shared.map((s) => `${s.kind}:${s.id}`).join(",")
|
|
782
1077
|
} : {}
|
|
@@ -789,92 +1084,130 @@ function createLimits(client, options = {}) {
|
|
|
789
1084
|
},
|
|
790
1085
|
/** Checks `cost`, runs `fn` only if allowed, and returns its result. */
|
|
791
1086
|
async guard(request, fn) {
|
|
792
|
-
const
|
|
1087
|
+
const body = {
|
|
793
1088
|
subject: request.subject,
|
|
794
1089
|
shared: request.shared,
|
|
795
1090
|
action: request.action,
|
|
796
1091
|
cost: request.cost ?? 1
|
|
797
|
-
}
|
|
1092
|
+
};
|
|
1093
|
+
const idempotencyKey = crypto.randomUUID();
|
|
1094
|
+
const decision = await check(body, idempotencyKey);
|
|
798
1095
|
if (decision === UNREACHABLE || decision === TIMED_OUT) {
|
|
799
1096
|
if (failMode(request.subject, request.action) === "closed") {
|
|
800
|
-
throw new
|
|
1097
|
+
throw new Unavailable();
|
|
1098
|
+
}
|
|
1099
|
+
if (decision === TIMED_OUT) {
|
|
1100
|
+
void check(body, idempotencyKey, FINISH_TIMEOUT_MS, true).catch(() => {
|
|
1101
|
+
});
|
|
801
1102
|
}
|
|
802
1103
|
return fn();
|
|
803
1104
|
}
|
|
804
1105
|
remember(request.subject, request.action, decision);
|
|
805
1106
|
if (!decision.allowed)
|
|
806
|
-
throw new
|
|
1107
|
+
throw new LimitReached(decision);
|
|
807
1108
|
return fn();
|
|
808
1109
|
},
|
|
809
1110
|
/**
|
|
810
|
-
*
|
|
811
|
-
* (`
|
|
812
|
-
* `fn` throws, and rethrows. A concurrent limit's slot
|
|
813
|
-
* exactly as long as `fn` runs.
|
|
1111
|
+
* Reserves `cost`, runs `fn`, then commits the cost it reported
|
|
1112
|
+
* (`reservation.cost(n)`, default the reserved cost) — or releases the
|
|
1113
|
+
* reservation if `fn` throws, and rethrows. A concurrent limit's slot
|
|
1114
|
+
* is taken for exactly as long as `fn` runs.
|
|
814
1115
|
*/
|
|
815
|
-
async
|
|
816
|
-
const
|
|
1116
|
+
async reserve(request, fn) {
|
|
1117
|
+
const reserved = request.cost ?? 1;
|
|
817
1118
|
const body = {
|
|
818
1119
|
subject: request.subject,
|
|
819
1120
|
shared: request.shared,
|
|
820
1121
|
action: request.action,
|
|
821
|
-
cost:
|
|
1122
|
+
cost: reserved,
|
|
822
1123
|
mode: "reserve",
|
|
823
1124
|
ttlSeconds: request.ttlSeconds
|
|
824
1125
|
};
|
|
825
1126
|
const idempotencyKey = crypto.randomUUID();
|
|
826
|
-
const
|
|
827
|
-
const
|
|
828
|
-
|
|
829
|
-
} };
|
|
830
|
-
if (decision === UNREACHABLE || decision === TIMED_OUT) {
|
|
831
|
-
const unheard = () => decision === TIMED_OUT ? releaseUnheard(body, idempotencyKey) : Promise.resolve();
|
|
832
|
-
if (failMode(request.subject, request.action) === "closed") {
|
|
833
|
-
await unheard();
|
|
834
|
-
throw new LimitExceeded(null);
|
|
835
|
-
}
|
|
836
|
-
try {
|
|
837
|
-
return await fn(unmetered);
|
|
838
|
-
} finally {
|
|
839
|
-
await unheard();
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
remember(request.subject, request.action, decision);
|
|
843
|
-
if (!decision.allowed || !decision.reservation) {
|
|
844
|
-
throw new LimitExceeded(decision);
|
|
845
|
-
}
|
|
846
|
-
const id = decision.reservation.id;
|
|
847
|
-
let used = estimate;
|
|
848
|
-
const held = {
|
|
849
|
-
cost(units) {
|
|
850
|
-
used = units;
|
|
851
|
-
},
|
|
852
|
-
async extend(ttlSeconds) {
|
|
853
|
-
await client.POST("/reservations/{reservationId}/extend", {
|
|
854
|
-
params: { path: { reservationId: id } },
|
|
855
|
-
body: ttlSeconds ? { ttlSeconds } : {},
|
|
856
|
-
signal: AbortSignal.timeout(timeoutMs)
|
|
857
|
-
});
|
|
858
|
-
}
|
|
859
|
-
};
|
|
860
|
-
let result;
|
|
1127
|
+
const slot = key(request.subject, request.action);
|
|
1128
|
+
const before = running.get(slot) ?? 0;
|
|
1129
|
+
running.set(slot, before + 1);
|
|
861
1130
|
try {
|
|
862
|
-
|
|
863
|
-
}
|
|
864
|
-
|
|
865
|
-
|
|
1131
|
+
return await reserveOnce(request, fn, body, idempotencyKey, before);
|
|
1132
|
+
} finally {
|
|
1133
|
+
const left2 = (running.get(slot) ?? 1) - 1;
|
|
1134
|
+
if (left2 > 0)
|
|
1135
|
+
running.set(slot, left2);
|
|
1136
|
+
else
|
|
1137
|
+
running.delete(slot);
|
|
866
1138
|
}
|
|
867
|
-
await settle(id, "commit", used);
|
|
868
|
-
return result;
|
|
869
1139
|
}
|
|
870
1140
|
};
|
|
1141
|
+
async function reserveOnce(request, fn, body, idempotencyKey, before) {
|
|
1142
|
+
const reserved = request.cost ?? 1;
|
|
1143
|
+
const decision = await check(body, idempotencyKey);
|
|
1144
|
+
const unmetered = {
|
|
1145
|
+
// Checked even unmetered: the mistake shows whether or not
|
|
1146
|
+
// Limitry answered.
|
|
1147
|
+
cost: (units) => void checkedCost(units),
|
|
1148
|
+
extend: async () => {
|
|
1149
|
+
}
|
|
1150
|
+
};
|
|
1151
|
+
if (decision === UNREACHABLE || decision === TIMED_OUT) {
|
|
1152
|
+
const unheard = () => decision === TIMED_OUT ? releaseUnheard(body, idempotencyKey) : Promise.resolve();
|
|
1153
|
+
const amount = concurrentAmounts.get(key(request.subject, request.action));
|
|
1154
|
+
if (failMode(request.subject, request.action) === "closed" || // Open, but never past a concurrent limit's amount here.
|
|
1155
|
+
amount !== void 0 && before >= amount) {
|
|
1156
|
+
await unheard();
|
|
1157
|
+
throw new Unavailable();
|
|
1158
|
+
}
|
|
1159
|
+
try {
|
|
1160
|
+
return await fn(unmetered);
|
|
1161
|
+
} finally {
|
|
1162
|
+
await unheard();
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
remember(request.subject, request.action, decision);
|
|
1166
|
+
if (!decision.allowed || !decision.reservation) {
|
|
1167
|
+
throw new LimitReached(decision);
|
|
1168
|
+
}
|
|
1169
|
+
const id = decision.reservation.id;
|
|
1170
|
+
let used = reserved;
|
|
1171
|
+
const reservation = {
|
|
1172
|
+
cost(units) {
|
|
1173
|
+
used = checkedCost(units);
|
|
1174
|
+
},
|
|
1175
|
+
async extend(ttlSeconds) {
|
|
1176
|
+
const { error, response } = await client.POST("/reservations/{reservationId}/extend", {
|
|
1177
|
+
params: { path: { reservationId: id } },
|
|
1178
|
+
body: ttlSeconds === void 0 ? {} : { ttlSeconds },
|
|
1179
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
1180
|
+
});
|
|
1181
|
+
if (error !== void 0)
|
|
1182
|
+
throw apiErrorFrom(error, response);
|
|
1183
|
+
}
|
|
1184
|
+
};
|
|
1185
|
+
let result;
|
|
1186
|
+
try {
|
|
1187
|
+
result = await fn(reservation);
|
|
1188
|
+
} catch (error) {
|
|
1189
|
+
await settle(id, "release");
|
|
1190
|
+
throw error;
|
|
1191
|
+
}
|
|
1192
|
+
await settle(id, "commit", used);
|
|
1193
|
+
return result;
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
function isRetryLater(response, error) {
|
|
1197
|
+
const code2 = error?.error?.code;
|
|
1198
|
+
return response.status === 503 && (code2 === "moving" || code2 === "busy");
|
|
871
1199
|
}
|
|
872
1200
|
|
|
873
1201
|
// node_modules/@limitry/sdk/dist/product/index.js
|
|
874
1202
|
var productMethods = (client) => {
|
|
875
1203
|
const limits = createLimits(client);
|
|
876
|
-
return {
|
|
1204
|
+
return {
|
|
1205
|
+
guard: limits.guard,
|
|
1206
|
+
reserve: limits.reserve,
|
|
1207
|
+
standing: limits.standing
|
|
1208
|
+
};
|
|
877
1209
|
};
|
|
1210
|
+
var productRetry = movingRetry();
|
|
878
1211
|
|
|
879
1212
|
// node_modules/@limitry/sdk/dist/resources.js
|
|
880
1213
|
async function call(client, op, pathParams, input, options = {}, content) {
|
|
@@ -925,20 +1258,20 @@ function resources(client) {
|
|
|
925
1258
|
/** Create a limit */
|
|
926
1259
|
(input, options) => call(client, { "method": "post", "path": "/limits", "query": [], "hasBody": true }, {}, input, options)
|
|
927
1260
|
),
|
|
928
|
-
|
|
929
|
-
/** Get a limit */
|
|
930
|
-
(limitId, options) => call(client, { "method": "get", "path": "/limits/{limitId}", "query": [], "hasBody": false }, { "limitId": limitId }, void 0, options)
|
|
931
|
-
),
|
|
932
|
-
grants: {
|
|
1261
|
+
credits: {
|
|
933
1262
|
create: (
|
|
934
1263
|
/** Add credits to a balance */
|
|
935
|
-
(limitId, input, options) => call(client, { "method": "post", "path": "/limits/{limitId}/
|
|
1264
|
+
(limitId, input, options) => call(client, { "method": "post", "path": "/limits/{limitId}/credits", "query": [], "hasBody": true }, { "limitId": limitId }, input, options)
|
|
936
1265
|
),
|
|
937
1266
|
list: (
|
|
938
|
-
/** List a balance's
|
|
939
|
-
(limitId, options) => call(client, { "method": "get", "path": "/limits/{limitId}/
|
|
1267
|
+
/** List a balance's credits */
|
|
1268
|
+
(limitId, options) => call(client, { "method": "get", "path": "/limits/{limitId}/credits", "query": [], "hasBody": false }, { "limitId": limitId }, void 0, options)
|
|
940
1269
|
)
|
|
941
1270
|
},
|
|
1271
|
+
get: (
|
|
1272
|
+
/** Get a limit */
|
|
1273
|
+
(limitId, options) => call(client, { "method": "get", "path": "/limits/{limitId}", "query": [], "hasBody": false }, { "limitId": limitId }, void 0, options)
|
|
1274
|
+
),
|
|
942
1275
|
list: (
|
|
943
1276
|
/** List limits */
|
|
944
1277
|
(input, options) => call(client, { "method": "get", "path": "/limits", "query": ["limit", "cursor", "subjectKind", "action", "archived"], "hasBody": false }, {}, input, options)
|
|
@@ -980,18 +1313,18 @@ function resources(client) {
|
|
|
980
1313
|
(subjectKind, subjectId, input, options) => call(client, { "method": "post", "path": "/subjects/{subjectKind}/{subjectId}/reset", "query": [], "hasBody": true }, { "subjectKind": subjectKind, "subjectId": subjectId }, input, options)
|
|
981
1314
|
),
|
|
982
1315
|
update: (
|
|
983
|
-
/** Set a subject's
|
|
1316
|
+
/** Set a subject's billing date */
|
|
984
1317
|
(subjectKind, subjectId, input, options) => call(client, { "method": "patch", "path": "/subjects/{subjectKind}/{subjectId}", "query": [], "hasBody": true }, { "subjectKind": subjectKind, "subjectId": subjectId }, input, options)
|
|
985
1318
|
)
|
|
986
1319
|
},
|
|
987
1320
|
usage: {
|
|
988
1321
|
query: (
|
|
989
1322
|
/** Get a limit's usage over time */
|
|
990
|
-
(input, options) => call(client, { "method": "get", "path": "/usage", "query": ["limitId", "
|
|
1323
|
+
(input, options) => call(client, { "method": "get", "path": "/usage", "query": ["limitId", "subject", "from", "to", "granularity"], "hasBody": false }, {}, input, options)
|
|
991
1324
|
),
|
|
992
1325
|
standing: (
|
|
993
1326
|
/** Get where a subject stands */
|
|
994
|
-
(input, options) => call(client, { "method": "get", "path": "/usage/standing", "query": ["
|
|
1327
|
+
(input, options) => call(client, { "method": "get", "path": "/usage/standing", "query": ["subject", "shared"], "hasBody": false }, {}, input, options)
|
|
995
1328
|
),
|
|
996
1329
|
subjects: (
|
|
997
1330
|
/** List who is using a limit now */
|
|
@@ -1101,98 +1434,16 @@ function resources(client) {
|
|
|
1101
1434
|
test: (
|
|
1102
1435
|
/** Send a test event */
|
|
1103
1436
|
(endpointId, options) => call(client, { "method": "post", "path": "/workspace/webhook-endpoints/{endpointId}/test", "query": [], "hasBody": false }, { "endpointId": endpointId }, void 0, options)
|
|
1437
|
+
),
|
|
1438
|
+
update: (
|
|
1439
|
+
/** Update a webhook endpoint */
|
|
1440
|
+
(endpointId, input, options) => call(client, { "method": "patch", "path": "/workspace/webhook-endpoints/{endpointId}", "query": [], "hasBody": true }, { "endpointId": endpointId }, input, options)
|
|
1104
1441
|
)
|
|
1105
1442
|
}
|
|
1106
1443
|
}
|
|
1107
1444
|
};
|
|
1108
1445
|
}
|
|
1109
1446
|
|
|
1110
|
-
// node_modules/@limitry/sdk/dist/retry.js
|
|
1111
|
-
var WRITES = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH", "DELETE"]);
|
|
1112
|
-
var RETRY_STATUSES = /* @__PURE__ */ new Set([408, 429, 500, 502, 503, 504]);
|
|
1113
|
-
var DEFAULT_MAX_RETRIES = 2;
|
|
1114
|
-
var MAX_WAIT_MS = 2e4;
|
|
1115
|
-
var MAX_BASE_KEY = 200;
|
|
1116
|
-
var wait = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
1117
|
-
function backoff(attempt) {
|
|
1118
|
-
const base = Math.min(500 * 2 ** attempt, 8e3);
|
|
1119
|
-
return base * (0.75 + Math.random() * 0.25);
|
|
1120
|
-
}
|
|
1121
|
-
function retryAfter(response) {
|
|
1122
|
-
const value = response.headers.get("retry-after");
|
|
1123
|
-
if (!value)
|
|
1124
|
-
return void 0;
|
|
1125
|
-
const seconds = Number(value);
|
|
1126
|
-
if (Number.isFinite(seconds))
|
|
1127
|
-
return Math.max(0, seconds * 1e3);
|
|
1128
|
-
const date = Date.parse(value);
|
|
1129
|
-
return Number.isNaN(date) ? void 0 : Math.max(0, date - Date.now());
|
|
1130
|
-
}
|
|
1131
|
-
async function retryable(response) {
|
|
1132
|
-
if (RETRY_STATUSES.has(response.status))
|
|
1133
|
-
return true;
|
|
1134
|
-
if (response.status !== 409)
|
|
1135
|
-
return false;
|
|
1136
|
-
try {
|
|
1137
|
-
const body = await response.clone().json();
|
|
1138
|
-
return body.error?.code === "idempotency_in_progress";
|
|
1139
|
-
} catch {
|
|
1140
|
-
return false;
|
|
1141
|
-
}
|
|
1142
|
-
}
|
|
1143
|
-
function withRetries(fetch2, options = {}) {
|
|
1144
|
-
const maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
1145
|
-
const sleep = options.sleep ?? wait;
|
|
1146
|
-
return async (request) => {
|
|
1147
|
-
if (WRITES.has(request.method) && !request.headers.has("idempotency-key")) {
|
|
1148
|
-
const url = new URL(request.url);
|
|
1149
|
-
const key = options.key ? await options.key(request.method, url.pathname) : crypto.randomUUID();
|
|
1150
|
-
const headers = new Headers(request.headers);
|
|
1151
|
-
headers.set("idempotency-key", key);
|
|
1152
|
-
request = new Request(request, { headers });
|
|
1153
|
-
}
|
|
1154
|
-
for (let attempt = 0; ; attempt++) {
|
|
1155
|
-
const last = attempt >= maxRetries;
|
|
1156
|
-
const sending = last ? request : request.clone();
|
|
1157
|
-
let response;
|
|
1158
|
-
try {
|
|
1159
|
-
response = await fetch2(sending);
|
|
1160
|
-
} catch (error) {
|
|
1161
|
-
if (last || request.signal.aborted)
|
|
1162
|
-
throw error;
|
|
1163
|
-
await sleep(backoff(attempt));
|
|
1164
|
-
continue;
|
|
1165
|
-
}
|
|
1166
|
-
if (last || !await retryable(response))
|
|
1167
|
-
return response;
|
|
1168
|
-
const delay = retryAfter(response) ?? backoff(attempt);
|
|
1169
|
-
if (delay > MAX_WAIT_MS)
|
|
1170
|
-
return response;
|
|
1171
|
-
await response.body?.cancel();
|
|
1172
|
-
await sleep(delay);
|
|
1173
|
-
}
|
|
1174
|
-
};
|
|
1175
|
-
}
|
|
1176
|
-
var hex = (bytes) => Array.from(new Uint8Array(bytes), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
1177
|
-
function keysFrom(base) {
|
|
1178
|
-
if (base !== void 0 && (base.length === 0 || base.length > MAX_BASE_KEY)) {
|
|
1179
|
-
throw new Error(`the idempotency key must be 1\u2013${MAX_BASE_KEY} characters`);
|
|
1180
|
-
}
|
|
1181
|
-
if (base !== void 0 && !/^[\x21-\x7e]+$/.test(base)) {
|
|
1182
|
-
throw new Error("the idempotency key must be printable ASCII with no spaces");
|
|
1183
|
-
}
|
|
1184
|
-
const seen = /* @__PURE__ */ new Map();
|
|
1185
|
-
return async (method, path2) => {
|
|
1186
|
-
if (base === void 0)
|
|
1187
|
-
return crypto.randomUUID();
|
|
1188
|
-
const target = `${method} ${path2}`;
|
|
1189
|
-
const n = (seen.get(target) ?? 0) + 1;
|
|
1190
|
-
seen.set(target, n);
|
|
1191
|
-
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(`${target}#${n}`));
|
|
1192
|
-
return `${base}:${hex(digest).slice(0, 16)}`;
|
|
1193
|
-
};
|
|
1194
|
-
}
|
|
1195
|
-
|
|
1196
1447
|
// node_modules/@limitry/sdk/dist/webhooks.js
|
|
1197
1448
|
var WebhookVerificationError = class extends Error {
|
|
1198
1449
|
constructor(message) {
|
|
@@ -1283,11 +1534,11 @@ function createBaseClient(options) {
|
|
|
1283
1534
|
if (!token) {
|
|
1284
1535
|
throw new Error(`${brand2.productName}: no API key. Pass { token } or set ${API_KEY_ENV}.`);
|
|
1285
1536
|
}
|
|
1286
|
-
const origin = (options.api ?? DEFAULT_API)
|
|
1537
|
+
const origin = withoutTrailingSlashes(options.api ?? DEFAULT_API);
|
|
1287
1538
|
const send = options.fetch ?? ((request) => fetch(request));
|
|
1288
1539
|
const client = createClient({
|
|
1289
1540
|
baseUrl: `${origin}/v1`,
|
|
1290
|
-
fetch: options.maxRetries === 0 ? send : withRetries(send, { maxRetries: options.maxRetries })
|
|
1541
|
+
fetch: options.maxRetries === 0 ? withoutProductRetryHeader(send) : withRetries(send, { maxRetries: options.maxRetries })
|
|
1291
1542
|
});
|
|
1292
1543
|
client.use({
|
|
1293
1544
|
onRequest({ request }) {
|
|
@@ -1315,6 +1566,12 @@ function unwrap(result) {
|
|
|
1315
1566
|
}
|
|
1316
1567
|
return result.data;
|
|
1317
1568
|
}
|
|
1569
|
+
function withoutTrailingSlashes(url) {
|
|
1570
|
+
let end = url.length;
|
|
1571
|
+
while (end > 0 && url[end - 1] === "/")
|
|
1572
|
+
end--;
|
|
1573
|
+
return url.slice(0, end);
|
|
1574
|
+
}
|
|
1318
1575
|
|
|
1319
1576
|
// node_modules/commander/lib/error.js
|
|
1320
1577
|
var CommanderError = class extends Error {
|
|
@@ -4685,12 +4942,35 @@ function useColor() {
|
|
|
4685
4942
|
var program = new Command();
|
|
4686
4943
|
|
|
4687
4944
|
// src/browser-login.ts
|
|
4688
|
-
import {
|
|
4945
|
+
import {
|
|
4946
|
+
constants,
|
|
4947
|
+
generateKeyPairSync,
|
|
4948
|
+
privateDecrypt,
|
|
4949
|
+
randomBytes
|
|
4950
|
+
} from "node:crypto";
|
|
4689
4951
|
import { createServer } from "node:http";
|
|
4690
4952
|
var PAGE = (title, body) => `<!doctype html><html><head><meta charset="utf-8"><title>${title}</title><meta name="viewport" content="width=device-width, initial-scale=1"></head><body style="font-family:system-ui,sans-serif;max-width:32rem;margin:4rem auto;padding:0 1rem"><h1 style="font-size:1.25rem">${title}</h1><p>${body}</p></body></html>`;
|
|
4691
|
-
var
|
|
4953
|
+
var MAX_URL = 16 * 1024;
|
|
4692
4954
|
function browserLogin(options) {
|
|
4693
4955
|
const state = randomBytes(24).toString("base64url");
|
|
4956
|
+
const { publicKey, privateKey } = generateKeyPairSync("rsa", {
|
|
4957
|
+
modulusLength: 2048
|
|
4958
|
+
});
|
|
4959
|
+
const key = publicKey.export({ type: "spki", format: "der" }).toString("base64url");
|
|
4960
|
+
const decrypt = (sealed) => {
|
|
4961
|
+
try {
|
|
4962
|
+
return privateDecrypt(
|
|
4963
|
+
{
|
|
4964
|
+
key: privateKey,
|
|
4965
|
+
padding: constants.RSA_PKCS1_OAEP_PADDING,
|
|
4966
|
+
oaepHash: "sha256"
|
|
4967
|
+
},
|
|
4968
|
+
Buffer.from(sealed, "base64url")
|
|
4969
|
+
).toString("utf8");
|
|
4970
|
+
} catch {
|
|
4971
|
+
return null;
|
|
4972
|
+
}
|
|
4973
|
+
};
|
|
4694
4974
|
return new Promise((resolve2, reject) => {
|
|
4695
4975
|
let settled = false;
|
|
4696
4976
|
const finish = (outcome) => {
|
|
@@ -4701,44 +4981,41 @@ function browserLogin(options) {
|
|
|
4701
4981
|
outcome();
|
|
4702
4982
|
};
|
|
4703
4983
|
const server = createServer((req, res) => {
|
|
4704
|
-
|
|
4984
|
+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
4985
|
+
if (req.method !== "GET" || url.pathname !== "/callback" || (req.url ?? "").length > MAX_URL) {
|
|
4705
4986
|
res.writeHead(404).end();
|
|
4706
4987
|
return;
|
|
4707
4988
|
}
|
|
4708
|
-
|
|
4709
|
-
|
|
4710
|
-
|
|
4711
|
-
|
|
4712
|
-
|
|
4713
|
-
});
|
|
4714
|
-
|
|
4715
|
-
const fields = new URLSearchParams(body);
|
|
4716
|
-
const send = (status, title, text) => res.writeHead(status, { "content-type": "text/html; charset=utf-8" }).end(PAGE(title, text));
|
|
4717
|
-
if (fields.get("state") !== state) {
|
|
4718
|
-
send(
|
|
4719
|
-
400,
|
|
4720
|
-
"Not this login",
|
|
4721
|
-
"This sign-in did not come from the terminal that is waiting. Run login again."
|
|
4722
|
-
);
|
|
4723
|
-
return;
|
|
4724
|
-
}
|
|
4725
|
-
if (fields.get("error")) {
|
|
4726
|
-
send(200, "Sign-in cancelled", "You can close this tab.");
|
|
4727
|
-
finish(() => reject(new Error("sign-in was denied in the browser")));
|
|
4728
|
-
return;
|
|
4729
|
-
}
|
|
4730
|
-
const token = fields.get("token");
|
|
4731
|
-
if (!token) {
|
|
4732
|
-
send(400, "Something went wrong", "Run login again.");
|
|
4733
|
-
return;
|
|
4734
|
-
}
|
|
4989
|
+
const fields = url.searchParams;
|
|
4990
|
+
const send = (status, title, text) => res.writeHead(status, {
|
|
4991
|
+
"content-type": "text/html; charset=utf-8",
|
|
4992
|
+
"cache-control": "no-store",
|
|
4993
|
+
"referrer-policy": "no-referrer"
|
|
4994
|
+
}).end(PAGE(title, text));
|
|
4995
|
+
if (fields.get("state") !== state) {
|
|
4735
4996
|
send(
|
|
4736
|
-
|
|
4737
|
-
"
|
|
4738
|
-
"
|
|
4997
|
+
400,
|
|
4998
|
+
"Not this login",
|
|
4999
|
+
"This sign-in did not come from the terminal that is waiting. Run login again."
|
|
4739
5000
|
);
|
|
4740
|
-
|
|
4741
|
-
}
|
|
5001
|
+
return;
|
|
5002
|
+
}
|
|
5003
|
+
if (fields.get("error")) {
|
|
5004
|
+
send(200, "Sign-in cancelled", "You can close this tab.");
|
|
5005
|
+
finish(() => reject(new Error("sign-in was denied in the browser")));
|
|
5006
|
+
return;
|
|
5007
|
+
}
|
|
5008
|
+
const token = decrypt(fields.get("token") ?? "");
|
|
5009
|
+
if (!token) {
|
|
5010
|
+
send(400, "Something went wrong", "Run login again.");
|
|
5011
|
+
return;
|
|
5012
|
+
}
|
|
5013
|
+
send(
|
|
5014
|
+
200,
|
|
5015
|
+
"You are signed in",
|
|
5016
|
+
"Return to your terminal \u2014 you can close this tab."
|
|
5017
|
+
);
|
|
5018
|
+
finish(() => resolve2(token));
|
|
4742
5019
|
});
|
|
4743
5020
|
const timer = setTimeout(
|
|
4744
5021
|
() => finish(
|
|
@@ -4755,6 +5032,7 @@ function browserLogin(options) {
|
|
|
4755
5032
|
const url = `${options.webOrigin}/cli/authorize?${new URLSearchParams({
|
|
4756
5033
|
port: String(port),
|
|
4757
5034
|
state,
|
|
5035
|
+
key,
|
|
4758
5036
|
name: options.computer
|
|
4759
5037
|
})}`;
|
|
4760
5038
|
options.onUrl(url);
|
|
@@ -4768,8 +5046,8 @@ function webOriginFor(api2) {
|
|
|
4768
5046
|
url.hostname = `app.${url.hostname.slice(4)}`;
|
|
4769
5047
|
return url.origin;
|
|
4770
5048
|
}
|
|
4771
|
-
if ((url.hostname === "localhost" || url.hostname === "127.0.0.1") && url.port
|
|
4772
|
-
url.port =
|
|
5049
|
+
if ((url.hostname === "localhost" || url.hostname === "127.0.0.1") && Number(url.port) >= 8787 && Number(url.port) % 100 === 87) {
|
|
5050
|
+
url.port = String(Number(url.port) - 5787);
|
|
4773
5051
|
return url.origin;
|
|
4774
5052
|
}
|
|
4775
5053
|
return null;
|
|
@@ -4814,11 +5092,18 @@ function resolveSettings(env, homeDir, flags = {}) {
|
|
|
4814
5092
|
const environment2 = flags.environment ? { environment: flags.environment, environmentSource: "flag" } : env[environmentEnvVar()] ? { environment: env[environmentEnvVar()], environmentSource: "env" } : config2.environment ? { environment: config2.environment, environmentSource: "config" } : { environment: void 0, environmentSource: null };
|
|
4815
5093
|
return {
|
|
4816
5094
|
...environment2,
|
|
4817
|
-
api: (
|
|
5095
|
+
api: withoutTrailingSlashes2(
|
|
5096
|
+
env[`${brand.identifier.toUpperCase()}_API`] ?? config2.api ?? DEFAULT_API
|
|
5097
|
+
),
|
|
4818
5098
|
token: env[tokenEnvVar()] ?? config2.token,
|
|
4819
5099
|
workspace: env[`${brand.identifier.toUpperCase()}_WORKSPACE`] ?? config2.workspace
|
|
4820
5100
|
};
|
|
4821
5101
|
}
|
|
5102
|
+
function withoutTrailingSlashes2(url) {
|
|
5103
|
+
let end = url.length;
|
|
5104
|
+
while (end > 0 && url[end - 1] === "/") end--;
|
|
5105
|
+
return url.slice(0, end);
|
|
5106
|
+
}
|
|
4822
5107
|
|
|
4823
5108
|
// src/http.ts
|
|
4824
5109
|
async function rawRequest(ctx, options) {
|
|
@@ -4974,7 +5259,7 @@ var login = (program2, ctx) => {
|
|
|
4974
5259
|
}
|
|
4975
5260
|
if (!ctx.io.interactive) {
|
|
4976
5261
|
ctx.io.stderr(
|
|
4977
|
-
`Paste a personal access token (Account \u2192
|
|
5262
|
+
`Paste a personal access token (Account \u2192 Personal access tokens in ${brand.productName}), then Enter:`
|
|
4978
5263
|
);
|
|
4979
5264
|
await saveToken(ctx, (await ctx.io.stdin()).trim(), options.api);
|
|
4980
5265
|
return;
|
|
@@ -5010,7 +5295,7 @@ var logout = (program2, ctx) => {
|
|
|
5010
5295
|
).catch(() => null);
|
|
5011
5296
|
if (revoked && !revoked.ok && revoked.status !== 401) {
|
|
5012
5297
|
ctx.io.stderr(
|
|
5013
|
-
`Could not revoke the token (HTTP ${revoked.status}) \u2014 revoke it under Account \u2192
|
|
5298
|
+
`Could not revoke the token (HTTP ${revoked.status}) \u2014 revoke it under Account \u2192 Personal access tokens.`
|
|
5014
5299
|
);
|
|
5015
5300
|
}
|
|
5016
5301
|
}
|
|
@@ -5167,8 +5452,11 @@ function operationRequest(op, input, body = {}) {
|
|
|
5167
5452
|
json2[field.name] = value;
|
|
5168
5453
|
}
|
|
5169
5454
|
}
|
|
5170
|
-
const
|
|
5171
|
-
|
|
5455
|
+
const open = path2.indexOf("{");
|
|
5456
|
+
const close = open === -1 ? -1 : path2.indexOf("}", open + 1);
|
|
5457
|
+
if (close > open + 1) {
|
|
5458
|
+
throw new Error(`missing ${path2.slice(open + 1, close)}`);
|
|
5459
|
+
}
|
|
5172
5460
|
const qs = query.toString();
|
|
5173
5461
|
return {
|
|
5174
5462
|
method: op.method.toUpperCase(),
|
|
@@ -5199,7 +5487,7 @@ var OPERATIONS = [
|
|
|
5199
5487
|
"idempotentHint": false
|
|
5200
5488
|
}
|
|
5201
5489
|
},
|
|
5202
|
-
"description": "Asks whether `subject` may do `action` at `cost`, given every enabled limit that applies, and \u2014 in `consume` mode \u2014 uses it. Always 200 for a decision: read `allowed`; `
|
|
5490
|
+
"description": "Asks whether `subject` may do `action` at `cost`, given every enabled limit that applies, and \u2014 in `consume` mode \u2014 uses it. Always 200 for a decision, allowed or refused: read `allowed`; `limits` shows what is left on each limit and `retryAfterSeconds` when to try again. An `Idempotency-Key` header makes a retried check return the same decision without using twice. Its API-key check is cached briefly for speed: a revoked key can keep working here for up to 60 seconds.",
|
|
5203
5491
|
"fields": [
|
|
5204
5492
|
{
|
|
5205
5493
|
"name": "subject",
|
|
@@ -5304,7 +5592,7 @@ var OPERATIONS = [
|
|
|
5304
5592
|
"in": "body",
|
|
5305
5593
|
"type": "string",
|
|
5306
5594
|
"required": false,
|
|
5307
|
-
"description": "`consume` (default) decides and uses; `preview` decides without using; `reserve` decides and
|
|
5595
|
+
"description": "`consume` (default) decides and uses; `preview` decides without using; `reserve` decides and reserves `cost` (what it will probably use) until you commit the actual amount or release it \u2014 see `reservation` in the answer. Only a reservation takes a slot of a `concurrent` limit.",
|
|
5308
5596
|
"example": "consume",
|
|
5309
5597
|
"schema": {
|
|
5310
5598
|
"type": "string",
|
|
@@ -5313,7 +5601,7 @@ var OPERATIONS = [
|
|
|
5313
5601
|
"preview",
|
|
5314
5602
|
"reserve"
|
|
5315
5603
|
],
|
|
5316
|
-
"description": "`consume` (default) decides and uses; `preview` decides without using; `reserve` decides and
|
|
5604
|
+
"description": "`consume` (default) decides and uses; `preview` decides without using; `reserve` decides and reserves `cost` (what it will probably use) until you commit the actual amount or release it \u2014 see `reservation` in the answer. Only a reservation takes a slot of a `concurrent` limit."
|
|
5317
5605
|
}
|
|
5318
5606
|
},
|
|
5319
5607
|
{
|
|
@@ -5321,13 +5609,13 @@ var OPERATIONS = [
|
|
|
5321
5609
|
"in": "body",
|
|
5322
5610
|
"type": "number",
|
|
5323
5611
|
"required": false,
|
|
5324
|
-
"description": "With `reserve`: how long the
|
|
5612
|
+
"description": "With `reserve`: how long the reservation lasts before it releases itself. Default 300, at most 3600.",
|
|
5325
5613
|
"example": 300,
|
|
5326
5614
|
"schema": {
|
|
5327
5615
|
"type": "integer",
|
|
5328
5616
|
"minimum": 1,
|
|
5329
5617
|
"maximum": 3600,
|
|
5330
|
-
"description": "With `reserve`: how long the
|
|
5618
|
+
"description": "With `reserve`: how long the reservation lasts before it releases itself. Default 300, at most 3600."
|
|
5331
5619
|
}
|
|
5332
5620
|
}
|
|
5333
5621
|
],
|
|
@@ -5356,14 +5644,14 @@ var OPERATIONS = [
|
|
|
5356
5644
|
"idempotentHint": false
|
|
5357
5645
|
}
|
|
5358
5646
|
},
|
|
5359
|
-
"description": "Stops a limit: checks stop enforcing it
|
|
5647
|
+
"description": "Stops a limit: checks stop enforcing it at once, and it leaves the list. It is archived, not erased \u2014 its usage history and credits stay, and `POST /v1/limits/{limitId}/restore` brings it back. Owners and admins only, for a person's token.",
|
|
5360
5648
|
"fields": [
|
|
5361
5649
|
{
|
|
5362
5650
|
"name": "limitId",
|
|
5363
5651
|
"in": "path",
|
|
5364
5652
|
"type": "string",
|
|
5365
5653
|
"required": true,
|
|
5366
|
-
"example": "
|
|
5654
|
+
"example": "lmt_3f0c\u2026",
|
|
5367
5655
|
"schema": {
|
|
5368
5656
|
"type": "string"
|
|
5369
5657
|
}
|
|
@@ -5392,64 +5680,68 @@ var OPERATIONS = [
|
|
|
5392
5680
|
"idempotentHint": false
|
|
5393
5681
|
}
|
|
5394
5682
|
},
|
|
5395
|
-
"description": "Limits how much
|
|
5683
|
+
"description": "Limits how much a subject may use per window \u2014 for example 200 cents a month for every `user`. Only `subject.kind`, `amount` and `window` are needed: `action` defaults to every action (`*`), `unit` to units, and `name` to the sentence the limit reads as. Checks enforce it from then on. What it counts (subject, action, unit, window, every, start, rolling) is fixed once created. Owners and admins only, for a person's token.",
|
|
5396
5684
|
"fields": [
|
|
5397
5685
|
{
|
|
5398
5686
|
"name": "name",
|
|
5399
5687
|
"in": "body",
|
|
5400
5688
|
"type": "string",
|
|
5401
|
-
"required":
|
|
5402
|
-
"description":
|
|
5689
|
+
"required": false,
|
|
5690
|
+
"description": `A name, unique among the environment's active limits. Default: the sentence the limit reads as, such as "200 cents per user per month". If a limit already has that name, the answer is 409: give this one a name.`,
|
|
5403
5691
|
"example": "AI budget",
|
|
5404
5692
|
"schema": {
|
|
5405
5693
|
"type": "string",
|
|
5406
5694
|
"minLength": 1,
|
|
5407
5695
|
"maxLength": 100,
|
|
5408
|
-
"description":
|
|
5696
|
+
"description": `A name, unique among the environment's active limits. Default: the sentence the limit reads as, such as "200 cents per user per month". If a limit already has that name, the answer is 409: give this one a name.`
|
|
5409
5697
|
}
|
|
5410
5698
|
},
|
|
5411
5699
|
{
|
|
5412
|
-
"name": "
|
|
5700
|
+
"name": "subject",
|
|
5413
5701
|
"in": "body",
|
|
5414
|
-
"type": "
|
|
5702
|
+
"type": "json",
|
|
5415
5703
|
"required": true,
|
|
5416
|
-
"description": "
|
|
5417
|
-
"example":
|
|
5418
|
-
|
|
5419
|
-
|
|
5420
|
-
"minLength": 1,
|
|
5421
|
-
"maxLength": 100,
|
|
5422
|
-
"description": "Your own label for who is checked."
|
|
5423
|
-
}
|
|
5424
|
-
},
|
|
5425
|
-
{
|
|
5426
|
-
"name": "subjectId",
|
|
5427
|
-
"in": "body",
|
|
5428
|
-
"type": "string",
|
|
5429
|
-
"required": false,
|
|
5430
|
-
"description": "One subject; omit or null for every subject of the kind. A subject's own limit replaces the kind's limits with the same action and window.",
|
|
5704
|
+
"description": 'Who it applies to: `{"kind": "user"}` for every user, `{"kind": "user", "id": "usr_123"}` for one.',
|
|
5705
|
+
"example": {
|
|
5706
|
+
"kind": "user"
|
|
5707
|
+
},
|
|
5431
5708
|
"schema": {
|
|
5432
|
-
"type":
|
|
5433
|
-
|
|
5434
|
-
"
|
|
5709
|
+
"type": "object",
|
|
5710
|
+
"properties": {
|
|
5711
|
+
"kind": {
|
|
5712
|
+
"type": "string",
|
|
5713
|
+
"minLength": 1,
|
|
5714
|
+
"maxLength": 100,
|
|
5715
|
+
"description": "Your own label for who is checked: user, team, apikey \u2026"
|
|
5716
|
+
},
|
|
5717
|
+
"id": {
|
|
5718
|
+
"type": [
|
|
5719
|
+
"string",
|
|
5720
|
+
"null"
|
|
5721
|
+
],
|
|
5722
|
+
"minLength": 1,
|
|
5723
|
+
"maxLength": 100,
|
|
5724
|
+
"description": "One subject; omit or null for every subject of the kind. A subject's own limit replaces the kind's limits with the same action and window."
|
|
5725
|
+
}
|
|
5726
|
+
},
|
|
5727
|
+
"required": [
|
|
5728
|
+
"kind"
|
|
5435
5729
|
],
|
|
5436
|
-
"
|
|
5437
|
-
"maxLength": 100,
|
|
5438
|
-
"description": "One subject; omit or null for every subject of the kind. A subject's own limit replaces the kind's limits with the same action and window."
|
|
5730
|
+
"description": 'Who it applies to: `{"kind": "user"}` for every user, `{"kind": "user", "id": "usr_123"}` for one.'
|
|
5439
5731
|
}
|
|
5440
5732
|
},
|
|
5441
5733
|
{
|
|
5442
5734
|
"name": "action",
|
|
5443
5735
|
"in": "body",
|
|
5444
5736
|
"type": "string",
|
|
5445
|
-
"required":
|
|
5446
|
-
"description": "The action to limit
|
|
5737
|
+
"required": false,
|
|
5738
|
+
"description": "The action to limit. Default: `*`, every action.",
|
|
5447
5739
|
"example": "ai-call",
|
|
5448
5740
|
"schema": {
|
|
5449
5741
|
"type": "string",
|
|
5450
5742
|
"minLength": 1,
|
|
5451
5743
|
"maxLength": 100,
|
|
5452
|
-
"description": "The action to limit
|
|
5744
|
+
"description": "The action to limit. Default: `*`, every action."
|
|
5453
5745
|
}
|
|
5454
5746
|
},
|
|
5455
5747
|
{
|
|
@@ -5471,13 +5763,13 @@ var OPERATIONS = [
|
|
|
5471
5763
|
"in": "body",
|
|
5472
5764
|
"type": "string",
|
|
5473
5765
|
"required": false,
|
|
5474
|
-
"description": "What the amount counts
|
|
5766
|
+
"description": "What the amount counts, in words. Default: units.",
|
|
5475
5767
|
"example": "cents",
|
|
5476
5768
|
"schema": {
|
|
5477
5769
|
"type": "string",
|
|
5478
5770
|
"minLength": 1,
|
|
5479
5771
|
"maxLength": 40,
|
|
5480
|
-
"description": "What the amount counts
|
|
5772
|
+
"description": "What the amount counts, in words. Default: units."
|
|
5481
5773
|
}
|
|
5482
5774
|
},
|
|
5483
5775
|
{
|
|
@@ -5485,7 +5777,7 @@ var OPERATIONS = [
|
|
|
5485
5777
|
"in": "body",
|
|
5486
5778
|
"type": "string",
|
|
5487
5779
|
"required": true,
|
|
5488
|
-
"description": "`minute`, `hour`, `day`, `week`, `month` or `year`: a period of `every` of them,
|
|
5780
|
+
"description": "`minute`, `hour`, `day`, `week`, `month` or `year`: a period of `every` of them, reset where `start` says (or rolling: see `rolling`). `total`: a balance that never resets, raised by credits. `concurrent`: at most `amount` open reservations at once.",
|
|
5489
5781
|
"example": "month",
|
|
5490
5782
|
"schema": {
|
|
5491
5783
|
"type": "string",
|
|
@@ -5499,7 +5791,7 @@ var OPERATIONS = [
|
|
|
5499
5791
|
"total",
|
|
5500
5792
|
"concurrent"
|
|
5501
5793
|
],
|
|
5502
|
-
"description": "`minute`, `hour`, `day`, `week`, `month` or `year`: a period of `every` of them,
|
|
5794
|
+
"description": "`minute`, `hour`, `day`, `week`, `month` or `year`: a period of `every` of them, reset where `start` says (or rolling: see `rolling`). `total`: a balance that never resets, raised by credits. `concurrent`: at most `amount` open reservations at once."
|
|
5503
5795
|
}
|
|
5504
5796
|
},
|
|
5505
5797
|
{
|
|
@@ -5521,16 +5813,16 @@ var OPERATIONS = [
|
|
|
5521
5813
|
"in": "body",
|
|
5522
5814
|
"type": "string",
|
|
5523
5815
|
"required": false,
|
|
5524
|
-
"description": "Where a period begins. `calendar`: UTC boundaries (midnight, Monday, the 1st, 1 January). `
|
|
5816
|
+
"description": "Where a period begins. `calendar`: UTC boundaries (midnight, Monday, the 1st, 1 January). `billing_date`: from the subject's billing date, set with `subjects.update` (its first check until then), so an annual plan can reset monthly from its start day. `first_use`: at the first check after the previous period ended. Default: calendar.",
|
|
5525
5817
|
"example": "calendar",
|
|
5526
5818
|
"schema": {
|
|
5527
5819
|
"type": "string",
|
|
5528
5820
|
"enum": [
|
|
5529
5821
|
"calendar",
|
|
5530
|
-
"
|
|
5822
|
+
"billing_date",
|
|
5531
5823
|
"first_use"
|
|
5532
5824
|
],
|
|
5533
|
-
"description": "Where a period begins. `calendar`: UTC boundaries (midnight, Monday, the 1st, 1 January). `
|
|
5825
|
+
"description": "Where a period begins. `calendar`: UTC boundaries (midnight, Monday, the 1st, 1 January). `billing_date`: from the subject's billing date, set with `subjects.update` (its first check until then), so an annual plan can reset monthly from its start day. `first_use`: at the first check after the previous period ended. Default: calendar."
|
|
5534
5826
|
}
|
|
5535
5827
|
},
|
|
5536
5828
|
{
|
|
@@ -5576,50 +5868,12 @@ var OPERATIONS = [
|
|
|
5576
5868
|
"scope": "limits:write"
|
|
5577
5869
|
},
|
|
5578
5870
|
{
|
|
5579
|
-
"operationId": "limits.
|
|
5580
|
-
"method": "get",
|
|
5581
|
-
"path": "/limits/{limitId}",
|
|
5582
|
-
"summary": "Get a limit",
|
|
5583
|
-
"cli": {
|
|
5584
|
-
"command": "limits get",
|
|
5585
|
-
"args": [
|
|
5586
|
-
"limitId"
|
|
5587
|
-
],
|
|
5588
|
-
"confirm": false
|
|
5589
|
-
},
|
|
5590
|
-
"mcp": {
|
|
5591
|
-
"exposed": true,
|
|
5592
|
-
"name": "limits_get",
|
|
5593
|
-
"hints": {
|
|
5594
|
-
"readOnlyHint": true,
|
|
5595
|
-
"destructiveHint": false,
|
|
5596
|
-
"idempotentHint": true
|
|
5597
|
-
}
|
|
5598
|
-
},
|
|
5599
|
-
"description": "One limit: who it applies to, the action, amount and window.",
|
|
5600
|
-
"fields": [
|
|
5601
|
-
{
|
|
5602
|
-
"name": "limitId",
|
|
5603
|
-
"in": "path",
|
|
5604
|
-
"type": "string",
|
|
5605
|
-
"required": true,
|
|
5606
|
-
"example": "lim_3f0c\u2026",
|
|
5607
|
-
"schema": {
|
|
5608
|
-
"type": "string"
|
|
5609
|
-
}
|
|
5610
|
-
}
|
|
5611
|
-
],
|
|
5612
|
-
"hasBody": false,
|
|
5613
|
-
"workspace": true,
|
|
5614
|
-
"scope": "limits:read"
|
|
5615
|
-
},
|
|
5616
|
-
{
|
|
5617
|
-
"operationId": "limits.grants.create",
|
|
5871
|
+
"operationId": "limits.credits.create",
|
|
5618
5872
|
"method": "post",
|
|
5619
|
-
"path": "/limits/{limitId}/
|
|
5873
|
+
"path": "/limits/{limitId}/credits",
|
|
5620
5874
|
"summary": "Add credits to a balance",
|
|
5621
5875
|
"cli": {
|
|
5622
|
-
"command": "limits
|
|
5876
|
+
"command": "limits credits create",
|
|
5623
5877
|
"args": [
|
|
5624
5878
|
"limitId"
|
|
5625
5879
|
],
|
|
@@ -5627,16 +5881,16 @@ var OPERATIONS = [
|
|
|
5627
5881
|
},
|
|
5628
5882
|
"mcp": {
|
|
5629
5883
|
"exposed": false,
|
|
5630
|
-
"reason": "Adding credits gives value away; an agent asks a person (limit.
|
|
5884
|
+
"reason": "Adding credits gives value away; an agent asks a person (limit.credit approval)"
|
|
5631
5885
|
},
|
|
5632
|
-
"description": "Adds `amount` to one subject's balance \u2014 a limit whose window is `total`. A negative `amount` takes credits back (a refund, a chargeback) and needs a `note` saying why; the balance may go below zero, and checks are refused until
|
|
5886
|
+
"description": "Adds `amount` credits to one subject's balance \u2014 a limit whose window is `total`. A negative `amount` takes credits back (a refund, a chargeback) and needs a `note` saying why; the balance may go below zero, and checks are refused until more credits are added. Owners and admins, or a workspace API key; an agent asks a person (the `limit.credit` approval).",
|
|
5633
5887
|
"fields": [
|
|
5634
5888
|
{
|
|
5635
5889
|
"name": "limitId",
|
|
5636
5890
|
"in": "path",
|
|
5637
5891
|
"type": "string",
|
|
5638
5892
|
"required": true,
|
|
5639
|
-
"example": "
|
|
5893
|
+
"example": "lmt_3f0c\u2026",
|
|
5640
5894
|
"schema": {
|
|
5641
5895
|
"type": "string"
|
|
5642
5896
|
}
|
|
@@ -5690,7 +5944,7 @@ var OPERATIONS = [
|
|
|
5690
5944
|
"type": "string",
|
|
5691
5945
|
"required": false,
|
|
5692
5946
|
"description": "Required when `amount` is negative.",
|
|
5693
|
-
"example": "October
|
|
5947
|
+
"example": "October credits",
|
|
5694
5948
|
"schema": {
|
|
5695
5949
|
"type": "string",
|
|
5696
5950
|
"maxLength": 200,
|
|
@@ -5703,12 +5957,50 @@ var OPERATIONS = [
|
|
|
5703
5957
|
"scope": "limits:write"
|
|
5704
5958
|
},
|
|
5705
5959
|
{
|
|
5706
|
-
"operationId": "limits.
|
|
5960
|
+
"operationId": "limits.credits.list",
|
|
5961
|
+
"method": "get",
|
|
5962
|
+
"path": "/limits/{limitId}/credits",
|
|
5963
|
+
"summary": "List a balance's credits",
|
|
5964
|
+
"cli": {
|
|
5965
|
+
"command": "limits credits list",
|
|
5966
|
+
"args": [
|
|
5967
|
+
"limitId"
|
|
5968
|
+
],
|
|
5969
|
+
"confirm": false
|
|
5970
|
+
},
|
|
5971
|
+
"mcp": {
|
|
5972
|
+
"exposed": true,
|
|
5973
|
+
"name": "limits_credits_list",
|
|
5974
|
+
"hints": {
|
|
5975
|
+
"readOnlyHint": true,
|
|
5976
|
+
"destructiveHint": false,
|
|
5977
|
+
"idempotentHint": true
|
|
5978
|
+
}
|
|
5979
|
+
},
|
|
5980
|
+
"description": "Credits added to (or taken back from) subjects' balances for this limit, newest first.",
|
|
5981
|
+
"fields": [
|
|
5982
|
+
{
|
|
5983
|
+
"name": "limitId",
|
|
5984
|
+
"in": "path",
|
|
5985
|
+
"type": "string",
|
|
5986
|
+
"required": true,
|
|
5987
|
+
"example": "lmt_3f0c\u2026",
|
|
5988
|
+
"schema": {
|
|
5989
|
+
"type": "string"
|
|
5990
|
+
}
|
|
5991
|
+
}
|
|
5992
|
+
],
|
|
5993
|
+
"hasBody": false,
|
|
5994
|
+
"workspace": true,
|
|
5995
|
+
"scope": "limits:read"
|
|
5996
|
+
},
|
|
5997
|
+
{
|
|
5998
|
+
"operationId": "limits.get",
|
|
5707
5999
|
"method": "get",
|
|
5708
|
-
"path": "/limits/{limitId}
|
|
5709
|
-
"summary": "
|
|
6000
|
+
"path": "/limits/{limitId}",
|
|
6001
|
+
"summary": "Get a limit",
|
|
5710
6002
|
"cli": {
|
|
5711
|
-
"command": "limits
|
|
6003
|
+
"command": "limits get",
|
|
5712
6004
|
"args": [
|
|
5713
6005
|
"limitId"
|
|
5714
6006
|
],
|
|
@@ -5716,21 +6008,21 @@ var OPERATIONS = [
|
|
|
5716
6008
|
},
|
|
5717
6009
|
"mcp": {
|
|
5718
6010
|
"exposed": true,
|
|
5719
|
-
"name": "
|
|
6011
|
+
"name": "limits_get",
|
|
5720
6012
|
"hints": {
|
|
5721
6013
|
"readOnlyHint": true,
|
|
5722
6014
|
"destructiveHint": false,
|
|
5723
6015
|
"idempotentHint": true
|
|
5724
6016
|
}
|
|
5725
6017
|
},
|
|
5726
|
-
"description": "
|
|
6018
|
+
"description": "One limit: who it applies to, the action, amount and window.",
|
|
5727
6019
|
"fields": [
|
|
5728
6020
|
{
|
|
5729
6021
|
"name": "limitId",
|
|
5730
6022
|
"in": "path",
|
|
5731
6023
|
"type": "string",
|
|
5732
6024
|
"required": true,
|
|
5733
|
-
"example": "
|
|
6025
|
+
"example": "lmt_3f0c\u2026",
|
|
5734
6026
|
"schema": {
|
|
5735
6027
|
"type": "string"
|
|
5736
6028
|
}
|
|
@@ -5861,7 +6153,7 @@ var OPERATIONS = [
|
|
|
5861
6153
|
"in": "path",
|
|
5862
6154
|
"type": "string",
|
|
5863
6155
|
"required": true,
|
|
5864
|
-
"example": "
|
|
6156
|
+
"example": "lmt_3f0c\u2026",
|
|
5865
6157
|
"schema": {
|
|
5866
6158
|
"type": "string"
|
|
5867
6159
|
}
|
|
@@ -5891,18 +6183,20 @@ var OPERATIONS = [
|
|
|
5891
6183
|
"idempotentHint": false
|
|
5892
6184
|
}
|
|
5893
6185
|
},
|
|
5894
|
-
"description": "Makes the environment's limits match `limits`, by name: creates the missing, changes those that differ (an omitted field means its default) and, with `archiveMissing`, archives active limits the list doesn't name. Running it twice changes nothing the second time. Limitry checks the whole list first, and a problem anywhere changes nothing (400, each problem in `details`); then it writes every change at once. `
|
|
6186
|
+
"description": "Makes the environment's limits match `limits`, by name: creates the missing, changes those that differ (an omitted field means its default) and, with `archiveMissing`, archives active limits the list doesn't name. Running it twice changes nothing the second time. Limitry checks the whole list first, and a problem anywhere changes nothing (400, each problem in `details`); then it writes every change at once. What a limit counts is fixed: a different subject, action, unit or window under an existing name is refused until the entry is renamed. `preview` answers what it would do without writing. For keeping limits in a file in your repository (`limitry sync`). Owners and admins only, for a person's token.",
|
|
5895
6187
|
"fields": [
|
|
5896
6188
|
{
|
|
5897
6189
|
"name": "limits",
|
|
5898
6190
|
"in": "body",
|
|
5899
6191
|
"type": "json",
|
|
5900
6192
|
"required": true,
|
|
5901
|
-
"description": "Every limit you want, each described whole: an omitted field is its default, as on create. Names are the key and must be unique in the list. At most 200.",
|
|
6193
|
+
"description": "Every limit you want, each described whole: an omitted field is its default, as on create. Names are the key and must be unique in the list. What a limit counts (subject, action, unit, window, every, start, rolling) is fixed: to change it, rename the entry, which creates a new limit. At most 200.",
|
|
5902
6194
|
"example": [
|
|
5903
6195
|
{
|
|
5904
6196
|
"name": "AI budget",
|
|
5905
|
-
"
|
|
6197
|
+
"subject": {
|
|
6198
|
+
"kind": "user"
|
|
6199
|
+
},
|
|
5906
6200
|
"action": "ai-call",
|
|
5907
6201
|
"amount": 200,
|
|
5908
6202
|
"unit": "cents",
|
|
@@ -5912,103 +6206,29 @@ var OPERATIONS = [
|
|
|
5912
6206
|
"schema": {
|
|
5913
6207
|
"type": "array",
|
|
5914
6208
|
"items": {
|
|
5915
|
-
"
|
|
5916
|
-
|
|
5917
|
-
|
|
5918
|
-
"type": "string",
|
|
5919
|
-
"minLength": 1,
|
|
5920
|
-
"maxLength": 100,
|
|
5921
|
-
"description": "A name for the limit, unique among its environment's active limits."
|
|
6209
|
+
"allOf": [
|
|
6210
|
+
{
|
|
6211
|
+
"$ref": "#/components/schemas/LimitInput"
|
|
5922
6212
|
},
|
|
5923
|
-
|
|
5924
|
-
"type": "
|
|
5925
|
-
"
|
|
5926
|
-
|
|
5927
|
-
|
|
5928
|
-
|
|
5929
|
-
|
|
5930
|
-
|
|
5931
|
-
|
|
5932
|
-
|
|
5933
|
-
|
|
5934
|
-
"
|
|
5935
|
-
|
|
5936
|
-
|
|
5937
|
-
},
|
|
5938
|
-
"action": {
|
|
5939
|
-
"type": "string",
|
|
5940
|
-
"minLength": 1,
|
|
5941
|
-
"maxLength": 100,
|
|
5942
|
-
"description": "The action to limit; `*` for every action."
|
|
5943
|
-
},
|
|
5944
|
-
"amount": {
|
|
5945
|
-
"type": "integer",
|
|
5946
|
-
"minimum": 1,
|
|
5947
|
-
"maximum": 1e9,
|
|
5948
|
-
"description": "How many units each subject may use per window."
|
|
5949
|
-
},
|
|
5950
|
-
"unit": {
|
|
5951
|
-
"type": "string",
|
|
5952
|
-
"minLength": 1,
|
|
5953
|
-
"maxLength": 40,
|
|
5954
|
-
"description": "What the amount counts (display only). Default: units."
|
|
5955
|
-
},
|
|
5956
|
-
"window": {
|
|
5957
|
-
"type": "string",
|
|
5958
|
-
"enum": [
|
|
5959
|
-
"minute",
|
|
5960
|
-
"hour",
|
|
5961
|
-
"day",
|
|
5962
|
-
"week",
|
|
5963
|
-
"month",
|
|
5964
|
-
"year",
|
|
5965
|
-
"total",
|
|
5966
|
-
"concurrent"
|
|
5967
|
-
],
|
|
5968
|
-
"description": "`minute`, `hour`, `day`, `week`, `month` or `year`: a period of `every` of them, refilled where `start` says (or rolling: see `rolling`). `total`: a balance that never resets, topped up by grants. `concurrent`: at most `amount` held reservations at once."
|
|
5969
|
-
},
|
|
5970
|
-
"every": {
|
|
5971
|
-
"type": "integer",
|
|
5972
|
-
"minimum": 1,
|
|
5973
|
-
"maximum": 1e3,
|
|
5974
|
-
"description": 'How many `window` units a period lasts: `window: "month", every: 3` is a quarter, `window: "hour", every: 5` five hours. Default: 1.'
|
|
5975
|
-
},
|
|
5976
|
-
"start": {
|
|
5977
|
-
"type": "string",
|
|
5978
|
-
"enum": [
|
|
5979
|
-
"calendar",
|
|
5980
|
-
"anchor",
|
|
5981
|
-
"first_use"
|
|
5982
|
-
],
|
|
5983
|
-
"description": "Where a period begins. `calendar`: UTC boundaries (midnight, Monday, the 1st, 1 January). `anchor`: from the subject's anchor, its billing date, set with `subjects.update` (its first check until then), so an annual plan can refill monthly from its start day. `first_use`: at the first check after the previous period ended. Default: calendar."
|
|
5984
|
-
},
|
|
5985
|
-
"rolling": {
|
|
5986
|
-
"type": "boolean",
|
|
5987
|
-
"description": "Count the last window instead of the calendar one (minute, hour or day only). Default: false."
|
|
5988
|
-
},
|
|
5989
|
-
"enabled": {
|
|
5990
|
-
"type": "boolean",
|
|
5991
|
-
"description": "Whether checks enforce it. Default: true."
|
|
5992
|
-
},
|
|
5993
|
-
"failMode": {
|
|
5994
|
-
"type": "string",
|
|
5995
|
-
"enum": [
|
|
5996
|
-
"open",
|
|
5997
|
-
"closed"
|
|
5998
|
-
],
|
|
5999
|
-
"description": "What your code should do when Limitry can't be reached: `open` (allow) or `closed` (deny). Default: open."
|
|
6213
|
+
{
|
|
6214
|
+
"type": "object",
|
|
6215
|
+
"properties": {
|
|
6216
|
+
"name": {
|
|
6217
|
+
"type": "string",
|
|
6218
|
+
"minLength": 1,
|
|
6219
|
+
"maxLength": 100,
|
|
6220
|
+
"example": "AI budget",
|
|
6221
|
+
"description": "The limit's name: the key a sync matches on, unique in the list."
|
|
6222
|
+
}
|
|
6223
|
+
},
|
|
6224
|
+
"required": [
|
|
6225
|
+
"name"
|
|
6226
|
+
]
|
|
6000
6227
|
}
|
|
6001
|
-
},
|
|
6002
|
-
"required": [
|
|
6003
|
-
"name",
|
|
6004
|
-
"subjectKind",
|
|
6005
|
-
"action",
|
|
6006
|
-
"amount",
|
|
6007
|
-
"window"
|
|
6008
6228
|
]
|
|
6009
6229
|
},
|
|
6010
6230
|
"maxItems": 200,
|
|
6011
|
-
"description": "Every limit you want, each described whole: an omitted field is its default, as on create. Names are the key and must be unique in the list. At most 200."
|
|
6231
|
+
"description": "Every limit you want, each described whole: an omitted field is its default, as on create. Names are the key and must be unique in the list. What a limit counts (subject, action, unit, window, every, start, rolling) is fixed: to change it, rename the entry, which creates a new limit. At most 200."
|
|
6012
6232
|
}
|
|
6013
6233
|
},
|
|
6014
6234
|
{
|
|
@@ -6023,7 +6243,7 @@ var OPERATIONS = [
|
|
|
6023
6243
|
}
|
|
6024
6244
|
},
|
|
6025
6245
|
{
|
|
6026
|
-
"name": "
|
|
6246
|
+
"name": "preview",
|
|
6027
6247
|
"in": "body",
|
|
6028
6248
|
"type": "boolean",
|
|
6029
6249
|
"required": false,
|
|
@@ -6059,14 +6279,14 @@ var OPERATIONS = [
|
|
|
6059
6279
|
"idempotentHint": false
|
|
6060
6280
|
}
|
|
6061
6281
|
},
|
|
6062
|
-
"description": "Changes
|
|
6282
|
+
"description": "Changes a limit's name, amount, fail mode or whether it is enabled; checks use the new rule at once. What a limit counts (subject, action, unit, window, every, start, rolling) is fixed: a different value is refused (400) \u2014 create a new limit and archive this one. Owners and admins only, for a person's token.",
|
|
6063
6283
|
"fields": [
|
|
6064
6284
|
{
|
|
6065
6285
|
"name": "limitId",
|
|
6066
6286
|
"in": "path",
|
|
6067
6287
|
"type": "string",
|
|
6068
6288
|
"required": true,
|
|
6069
|
-
"example": "
|
|
6289
|
+
"example": "lmt_3f0c\u2026",
|
|
6070
6290
|
"schema": {
|
|
6071
6291
|
"type": "string"
|
|
6072
6292
|
}
|
|
@@ -6076,43 +6296,84 @@ var OPERATIONS = [
|
|
|
6076
6296
|
"in": "body",
|
|
6077
6297
|
"type": "string",
|
|
6078
6298
|
"required": false,
|
|
6079
|
-
"description": "A name
|
|
6299
|
+
"description": "A new name, unique among the environment's active limits.",
|
|
6080
6300
|
"example": "AI budget",
|
|
6081
6301
|
"schema": {
|
|
6082
6302
|
"type": "string",
|
|
6083
6303
|
"minLength": 1,
|
|
6084
6304
|
"maxLength": 100,
|
|
6085
|
-
"description": "A name
|
|
6305
|
+
"description": "A new name, unique among the environment's active limits."
|
|
6086
6306
|
}
|
|
6087
6307
|
},
|
|
6088
6308
|
{
|
|
6089
|
-
"name": "
|
|
6309
|
+
"name": "amount",
|
|
6090
6310
|
"in": "body",
|
|
6091
|
-
"type": "
|
|
6311
|
+
"type": "number",
|
|
6092
6312
|
"required": false,
|
|
6093
|
-
"description": "
|
|
6094
|
-
"example":
|
|
6313
|
+
"description": "How many units each subject may use per window.",
|
|
6314
|
+
"example": 300,
|
|
6095
6315
|
"schema": {
|
|
6096
|
-
"type": "
|
|
6097
|
-
"
|
|
6098
|
-
"
|
|
6099
|
-
"description": "
|
|
6316
|
+
"type": "integer",
|
|
6317
|
+
"minimum": 1,
|
|
6318
|
+
"maximum": 1e9,
|
|
6319
|
+
"description": "How many units each subject may use per window."
|
|
6100
6320
|
}
|
|
6101
6321
|
},
|
|
6102
6322
|
{
|
|
6103
|
-
"name": "
|
|
6323
|
+
"name": "enabled",
|
|
6324
|
+
"in": "body",
|
|
6325
|
+
"type": "boolean",
|
|
6326
|
+
"required": false,
|
|
6327
|
+
"description": "Whether checks enforce it.",
|
|
6328
|
+
"schema": {
|
|
6329
|
+
"type": "boolean",
|
|
6330
|
+
"description": "Whether checks enforce it."
|
|
6331
|
+
}
|
|
6332
|
+
},
|
|
6333
|
+
{
|
|
6334
|
+
"name": "failMode",
|
|
6104
6335
|
"in": "body",
|
|
6105
6336
|
"type": "string",
|
|
6106
6337
|
"required": false,
|
|
6107
|
-
"description": "
|
|
6338
|
+
"description": "What your code should do when Limitry can't be reached: `open` (allow) or `closed` (deny).",
|
|
6108
6339
|
"schema": {
|
|
6109
|
-
"type":
|
|
6110
|
-
|
|
6111
|
-
"
|
|
6340
|
+
"type": "string",
|
|
6341
|
+
"enum": [
|
|
6342
|
+
"open",
|
|
6343
|
+
"closed"
|
|
6112
6344
|
],
|
|
6113
|
-
"
|
|
6114
|
-
|
|
6115
|
-
|
|
6345
|
+
"description": "What your code should do when Limitry can't be reached: `open` (allow) or `closed` (deny)."
|
|
6346
|
+
}
|
|
6347
|
+
},
|
|
6348
|
+
{
|
|
6349
|
+
"name": "subject",
|
|
6350
|
+
"in": "body",
|
|
6351
|
+
"type": "json",
|
|
6352
|
+
"required": false,
|
|
6353
|
+
"description": "Who it applies to. Fixed once the limit is created: to change it, create a new limit and archive this one.",
|
|
6354
|
+
"example": {
|
|
6355
|
+
"kind": "user"
|
|
6356
|
+
},
|
|
6357
|
+
"schema": {
|
|
6358
|
+
"type": "object",
|
|
6359
|
+
"properties": {
|
|
6360
|
+
"kind": {
|
|
6361
|
+
"type": "string",
|
|
6362
|
+
"minLength": 1,
|
|
6363
|
+
"maxLength": 100,
|
|
6364
|
+
"description": "Your own label for who is checked: user, team, apikey \u2026"
|
|
6365
|
+
},
|
|
6366
|
+
"id": {
|
|
6367
|
+
"type": [
|
|
6368
|
+
"string",
|
|
6369
|
+
"null"
|
|
6370
|
+
],
|
|
6371
|
+
"minLength": 1,
|
|
6372
|
+
"maxLength": 100,
|
|
6373
|
+
"description": "One subject; omit or null for every subject of the kind. A subject's own limit replaces the kind's limits with the same action and window."
|
|
6374
|
+
}
|
|
6375
|
+
},
|
|
6376
|
+
"description": "Who it applies to. Fixed once the limit is created: to change it, create a new limit and archive this one."
|
|
6116
6377
|
}
|
|
6117
6378
|
},
|
|
6118
6379
|
{
|
|
@@ -6120,27 +6381,12 @@ var OPERATIONS = [
|
|
|
6120
6381
|
"in": "body",
|
|
6121
6382
|
"type": "string",
|
|
6122
6383
|
"required": false,
|
|
6123
|
-
"description": "The action to limit
|
|
6124
|
-
"example": "ai-call",
|
|
6384
|
+
"description": "The action. Fixed once the limit is created: to change it, create a new limit and archive this one.",
|
|
6125
6385
|
"schema": {
|
|
6126
6386
|
"type": "string",
|
|
6127
6387
|
"minLength": 1,
|
|
6128
6388
|
"maxLength": 100,
|
|
6129
|
-
"description": "The action to limit
|
|
6130
|
-
}
|
|
6131
|
-
},
|
|
6132
|
-
{
|
|
6133
|
-
"name": "amount",
|
|
6134
|
-
"in": "body",
|
|
6135
|
-
"type": "number",
|
|
6136
|
-
"required": false,
|
|
6137
|
-
"description": "How many units each subject may use per window.",
|
|
6138
|
-
"example": 200,
|
|
6139
|
-
"schema": {
|
|
6140
|
-
"type": "integer",
|
|
6141
|
-
"minimum": 1,
|
|
6142
|
-
"maximum": 1e9,
|
|
6143
|
-
"description": "How many units each subject may use per window."
|
|
6389
|
+
"description": "The action. Fixed once the limit is created: to change it, create a new limit and archive this one."
|
|
6144
6390
|
}
|
|
6145
6391
|
},
|
|
6146
6392
|
{
|
|
@@ -6148,13 +6394,12 @@ var OPERATIONS = [
|
|
|
6148
6394
|
"in": "body",
|
|
6149
6395
|
"type": "string",
|
|
6150
6396
|
"required": false,
|
|
6151
|
-
"description": "What the amount counts
|
|
6152
|
-
"example": "cents",
|
|
6397
|
+
"description": "What the amount counts. Fixed once the limit is created: to change it, create a new limit and archive this one.",
|
|
6153
6398
|
"schema": {
|
|
6154
6399
|
"type": "string",
|
|
6155
6400
|
"minLength": 1,
|
|
6156
6401
|
"maxLength": 40,
|
|
6157
|
-
"description": "What the amount counts
|
|
6402
|
+
"description": "What the amount counts. Fixed once the limit is created: to change it, create a new limit and archive this one."
|
|
6158
6403
|
}
|
|
6159
6404
|
},
|
|
6160
6405
|
{
|
|
@@ -6162,8 +6407,7 @@ var OPERATIONS = [
|
|
|
6162
6407
|
"in": "body",
|
|
6163
6408
|
"type": "string",
|
|
6164
6409
|
"required": false,
|
|
6165
|
-
"description": "
|
|
6166
|
-
"example": "month",
|
|
6410
|
+
"description": "The window. Fixed once the limit is created: to change it, create a new limit and archive this one.",
|
|
6167
6411
|
"schema": {
|
|
6168
6412
|
"type": "string",
|
|
6169
6413
|
"enum": [
|
|
@@ -6176,7 +6420,7 @@ var OPERATIONS = [
|
|
|
6176
6420
|
"total",
|
|
6177
6421
|
"concurrent"
|
|
6178
6422
|
],
|
|
6179
|
-
"description": "
|
|
6423
|
+
"description": "The window. Fixed once the limit is created: to change it, create a new limit and archive this one."
|
|
6180
6424
|
}
|
|
6181
6425
|
},
|
|
6182
6426
|
{
|
|
@@ -6184,13 +6428,12 @@ var OPERATIONS = [
|
|
|
6184
6428
|
"in": "body",
|
|
6185
6429
|
"type": "number",
|
|
6186
6430
|
"required": false,
|
|
6187
|
-
"description":
|
|
6188
|
-
"example": 1,
|
|
6431
|
+
"description": "How many windows a period lasts. Fixed once the limit is created: to change it, create a new limit and archive this one.",
|
|
6189
6432
|
"schema": {
|
|
6190
6433
|
"type": "integer",
|
|
6191
6434
|
"minimum": 1,
|
|
6192
6435
|
"maximum": 1e3,
|
|
6193
|
-
"description":
|
|
6436
|
+
"description": "How many windows a period lasts. Fixed once the limit is created: to change it, create a new limit and archive this one."
|
|
6194
6437
|
}
|
|
6195
6438
|
},
|
|
6196
6439
|
{
|
|
@@ -6198,16 +6441,15 @@ var OPERATIONS = [
|
|
|
6198
6441
|
"in": "body",
|
|
6199
6442
|
"type": "string",
|
|
6200
6443
|
"required": false,
|
|
6201
|
-
"description": "Where a period begins.
|
|
6202
|
-
"example": "calendar",
|
|
6444
|
+
"description": "Where a period begins. Fixed once the limit is created: to change it, create a new limit and archive this one.",
|
|
6203
6445
|
"schema": {
|
|
6204
6446
|
"type": "string",
|
|
6205
6447
|
"enum": [
|
|
6206
6448
|
"calendar",
|
|
6207
|
-
"
|
|
6449
|
+
"billing_date",
|
|
6208
6450
|
"first_use"
|
|
6209
6451
|
],
|
|
6210
|
-
"description": "Where a period begins.
|
|
6452
|
+
"description": "Where a period begins. Fixed once the limit is created: to change it, create a new limit and archive this one."
|
|
6211
6453
|
}
|
|
6212
6454
|
},
|
|
6213
6455
|
{
|
|
@@ -6215,36 +6457,10 @@ var OPERATIONS = [
|
|
|
6215
6457
|
"in": "body",
|
|
6216
6458
|
"type": "boolean",
|
|
6217
6459
|
"required": false,
|
|
6218
|
-
"description": "
|
|
6219
|
-
"schema": {
|
|
6220
|
-
"type": "boolean",
|
|
6221
|
-
"description": "Count the last window instead of the calendar one (minute, hour or day only). Default: false."
|
|
6222
|
-
}
|
|
6223
|
-
},
|
|
6224
|
-
{
|
|
6225
|
-
"name": "enabled",
|
|
6226
|
-
"in": "body",
|
|
6227
|
-
"type": "boolean",
|
|
6228
|
-
"required": false,
|
|
6229
|
-
"description": "Whether checks enforce it. Default: true.",
|
|
6460
|
+
"description": "Whether it counts the last window. Fixed once the limit is created: to change it, create a new limit and archive this one.",
|
|
6230
6461
|
"schema": {
|
|
6231
6462
|
"type": "boolean",
|
|
6232
|
-
"description": "Whether
|
|
6233
|
-
}
|
|
6234
|
-
},
|
|
6235
|
-
{
|
|
6236
|
-
"name": "failMode",
|
|
6237
|
-
"in": "body",
|
|
6238
|
-
"type": "string",
|
|
6239
|
-
"required": false,
|
|
6240
|
-
"description": "What your code should do when Limitry can't be reached: `open` (allow) or `closed` (deny). Default: open.",
|
|
6241
|
-
"schema": {
|
|
6242
|
-
"type": "string",
|
|
6243
|
-
"enum": [
|
|
6244
|
-
"open",
|
|
6245
|
-
"closed"
|
|
6246
|
-
],
|
|
6247
|
-
"description": "What your code should do when Limitry can't be reached: `open` (allow) or `closed` (deny). Default: open."
|
|
6463
|
+
"description": "Whether it counts the last window. Fixed once the limit is created: to change it, create a new limit and archive this one."
|
|
6248
6464
|
}
|
|
6249
6465
|
}
|
|
6250
6466
|
],
|
|
@@ -6273,7 +6489,7 @@ var OPERATIONS = [
|
|
|
6273
6489
|
"idempotentHint": false
|
|
6274
6490
|
}
|
|
6275
6491
|
},
|
|
6276
|
-
"description": "
|
|
6492
|
+
"description": "Commits a reservation with the ACTUAL amount used (`cost`): less than reserved gives the rest back at once; more is charged in full \u2014 the work happened. A commit after the reservation expired still charges `cost`. Repeating a commit answers the same. Its API-key check is cached briefly for speed: a revoked key can keep working here for up to 60 seconds.",
|
|
6277
6493
|
"fields": [
|
|
6278
6494
|
{
|
|
6279
6495
|
"name": "reservationId",
|
|
@@ -6323,7 +6539,7 @@ var OPERATIONS = [
|
|
|
6323
6539
|
"idempotentHint": false
|
|
6324
6540
|
}
|
|
6325
6541
|
},
|
|
6326
|
-
"description": "Keeps a
|
|
6542
|
+
"description": "Keeps a reservation for longer work: it now expires `ttlSeconds` from now (default 300, at most 3600) \u2014 call it again as a heartbeat. Only an open reservation can be extended. Its API-key check is cached briefly for speed: a revoked key can keep working here for up to 60 seconds.",
|
|
6327
6543
|
"fields": [
|
|
6328
6544
|
{
|
|
6329
6545
|
"name": "reservationId",
|
|
@@ -6374,7 +6590,7 @@ var OPERATIONS = [
|
|
|
6374
6590
|
"idempotentHint": false
|
|
6375
6591
|
}
|
|
6376
6592
|
},
|
|
6377
|
-
"description": "Gives the whole
|
|
6593
|
+
"description": "Gives the whole reservation back \u2014 the work did not happen. Repeating a release answers the same; releasing a committed reservation is refused. Its API-key check is cached briefly for speed: a revoked key can keep working here for up to 60 seconds.",
|
|
6378
6594
|
"fields": [
|
|
6379
6595
|
{
|
|
6380
6596
|
"name": "reservationId",
|
|
@@ -6412,7 +6628,7 @@ var OPERATIONS = [
|
|
|
6412
6628
|
"idempotentHint": true
|
|
6413
6629
|
}
|
|
6414
6630
|
},
|
|
6415
|
-
"description": "A subject's
|
|
6631
|
+
"description": "A subject's billing date: when its `billing_date` limits' periods start from. A subject is yours (a user, a team, an agent) and exists once a check names it.",
|
|
6416
6632
|
"fields": [
|
|
6417
6633
|
{
|
|
6418
6634
|
"name": "subjectKind",
|
|
@@ -6465,7 +6681,7 @@ var OPERATIONS = [
|
|
|
6465
6681
|
"idempotentHint": false
|
|
6466
6682
|
}
|
|
6467
6683
|
},
|
|
6468
|
-
"description": "
|
|
6684
|
+
"description": "Resets one limit on a subject (`limitId`), or every limit on it (`{}`), at once: for goodwill, a promotion or support. The schedule doesn't move: the next reset is still the usual one. A balance goes back to its amount; a concurrent limit is skipped. Send an Idempotency-Key and a repeat resets once. Owners and admins only, for a person's token.",
|
|
6469
6685
|
"fields": [
|
|
6470
6686
|
{
|
|
6471
6687
|
"name": "subjectKind",
|
|
@@ -6497,7 +6713,7 @@ var OPERATIONS = [
|
|
|
6497
6713
|
"type": "string",
|
|
6498
6714
|
"required": false,
|
|
6499
6715
|
"description": "Just this limit. Default: every limit on the subject.",
|
|
6500
|
-
"example": "
|
|
6716
|
+
"example": "lmt_3f0c\u2026",
|
|
6501
6717
|
"schema": {
|
|
6502
6718
|
"type": "string",
|
|
6503
6719
|
"description": "Just this limit. Default: every limit on the subject."
|
|
@@ -6512,7 +6728,7 @@ var OPERATIONS = [
|
|
|
6512
6728
|
"operationId": "subjects.update",
|
|
6513
6729
|
"method": "patch",
|
|
6514
6730
|
"path": "/subjects/{subjectKind}/{subjectId}",
|
|
6515
|
-
"summary": "Set a subject's
|
|
6731
|
+
"summary": "Set a subject's billing date",
|
|
6516
6732
|
"cli": {
|
|
6517
6733
|
"command": "subjects update",
|
|
6518
6734
|
"args": [
|
|
@@ -6530,7 +6746,7 @@ var OPERATIONS = [
|
|
|
6530
6746
|
"idempotentHint": false
|
|
6531
6747
|
}
|
|
6532
6748
|
},
|
|
6533
|
-
"description": "Sets when a subject's `
|
|
6749
|
+
"description": "Sets when a subject's `billing_date` limits' periods start from: typically its subscription's start date, from your billing system's webhook, so its allowance resets on its own billing day (monthly for an annual plan, too). Owners and admins only, for a person's token.",
|
|
6534
6750
|
"fields": [
|
|
6535
6751
|
{
|
|
6536
6752
|
"name": "subjectKind",
|
|
@@ -6557,7 +6773,7 @@ var OPERATIONS = [
|
|
|
6557
6773
|
}
|
|
6558
6774
|
},
|
|
6559
6775
|
{
|
|
6560
|
-
"name": "
|
|
6776
|
+
"name": "billingDate",
|
|
6561
6777
|
"in": "body",
|
|
6562
6778
|
"type": "string",
|
|
6563
6779
|
"required": true,
|
|
@@ -6593,7 +6809,7 @@ var OPERATIONS = [
|
|
|
6593
6809
|
"idempotentHint": true
|
|
6594
6810
|
}
|
|
6595
6811
|
},
|
|
6596
|
-
"description": "Units used, checks allowed and
|
|
6812
|
+
"description": "Units used, checks allowed and refused for one limit \u2014 across all subjects, or one (`subject`, as `kind:id`) \u2014 per hour or per day (UTC), every bucket in the range included. Up to about a minute behind live checks. Default: the last 30 days, by day.",
|
|
6597
6813
|
"fields": [
|
|
6598
6814
|
{
|
|
6599
6815
|
"name": "limitId",
|
|
@@ -6601,34 +6817,24 @@ var OPERATIONS = [
|
|
|
6601
6817
|
"type": "string",
|
|
6602
6818
|
"required": true,
|
|
6603
6819
|
"description": "The limit.",
|
|
6604
|
-
"example": "
|
|
6820
|
+
"example": "lmt_3f0c\u2026",
|
|
6605
6821
|
"schema": {
|
|
6606
6822
|
"type": "string",
|
|
6607
6823
|
"description": "The limit."
|
|
6608
6824
|
}
|
|
6609
6825
|
},
|
|
6610
6826
|
{
|
|
6611
|
-
"name": "
|
|
6612
|
-
"in": "query",
|
|
6613
|
-
"type": "string",
|
|
6614
|
-
"required": false,
|
|
6615
|
-
"description": "With `subjectId`: one subject's usage, not every subject's.",
|
|
6616
|
-
"example": "user",
|
|
6617
|
-
"schema": {
|
|
6618
|
-
"type": "string",
|
|
6619
|
-
"description": "With `subjectId`: one subject's usage, not every subject's."
|
|
6620
|
-
}
|
|
6621
|
-
},
|
|
6622
|
-
{
|
|
6623
|
-
"name": "subjectId",
|
|
6827
|
+
"name": "subject",
|
|
6624
6828
|
"in": "query",
|
|
6625
6829
|
"type": "string",
|
|
6626
6830
|
"required": false,
|
|
6627
|
-
"description": "
|
|
6628
|
-
"example": "usr_123",
|
|
6831
|
+
"description": "One subject's usage, not every subject's, as `kind:id`.",
|
|
6832
|
+
"example": "user:usr_123",
|
|
6629
6833
|
"schema": {
|
|
6630
6834
|
"type": "string",
|
|
6631
|
-
"
|
|
6835
|
+
"minLength": 3,
|
|
6836
|
+
"maxLength": 301,
|
|
6837
|
+
"description": "One subject's usage, not every subject's, as `kind:id`."
|
|
6632
6838
|
}
|
|
6633
6839
|
},
|
|
6634
6840
|
{
|
|
@@ -6697,34 +6903,20 @@ var OPERATIONS = [
|
|
|
6697
6903
|
"idempotentHint": true
|
|
6698
6904
|
}
|
|
6699
6905
|
},
|
|
6700
|
-
"description": "Every enabled limit on one subject \u2014 and on its shared subjects (`shared`, comma-separated `kind:id`, such as `team:team_42`) \u2014 with what is used, what is left and when it resets, exact as of now. For showing an end user where they stand: call it from your server with your key, never from a browser. Not a check: nothing is counted. A subject with no limit here answers an empty `limits` list.",
|
|
6906
|
+
"description": "Every enabled limit on one subject (`subject`, as `kind:id`, such as `user:usr_123`) \u2014 and on its shared subjects (`shared`, comma-separated `kind:id`, such as `team:team_42`) \u2014 with what is used, what is left and when it resets, exact as of now. For showing an end user where they stand: call it from your server with your key, never from a browser. Not a check: nothing is counted. A subject with no limit here answers an empty `limits` list.",
|
|
6701
6907
|
"fields": [
|
|
6702
6908
|
{
|
|
6703
|
-
"name": "
|
|
6704
|
-
"in": "query",
|
|
6705
|
-
"type": "string",
|
|
6706
|
-
"required": true,
|
|
6707
|
-
"description": "The subject's kind, as your checks send it.",
|
|
6708
|
-
"example": "user",
|
|
6709
|
-
"schema": {
|
|
6710
|
-
"type": "string",
|
|
6711
|
-
"minLength": 1,
|
|
6712
|
-
"maxLength": 100,
|
|
6713
|
-
"description": "The subject's kind, as your checks send it."
|
|
6714
|
-
}
|
|
6715
|
-
},
|
|
6716
|
-
{
|
|
6717
|
-
"name": "id",
|
|
6909
|
+
"name": "subject",
|
|
6718
6910
|
"in": "query",
|
|
6719
6911
|
"type": "string",
|
|
6720
6912
|
"required": true,
|
|
6721
|
-
"description": "The subject
|
|
6722
|
-
"example": "usr_123",
|
|
6913
|
+
"description": "The subject, as `kind:id`: its kind and id as your checks send them.",
|
|
6914
|
+
"example": "user:usr_123",
|
|
6723
6915
|
"schema": {
|
|
6724
6916
|
"type": "string",
|
|
6725
|
-
"minLength":
|
|
6726
|
-
"maxLength":
|
|
6727
|
-
"description": "The subject
|
|
6917
|
+
"minLength": 3,
|
|
6918
|
+
"maxLength": 301,
|
|
6919
|
+
"description": "The subject, as `kind:id`: its kind and id as your checks send them."
|
|
6728
6920
|
}
|
|
6729
6921
|
},
|
|
6730
6922
|
{
|
|
@@ -6763,7 +6955,7 @@ var OPERATIONS = [
|
|
|
6763
6955
|
"idempotentHint": true
|
|
6764
6956
|
}
|
|
6765
6957
|
},
|
|
6766
|
-
"description": "A limit's subjects in its CURRENT window, most used first: units used, what is left, how many checks
|
|
6958
|
+
"description": "A limit's subjects in its CURRENT window, most used first: units used, what is left, and how many checks it refused. Up to about a minute behind live checks.",
|
|
6767
6959
|
"fields": [
|
|
6768
6960
|
{
|
|
6769
6961
|
"name": "limitId",
|
|
@@ -6771,7 +6963,7 @@ var OPERATIONS = [
|
|
|
6771
6963
|
"type": "string",
|
|
6772
6964
|
"required": true,
|
|
6773
6965
|
"description": "The limit.",
|
|
6774
|
-
"example": "
|
|
6966
|
+
"example": "lmt_3f0c\u2026",
|
|
6775
6967
|
"schema": {
|
|
6776
6968
|
"type": "string",
|
|
6777
6969
|
"description": "The limit."
|
|
@@ -7278,7 +7470,7 @@ var OPERATIONS = [
|
|
|
7278
7470
|
"idempotentHint": true
|
|
7279
7471
|
}
|
|
7280
7472
|
},
|
|
7281
|
-
"description": "How much of each meter the workspace used this period, with the plan's limit for it (null = unlimited). A period is a month from the workspace's billing date: its subscription's (an annual plan
|
|
7473
|
+
"description": "How much of each meter the workspace used this period, with the plan's limit for it (null = unlimited). A period is a month from the workspace's billing date: its subscription's (an annual plan's counts reset monthly from its start), else the day the workspace was created.",
|
|
7282
7474
|
"fields": [],
|
|
7283
7475
|
"hasBody": false,
|
|
7284
7476
|
"workspace": true,
|
|
@@ -7628,6 +7820,89 @@ var OPERATIONS = [
|
|
|
7628
7820
|
"hasBody": false,
|
|
7629
7821
|
"workspace": true,
|
|
7630
7822
|
"scope": "workspace.webhooks:write"
|
|
7823
|
+
},
|
|
7824
|
+
{
|
|
7825
|
+
"operationId": "workspace.webhookEndpoints.update",
|
|
7826
|
+
"method": "patch",
|
|
7827
|
+
"path": "/workspace/webhook-endpoints/{endpointId}",
|
|
7828
|
+
"summary": "Update a webhook endpoint",
|
|
7829
|
+
"cli": {
|
|
7830
|
+
"command": "workspace webhook-endpoints update",
|
|
7831
|
+
"args": [
|
|
7832
|
+
"endpointId"
|
|
7833
|
+
],
|
|
7834
|
+
"confirm": false
|
|
7835
|
+
},
|
|
7836
|
+
"mcp": {
|
|
7837
|
+
"exposed": false,
|
|
7838
|
+
"reason": "Changes where workspace data is sent. A person configures webhook endpoints (Settings \u2192 Webhooks in the app)."
|
|
7839
|
+
},
|
|
7840
|
+
"description": "Changes an endpoint's URL, description or event types in place; only the fields sent change. The endpoint keeps its signing secret, so receivers keep verifying deliveries. A new URL passes the same checks as on create: https only, no private hosts.",
|
|
7841
|
+
"fields": [
|
|
7842
|
+
{
|
|
7843
|
+
"name": "endpointId",
|
|
7844
|
+
"in": "path",
|
|
7845
|
+
"type": "string",
|
|
7846
|
+
"required": true,
|
|
7847
|
+
"example": "we_3f0c\u2026",
|
|
7848
|
+
"schema": {
|
|
7849
|
+
"type": "string"
|
|
7850
|
+
}
|
|
7851
|
+
},
|
|
7852
|
+
{
|
|
7853
|
+
"name": "url",
|
|
7854
|
+
"in": "body",
|
|
7855
|
+
"type": "string",
|
|
7856
|
+
"required": false,
|
|
7857
|
+
"description": "The https URL that receives the events.",
|
|
7858
|
+
"example": "https://example.com/hooks",
|
|
7859
|
+
"schema": {
|
|
7860
|
+
"type": "string",
|
|
7861
|
+
"minLength": 1,
|
|
7862
|
+
"maxLength": 2048,
|
|
7863
|
+
"description": "The https URL that receives the events."
|
|
7864
|
+
}
|
|
7865
|
+
},
|
|
7866
|
+
{
|
|
7867
|
+
"name": "description",
|
|
7868
|
+
"in": "body",
|
|
7869
|
+
"type": "string",
|
|
7870
|
+
"required": false,
|
|
7871
|
+
"description": "A note for people managing the endpoint; null clears it.",
|
|
7872
|
+
"schema": {
|
|
7873
|
+
"type": [
|
|
7874
|
+
"string",
|
|
7875
|
+
"null"
|
|
7876
|
+
],
|
|
7877
|
+
"maxLength": 200,
|
|
7878
|
+
"description": "A note for people managing the endpoint; null clears it."
|
|
7879
|
+
}
|
|
7880
|
+
},
|
|
7881
|
+
{
|
|
7882
|
+
"name": "eventTypes",
|
|
7883
|
+
"in": "body",
|
|
7884
|
+
"type": "json",
|
|
7885
|
+
"required": false,
|
|
7886
|
+
"description": "Only these event types (see event-types); null or an empty list for every event.",
|
|
7887
|
+
"example": [
|
|
7888
|
+
"workspace.member.joined"
|
|
7889
|
+
],
|
|
7890
|
+
"schema": {
|
|
7891
|
+
"type": [
|
|
7892
|
+
"array",
|
|
7893
|
+
"null"
|
|
7894
|
+
],
|
|
7895
|
+
"items": {
|
|
7896
|
+
"type": "string"
|
|
7897
|
+
},
|
|
7898
|
+
"maxItems": 100,
|
|
7899
|
+
"description": "Only these event types (see event-types); null or an empty list for every event."
|
|
7900
|
+
}
|
|
7901
|
+
}
|
|
7902
|
+
],
|
|
7903
|
+
"hasBody": true,
|
|
7904
|
+
"workspace": true,
|
|
7905
|
+
"scope": "workspace.webhooks:write"
|
|
7631
7906
|
}
|
|
7632
7907
|
];
|
|
7633
7908
|
|
|
@@ -7850,11 +8125,61 @@ function print(ctx, data, asJson) {
|
|
|
7850
8125
|
ctx.io.stdout(String(data));
|
|
7851
8126
|
}
|
|
7852
8127
|
|
|
8128
|
+
// src/product/check.ts
|
|
8129
|
+
var checkCommand = (program2, ctx) => {
|
|
8130
|
+
program2.command("check").description(`Short for \`${brand.name} checks create\``).argument("<subject>", "who is acting, as kind:id (user:usr_123)").argument("<action>", "what they want to do (ai-call)").option("--cost <n>", "units it would use (default 1)").option(
|
|
8131
|
+
"--shared <kind:id>",
|
|
8132
|
+
"a shared subject it also draws from (a team's pool); repeat for more",
|
|
8133
|
+
(value, previous = []) => [...previous, value]
|
|
8134
|
+
).option("--workspace <slug>", "act in this workspace for one call").option("--json", "the decision as the API answers it").addHelpText(
|
|
8135
|
+
"after",
|
|
8136
|
+
`
|
|
8137
|
+
Example: ${brand.name} check user:usr_123 ai-call --cost 5
|
|
8138
|
+
A refusal exits 0: it's an answer. Errors exit 1.`
|
|
8139
|
+
).action(
|
|
8140
|
+
async (subject, action, options) => {
|
|
8141
|
+
const cost = options.cost === void 0 ? void 0 : Number(options.cost);
|
|
8142
|
+
if (cost !== void 0 && !Number.isInteger(cost)) {
|
|
8143
|
+
throw new Error("--cost must be a whole number");
|
|
8144
|
+
}
|
|
8145
|
+
const decision = await ctx.client({ workspace: options.workspace }).checks.create(
|
|
8146
|
+
{
|
|
8147
|
+
subject: parseSubject(subject),
|
|
8148
|
+
action,
|
|
8149
|
+
...cost !== void 0 ? { cost } : {},
|
|
8150
|
+
...options.shared?.length ? { shared: options.shared.map(parseSubject) } : {}
|
|
8151
|
+
},
|
|
8152
|
+
{ idempotencyKey: crypto.randomUUID() }
|
|
8153
|
+
);
|
|
8154
|
+
if (!decision) throw new Error("Limitry answered without a decision");
|
|
8155
|
+
ctx.io.stdout(options.json ? json(decision) : answer(decision));
|
|
8156
|
+
}
|
|
8157
|
+
);
|
|
8158
|
+
};
|
|
8159
|
+
function parseSubject(value) {
|
|
8160
|
+
const at = value.indexOf(":");
|
|
8161
|
+
if (at <= 0 || at === value.length - 1) {
|
|
8162
|
+
throw new Error(
|
|
8163
|
+
`"${value}": write a subject as kind:id, e.g. user:usr_123`
|
|
8164
|
+
);
|
|
8165
|
+
}
|
|
8166
|
+
return { kind: value.slice(0, at), id: value.slice(at + 1) };
|
|
8167
|
+
}
|
|
8168
|
+
var left = (r) => `${r.name}: ${r.remaining} ${r.unit} left${r.resetsAt ? `, resets ${r.resetsAt}` : ""}`;
|
|
8169
|
+
function answer(decision) {
|
|
8170
|
+
if (decision.allowed) {
|
|
8171
|
+
return decision.limits.length === 0 ? "Allowed. No limit applies." : `Allowed. ${decision.limits.map(left).join("; ")}`;
|
|
8172
|
+
}
|
|
8173
|
+
return decision.reason?.message ?? "Refused.";
|
|
8174
|
+
}
|
|
8175
|
+
|
|
7853
8176
|
// src/product/limits-sync.ts
|
|
7854
8177
|
import { readFileSync as readFileSync4 } from "node:fs";
|
|
7855
8178
|
import { resolve } from "node:path";
|
|
7856
8179
|
var limitsSyncCommand = (program2, ctx) => {
|
|
7857
|
-
const group = program2.commands.find((c) => c.name() === "limits") ?? program2.command("limits").description(
|
|
8180
|
+
const group = program2.commands.find((c) => c.name() === "limits") ?? program2.command("limits").description(
|
|
8181
|
+
"archive, create, credits, get, list, restore, sync, update"
|
|
8182
|
+
);
|
|
7858
8183
|
syncCommand(
|
|
7859
8184
|
group.command("sync"),
|
|
7860
8185
|
ctx,
|
|
@@ -7869,27 +8194,21 @@ Shortcut: \`${brand.name} sync\`, the same command.`
|
|
|
7869
8194
|
""
|
|
7870
8195
|
);
|
|
7871
8196
|
};
|
|
7872
|
-
function clientFor(ctx, workspace2) {
|
|
7873
|
-
const token = ctx.settings().token ?? "";
|
|
7874
|
-
return ctx.client({
|
|
7875
|
-
workspace: token.startsWith("pat_") ? workspace2 : false
|
|
7876
|
-
});
|
|
7877
|
-
}
|
|
7878
8197
|
function syncCommand(command, ctx, description, more) {
|
|
7879
|
-
command.description(description).argument("[file]", "the file to read", "limitry.json").option("--
|
|
8198
|
+
command.description(description).argument("[file]", "the file to read", "limitry.json").option("--preview", "show what would change, and change nothing").option("--limits <json>", "the limits as a JSON array, instead of a file").option(
|
|
7880
8199
|
"--archive-missing",
|
|
7881
8200
|
"archive active limits the file doesn't name (restorable)"
|
|
7882
8201
|
).option("--workspace <slug>", "act in this workspace for one call").option("--json", "machine-readable output").addHelpText(
|
|
7883
8202
|
"after",
|
|
7884
|
-
'\nThe file: { "limits": [ { "name": "AI budget", "
|
|
8203
|
+
'\nThe file: { "limits": [ { "name": "AI budget", "subject": { "kind": "user" }, "action": "ai-call", "amount": 200, "unit": "cents", "window": "month" } ] }\nEach limit is described whole: a field left out is its default, as on `limits create`. Names are the key.\nWhat a limit counts (subject, action, unit, window, every, start, rolling) is fixed: to change it, give the entry a new name.' + more
|
|
7885
8204
|
).action(
|
|
7886
8205
|
async (file, options) => {
|
|
7887
8206
|
const limits = options.limits === void 0 ? readLimitsFile(file) : parseLimits(options.limits, "--limits");
|
|
7888
|
-
const result = await
|
|
8207
|
+
const result = await ctx.client({ workspace: options.workspace }).limits.sync(
|
|
7889
8208
|
{
|
|
7890
8209
|
limits,
|
|
7891
8210
|
archiveMissing: options.archiveMissing ?? false,
|
|
7892
|
-
|
|
8211
|
+
preview: options.preview ?? false
|
|
7893
8212
|
},
|
|
7894
8213
|
{ idempotencyKey: crypto.randomUUID() }
|
|
7895
8214
|
);
|
|
@@ -7939,7 +8258,7 @@ function parseLimits(text, flag2) {
|
|
|
7939
8258
|
}
|
|
7940
8259
|
var show = (value) => typeof value === "string" ? value : JSON.stringify(value);
|
|
7941
8260
|
function summary(result) {
|
|
7942
|
-
const dry = result.
|
|
8261
|
+
const dry = result.preview;
|
|
7943
8262
|
const lines = [
|
|
7944
8263
|
...result.created.map(
|
|
7945
8264
|
(name) => `${dry ? "Would create" : "Created"}: ${name}`
|
|
@@ -7959,64 +8278,10 @@ function summary(result) {
|
|
|
7959
8278
|
];
|
|
7960
8279
|
}
|
|
7961
8280
|
if (same2 > 0) lines.push(`Unchanged: ${same2}`);
|
|
7962
|
-
if (dry) lines.push("
|
|
8281
|
+
if (dry) lines.push("Preview: nothing was changed.");
|
|
7963
8282
|
return lines;
|
|
7964
8283
|
}
|
|
7965
8284
|
|
|
7966
|
-
// src/product/check.ts
|
|
7967
|
-
var checkCommand = (program2, ctx) => {
|
|
7968
|
-
program2.command("check").description(`Short for \`${brand.name} checks create\``).argument("<subject>", "who is acting, as kind:id (user:usr_123)").argument("<action>", "what they want to do (ai-call)").option("--cost <n>", "units it would use (default 1)").option(
|
|
7969
|
-
"--shared <kind:id>",
|
|
7970
|
-
"a shared subject it also draws from (a team's pool); repeat for more",
|
|
7971
|
-
(value, previous = []) => [...previous, value]
|
|
7972
|
-
).option("--workspace <slug>", "act in this workspace for one call").option("--json", "the decision as the API answers it").addHelpText(
|
|
7973
|
-
"after",
|
|
7974
|
-
`
|
|
7975
|
-
Example: ${brand.name} check user:usr_123 ai-call --cost 5
|
|
7976
|
-
A refusal exits 0: it's an answer. Errors exit 1.`
|
|
7977
|
-
).action(
|
|
7978
|
-
async (subject, action, options) => {
|
|
7979
|
-
const cost = options.cost === void 0 ? void 0 : Number(options.cost);
|
|
7980
|
-
if (cost !== void 0 && !Number.isInteger(cost)) {
|
|
7981
|
-
throw new Error("--cost must be a whole number");
|
|
7982
|
-
}
|
|
7983
|
-
const decision = await clientFor(ctx, options.workspace).checks.create(
|
|
7984
|
-
{
|
|
7985
|
-
subject: parseSubject(subject),
|
|
7986
|
-
action,
|
|
7987
|
-
...cost !== void 0 ? { cost } : {},
|
|
7988
|
-
...options.shared?.length ? { shared: options.shared.map(parseSubject) } : {}
|
|
7989
|
-
},
|
|
7990
|
-
{ idempotencyKey: crypto.randomUUID() }
|
|
7991
|
-
);
|
|
7992
|
-
ctx.io.stdout(options.json ? json(decision) : answer(decision));
|
|
7993
|
-
}
|
|
7994
|
-
);
|
|
7995
|
-
};
|
|
7996
|
-
function parseSubject(value) {
|
|
7997
|
-
const at = value.indexOf(":");
|
|
7998
|
-
if (at <= 0 || at === value.length - 1) {
|
|
7999
|
-
throw new Error(
|
|
8000
|
-
`"${value}": write a subject as kind:id, e.g. user:usr_123`
|
|
8001
|
-
);
|
|
8002
|
-
}
|
|
8003
|
-
return { kind: value.slice(0, at), id: value.slice(at + 1) };
|
|
8004
|
-
}
|
|
8005
|
-
var left = (r) => `${r.name}: ${r.remaining} ${r.unit} left${r.resetsAt ? `, resets ${r.resetsAt}` : ""}`;
|
|
8006
|
-
function answer(decision) {
|
|
8007
|
-
if (decision.allowed) {
|
|
8008
|
-
return decision.remaining.length === 0 ? "Allowed. No limit applies." : `Allowed. ${decision.remaining.map(left).join("; ")}`;
|
|
8009
|
-
}
|
|
8010
|
-
const retry = decision.retryAfterSeconds !== null ? ` Try again in ${decision.retryAfterSeconds} s.` : "";
|
|
8011
|
-
if (decision.reason?.code === "too_many_reservations") {
|
|
8012
|
-
return `Refused: too many open reservations for this subject.${retry}`;
|
|
8013
|
-
}
|
|
8014
|
-
const limit = decision.remaining.find(
|
|
8015
|
-
(r) => r.limit === decision.reason?.limit
|
|
8016
|
-
);
|
|
8017
|
-
return limit ? `Refused by ${left(limit)}.${retry}` : `Refused: a limit is used up.${retry}`;
|
|
8018
|
-
}
|
|
8019
|
-
|
|
8020
8285
|
// src/product/index.ts
|
|
8021
8286
|
var productCommands = [limitsSyncCommand, checkCommand];
|
|
8022
8287
|
|
|
@@ -8034,7 +8299,7 @@ function tokenEnvVar() {
|
|
|
8034
8299
|
function build(io) {
|
|
8035
8300
|
let actedIn = null;
|
|
8036
8301
|
let sending;
|
|
8037
|
-
const program2 = new Command().name(brand.name).description(`${brand.productName} command line`).version("1.0.0-rc.
|
|
8302
|
+
const program2 = new Command().name(brand.name).description(`${brand.productName} command line`).version("1.0.0-rc.11").addHelpText(
|
|
8038
8303
|
"after",
|
|
8039
8304
|
`
|
|
8040
8305
|
Token: \`${brand.name} login\` stores it in ~/.config/${brand.name}/; ${tokenEnvVar()} overrides.
|