agl 43.1.0 → 43.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +12 -0
- package/dist_serve/bundle.js +5329 -5116
- package/dist_serve/bundle.js.map +2 -2
- package/dist_serve/bundle.js.third-party-notices.json +30 -30
- package/dist_serve/bundle.js.third-party-notices.txt +30 -30
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/aglapi/aglapisurface.document.js +325 -1
- package/dist_ts/aglapi/interfaces.d.ts +3 -0
- package/dist_ts/aglapi/interfaces.js +2 -1
- package/dist_ts/classes.authmodels.js +17 -4
- package/dist_ts/classes.authstore.js +16 -2
- package/dist_ts/classes.cli.d.ts +4 -2
- package/dist_ts/classes.cli.js +6 -3
- package/dist_ts/classes.controller.d.ts +10 -0
- package/dist_ts/classes.controller.js +119 -4
- package/dist_ts/classes.controllermanagement.d.ts +14 -4
- package/dist_ts/classes.controllermanagement.js +20 -2
- package/dist_ts/classes.gatewayclient.d.ts +115 -0
- package/dist_ts/classes.gatewayclient.js +336 -0
- package/dist_ts/classes.gatewayclientstore.d.ts +97 -0
- package/dist_ts/classes.gatewayclientstore.js +261 -0
- package/dist_ts/classes.inferencerouter.d.ts +9 -0
- package/dist_ts/classes.inferencerouter.js +11 -1
- package/dist_ts/classes.inferenceservice.d.ts +9 -0
- package/dist_ts/classes.inferenceservice.js +27 -1
- package/dist_ts/classes.mcpclient.d.ts +4 -1
- package/dist_ts/classes.mcpclient.js +19 -1
- package/dist_ts/classes.mcpcontrollerhost.d.ts +3 -1
- package/dist_ts/classes.mcpcontrollerhost.js +9 -1
- package/dist_ts/classes.mcptools.d.ts +3 -3
- package/dist_ts/classes.mcptools.js +26 -1
- package/dist_ts/classes.mcpvalidation.js +7 -1
- package/dist_ts/classes.proxycontrol.d.ts +11 -0
- package/dist_ts/classes.proxycontrol.js +7 -1
- package/dist_ts/classes.proxycontrolclient.d.ts +17 -0
- package/dist_ts/classes.proxycontrolclient.js +35 -1
- package/dist_ts/classes.proxydaemon.js +8 -1
- package/dist_ts/classes.proxymonitor.d.ts +7 -0
- package/dist_ts/classes.proxymonitor.js +13 -1
- package/dist_ts/classes.trustedproxysecrets.d.ts +2 -0
- package/dist_ts/classes.trustedproxysecrets.js +5 -1
- package/dist_ts/functions.controllermanagementsurface.d.ts +4 -0
- package/dist_ts/functions.controllermanagementsurface.js +15 -1
- package/dist_ts/functions.gatewaycli.d.ts +24 -0
- package/dist_ts/functions.gatewaycli.js +395 -0
- package/dist_ts/functions.gatewayroute.d.ts +135 -0
- package/dist_ts/functions.gatewayroute.js +366 -0
- package/dist_ts/functions.inferencecli.js +30 -1
- package/dist_ts/functions.inferencecliarguments.d.ts +8 -2
- package/dist_ts/functions.inferencecliarguments.js +31 -3
- package/dist_ts/functions.proxycapacity.d.ts +50 -0
- package/dist_ts/functions.proxycapacity.js +142 -0
- package/dist_ts/functions.proxycapacitycli.d.ts +30 -0
- package/dist_ts/functions.proxycapacitycli.js +65 -0
- package/dist_ts/functions.remotecli.d.ts +30 -3
- package/dist_ts/functions.remotecli.js +44 -18
- package/dist_ts/interfaces.auth.d.ts +25 -4
- package/dist_ts/interfaces.auth.js +7 -1
- package/dist_ts/plugins.d.ts +2 -0
- package/dist_ts/plugins.js +4 -1
- package/dist_ts_authswitch/authority-placement.js +3 -17
- package/dist_ts_authswitch/authority-usagelimit.d.ts +29 -0
- package/dist_ts_authswitch/authority-usagelimit.js +66 -2
- package/dist_ts_authswitch/authority-usagewindows.d.ts +6 -0
- package/dist_ts_authswitch/authority-usagewindows.js +15 -1
- package/dist_ts_interfaces/gateway.d.ts +151 -0
- package/dist_ts_interfaces/gateway.js +66 -0
- package/dist_ts_interfaces/index.d.ts +2 -0
- package/dist_ts_interfaces/index.js +3 -1
- package/dist_ts_interfaces/management.d.ts +4 -4
- package/dist_ts_interfaces/management.js +3 -2
- package/dist_ts_interfaces/mcprequests.d.ts +14 -2
- package/dist_ts_interfaces/mcprequests.js +2 -1
- package/dist_ts_interfaces/proxycapacity.d.ts +99 -0
- package/dist_ts_interfaces/proxycapacity.js +74 -0
- package/package.json +4 -3
- package/readme.md +79 -5
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/aglapi/aglapisurface.document.ts +324 -0
- package/ts/aglapi/interfaces.ts +1 -0
- package/ts/classes.authmodels.ts +22 -3
- package/ts/classes.authstore.ts +15 -1
- package/ts/classes.cli.ts +7 -3
- package/ts/classes.controller.ts +134 -3
- package/ts/classes.controllermanagement.ts +40 -7
- package/ts/classes.gatewayclient.ts +401 -0
- package/ts/classes.gatewayclientstore.ts +272 -0
- package/ts/classes.inferencerouter.ts +12 -0
- package/ts/classes.inferenceservice.ts +27 -0
- package/ts/classes.mcpclient.ts +18 -0
- package/ts/classes.mcpcontrollerhost.ts +13 -0
- package/ts/classes.mcptools.ts +30 -2
- package/ts/classes.mcpvalidation.ts +5 -0
- package/ts/classes.proxycontrol.ts +16 -0
- package/ts/classes.proxycontrolclient.ts +30 -0
- package/ts/classes.proxydaemon.ts +8 -0
- package/ts/classes.proxymonitor.ts +12 -0
- package/ts/classes.trustedproxysecrets.ts +5 -0
- package/ts/functions.controllermanagementsurface.ts +15 -0
- package/ts/functions.gatewaycli.ts +436 -0
- package/ts/functions.gatewayroute.ts +429 -0
- package/ts/functions.inferencecli.ts +24 -0
- package/ts/functions.inferencecliarguments.ts +30 -4
- package/ts/functions.proxycapacity.ts +191 -0
- package/ts/functions.proxycapacitycli.ts +104 -0
- package/ts/functions.remotecli.ts +56 -20
- package/ts/interfaces.auth.ts +28 -4
- package/ts/plugins.ts +5 -0
- package/ts_authswitch/authority-placement.ts +2 -15
- package/ts_authswitch/authority-usagelimit.ts +84 -1
- package/ts_authswitch/authority-usagewindows.ts +13 -0
- package/ts_interfaces/gateway.ts +242 -0
- package/ts_interfaces/index.ts +2 -0
- package/ts_interfaces/management.ts +6 -4
- package/ts_interfaces/mcprequests.ts +18 -0
- package/ts_interfaces/proxycapacity.ts +162 -0
- package/ts_web/00_commitinfo_data.ts +1 -1
- package/ts_web/classes.browserpresentation.ts +13 -5
- package/ts_web/elements.accounts.ts +40 -113
- package/ts_web/elements.configurationpane.ts +24 -37
- package/ts_web/elements.devices.ts +4 -17
- package/ts_web/elements.flexlimitstrip.ts +21 -70
- package/ts_web/elements.harnesscontrollerapp.ts +91 -185
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* `authority-spend.ts`, whose own imports carry nothing to a page, so it bundles for a browser with nothing behind it.
|
|
7
7
|
*/
|
|
8
8
|
import type { TAuthSwitchProviderUsageReading, TAuthSwitchUsageReading } from './classes.authorityusage.js';
|
|
9
|
-
import { authSwitchUsageWindowName, authSwitchUsageWindowPercent, authSwitchUsageWindows,
|
|
9
|
+
import { authSwitchUsageWindowName, authSwitchUsageWindowPercent, authSwitchUsageWindowResetAt, authSwitchUsageWindows,
|
|
10
10
|
type IAuthSwitchUsageWindow } from './authority-usagewindows.js';
|
|
11
11
|
import { authSwitchSpendExhausted, authSwitchSpendMonthEnded } from './authority-spend.js';
|
|
12
12
|
|
|
@@ -167,6 +167,89 @@ export const authSwitchUsageLimit = (readingArg: TAuthSwitchUsageReading, maxUsa
|
|
|
167
167
|
}
|
|
168
168
|
};
|
|
169
169
|
|
|
170
|
+
/** One moment an account's headroom is projected to change (epoch milliseconds), and its headroom from then on. */
|
|
171
|
+
export interface IAuthSwitchHeadroomStep {
|
|
172
|
+
at: number;
|
|
173
|
+
headroom: number;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** How an account's headroom stands and is projected to change, from its reading alone (see `authSwitchHeadroomGrowth`). */
|
|
177
|
+
export interface IAuthSwitchHeadroomGrowth {
|
|
178
|
+
/** The headroom now, as `authSwitchUsageLimit` states it; `null` when the reading counts no window. */
|
|
179
|
+
headroom: number | null;
|
|
180
|
+
/**
|
|
181
|
+
* The counted window the headroom is bound by: of the windows at the headroom, the one whose period ends last (one
|
|
182
|
+
* whose end the reading does not state last of all), since the headroom grows only once each of them started a new
|
|
183
|
+
* period; `null` without a counted window.
|
|
184
|
+
*/
|
|
185
|
+
binding: IAuthSwitchUsageWindow | null;
|
|
186
|
+
/** The moments the headroom changes, in order, each with the headroom from then on; empty when none is foreseen. */
|
|
187
|
+
steps: IAuthSwitchHeadroomStep[];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* When an account's headroom (`authSwitchUsageLimit`) changes, as far as its reading foresees it at `nowArg`: each
|
|
192
|
+
* counted window starts a new period at its reset (`authSwitchUsageWindowResetAt`, a stated reset that passed moved on by
|
|
193
|
+
* whole lengths), from then on with nothing used, and an active reserve lift ends at its expiry, which brings the
|
|
194
|
+
* threshold back to `maxUsagePercentArg`. The headroom is the least of the windows' at every moment, so it grows only
|
|
195
|
+
* once every window at the least has reset: a binding 5-hour window lifts it at its own reset, long before a weekly
|
|
196
|
+
* window that is not binding. A window whose reading names no reset holds its headroom for as long as the projection
|
|
197
|
+
* runs. Usage spent meanwhile is not foreseen, so each step is the most the headroom can be then. An API key's month
|
|
198
|
+
* leaves all of its limit at the month's end. Pure, and bundles for a browser as the rest of the module does.
|
|
199
|
+
*/
|
|
200
|
+
export const authSwitchHeadroomGrowth = (readingArg: TAuthSwitchUsageReading, maxUsagePercentArg: number | null,
|
|
201
|
+
nowArg: number, reserveLiftUntilArg: string | null = null): IAuthSwitchHeadroomGrowth => {
|
|
202
|
+
const limit = authSwitchUsageLimit(readingArg, maxUsagePercentArg, nowArg, reserveLiftUntilArg);
|
|
203
|
+
switch (readingArg.kind) {
|
|
204
|
+
case 'openai':
|
|
205
|
+
case 'anthropic': {
|
|
206
|
+
const windows = authSwitchCountedWindows(readingArg, maxUsagePercentArg);
|
|
207
|
+
if (limit.headroom === null || !windows.length) return { headroom: limit.headroom, binding: null, steps: [] };
|
|
208
|
+
// The end of each window's current period, `Infinity` where the reading does not state it.
|
|
209
|
+
const ends = windows.map((windowArg) => {
|
|
210
|
+
if (windowArg.resetAt === null) return Infinity;
|
|
211
|
+
const end = authSwitchUsageWindowResetAt(windowArg, nowArg);
|
|
212
|
+
return end > nowArg ? end : Infinity;
|
|
213
|
+
});
|
|
214
|
+
const thresholdAt = (atArg: number): number =>
|
|
215
|
+
authSwitchEffectiveMaxUsage(maxUsagePercentArg, reserveLiftUntilArg, atArg) ?? 100;
|
|
216
|
+
const headroomAt = (atArg: number): number => {
|
|
217
|
+
const threshold = thresholdAt(atArg);
|
|
218
|
+
return Math.min(...windows.map((windowArg, indexArg) => ends[indexArg] <= atArg ? threshold
|
|
219
|
+
: Math.max(0, threshold - authSwitchUsageWindowPercent(windowArg))));
|
|
220
|
+
};
|
|
221
|
+
const liftEnd = maxUsagePercentArg !== null && authSwitchReserveLiftActive(reserveLiftUntilArg, nowArg)
|
|
222
|
+
? [Date.parse(reserveLiftUntilArg!)] : [];
|
|
223
|
+
const moments = [...new Set([...ends.filter((endArg) => Number.isFinite(endArg)), ...liftEnd])].sort((left, right) => left - right);
|
|
224
|
+
const steps: IAuthSwitchHeadroomStep[] = [];
|
|
225
|
+
let previous = limit.headroom;
|
|
226
|
+
for (const at of moments) {
|
|
227
|
+
const headroom = headroomAt(at);
|
|
228
|
+
if (headroom !== previous) steps.push({ at, headroom });
|
|
229
|
+
previous = headroom;
|
|
230
|
+
}
|
|
231
|
+
const threshold = thresholdAt(nowArg);
|
|
232
|
+
const binding = windows
|
|
233
|
+
.map((windowArg, indexArg) => ({ window: windowArg, end: ends[indexArg],
|
|
234
|
+
headroom: Math.max(0, threshold - authSwitchUsageWindowPercent(windowArg)) }))
|
|
235
|
+
.filter((entryArg) => entryArg.headroom === limit.headroom)
|
|
236
|
+
.sort((left, right) => right.end - left.end)[0]?.window ?? null;
|
|
237
|
+
return { headroom: limit.headroom, binding, steps };
|
|
238
|
+
}
|
|
239
|
+
case 'spend': {
|
|
240
|
+
const window = authSwitchUsageWindows(readingArg)[0];
|
|
241
|
+
if (limit.headroom === null || !window) return { headroom: limit.headroom, binding: null, steps: [] };
|
|
242
|
+
const end = Date.parse(readingArg.spend.resetAt);
|
|
243
|
+
return { headroom: limit.headroom, binding: window,
|
|
244
|
+
steps: limit.headroom < 100 && end > nowArg ? [{ at: end, headroom: 100 }] : [] };
|
|
245
|
+
}
|
|
246
|
+
default: {
|
|
247
|
+
const unhandledReading: never = readingArg;
|
|
248
|
+
return unhandledReading;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
|
|
170
253
|
const windowNames = (windowsArg: readonly IAuthSwitchUsageWindow[]): string => {
|
|
171
254
|
const names = [...new Set(windowsArg.map(authSwitchUsageWindowName))];
|
|
172
255
|
return names.length < 2 ? names.join('') : `${names.slice(0, -1).join(', ')} and ${names.at(-1)}`;
|
|
@@ -106,6 +106,19 @@ export const authSwitchUsageWindows = (readingArg: TAuthSwitchUsageReading | nul
|
|
|
106
106
|
export const authSwitchUsageWindowPercent = (windowArg: IAuthSwitchUsageWindow): number =>
|
|
107
107
|
windowArg.estimatedUsedPercent ?? windowArg.usedPercent;
|
|
108
108
|
|
|
109
|
+
/**
|
|
110
|
+
* When a window's current period ends, after `nowArg` (epoch milliseconds): its stated reset; a reset that passed and was
|
|
111
|
+
* not read again yet moved forward by whole window lengths, as the provider starts each period; `nowArg` plus the
|
|
112
|
+
* window's length when the reading names no reset.
|
|
113
|
+
*/
|
|
114
|
+
export const authSwitchUsageWindowResetAt = (windowArg: IAuthSwitchUsageWindow, nowArg: number): number => {
|
|
115
|
+
const lengthMs = windowArg.durationSeconds * 1000;
|
|
116
|
+
const stated = windowArg.resetAt === null ? Number.NaN : Date.parse(windowArg.resetAt);
|
|
117
|
+
if (!Number.isFinite(stated)) return nowArg + (lengthMs > 0 ? lengthMs : 0);
|
|
118
|
+
if (stated > nowArg || !(lengthMs > 0)) return stated;
|
|
119
|
+
return stated + (Math.floor((nowArg - stated) / lengthMs) + 1) * lengthMs;
|
|
120
|
+
};
|
|
121
|
+
|
|
109
122
|
/** A window's name: Claude's own, or a ChatGPT window's length after the name of the extra limit it belongs to. */
|
|
110
123
|
export const authSwitchUsageWindowName = (windowArg: IAuthSwitchUsageWindow): string => windowArg.label
|
|
111
124
|
?? `${windowArg.limitName === null ? '' : `${windowArg.limitName} `}${usageWindowPeriod(windowArg.durationSeconds)}`;
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
import type { IConfigurationLocalCliActor } from './configuration.js';
|
|
3
|
+
import type { IControllerStatus } from './interfaces.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The controller's own gateway route (AGL-828): AGL is a gateway client of a dcrouter (`agl remote gateway enrol`)
|
|
7
|
+
* and keeps one route for its public hostname there, LOCAL NETWORK only. Only the CLI and `agl status --json` show
|
|
8
|
+
* it, over the loopback management endpoint; the browser contract does not carry it.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Where the gateway client stands.
|
|
13
|
+
* - `not_enrolled`: no enrolment; nothing is synced.
|
|
14
|
+
* - `finalize_pending`: `agl remote gateway enrol` stored a candidate credential the gateway has not confirmed yet.
|
|
15
|
+
* - `syncing`: a sync runs.
|
|
16
|
+
* - `synced`: the last sync wrote the route; the next one repairs drift after six hours.
|
|
17
|
+
* - `retrying`: the last sync failed in a way a later one can fix; it is retried with backoff.
|
|
18
|
+
* - `waiting`: the gateway lacks `routes.requestHeaders`, so no route is written; the controller asks again with
|
|
19
|
+
* backoff from 30 s to 15 min, then at every six-hour sync and every `agl remote gateway sync`, and writes the route
|
|
20
|
+
* as soon as the gateway announces it.
|
|
21
|
+
* - `failed`: the last sync failed in a way only the operator can fix (`problem`); nothing is retried on its own.
|
|
22
|
+
* - `stopped`: the controller does not run; `problem` and `lastSyncedAt` are what it last recorded.
|
|
23
|
+
*/
|
|
24
|
+
export type TControllerGatewayState =
|
|
25
|
+
| 'not_enrolled'
|
|
26
|
+
| 'finalize_pending'
|
|
27
|
+
| 'syncing'
|
|
28
|
+
| 'synced'
|
|
29
|
+
| 'retrying'
|
|
30
|
+
| 'waiting'
|
|
31
|
+
| 'failed'
|
|
32
|
+
| 'stopped';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Why a sync did not write the route. The transient ones (`unreachable`, `gateway_error`) are retried, a gateway
|
|
36
|
+
* without request headers is waited for (`request_headers_unsupported`); every other one is terminal and names what
|
|
37
|
+
* the operator does.
|
|
38
|
+
*/
|
|
39
|
+
export type TControllerGatewayProblemCode =
|
|
40
|
+
/** The gateway did not answer: network, timeout or an HTTP error in front of its API. */
|
|
41
|
+
| 'unreachable'
|
|
42
|
+
/** The gateway answered with a refusal a later sync may not meet (not initialized, DNS provider unavailable). */
|
|
43
|
+
| 'gateway_error'
|
|
44
|
+
/** The credential is unknown, revoked, or bound to a missing or disabled gateway client: enrol again. */
|
|
45
|
+
| 'credential_invalid'
|
|
46
|
+
/** An admin changed the gateway client's policy, which invalidates its credential: enrol again. */
|
|
47
|
+
| 'credential_stale'
|
|
48
|
+
/** The enrolment's candidate credential is not finalized: run `agl remote gateway enrol` again. */
|
|
49
|
+
| 'finalize_pending'
|
|
50
|
+
/** The credential lacks a capability the route needs, or the gateway does not write routes idempotently. */
|
|
51
|
+
| 'capability_missing'
|
|
52
|
+
/**
|
|
53
|
+
* The gateway lacks `routes.requestHeaders` (before dcrouter 34.5.0, or one downgraded): the route cannot carry the
|
|
54
|
+
* trusted-proxy secret, so none is written until the gateway announces it.
|
|
55
|
+
*/
|
|
56
|
+
| 'request_headers_unsupported'
|
|
57
|
+
/** The gateway refused the route itself (validation, policy, an unknown source profile). */
|
|
58
|
+
| 'route_refused'
|
|
59
|
+
/** DNS `record-conflict`: another owner or a manual record holds the hostname. */
|
|
60
|
+
| 'dns_record_conflict'
|
|
61
|
+
/** DNS `zone-unmanaged`: the gateway manages no zone holding the hostname. */
|
|
62
|
+
| 'dns_zone_unmanaged'
|
|
63
|
+
/** DNS `target-invalid`: the gateway cannot publish an address for the route. */
|
|
64
|
+
| 'dns_target_invalid'
|
|
65
|
+
/**
|
|
66
|
+
* The controller's public origin is no longer the hostname it was enrolled for: enrol again. `agl remote set`
|
|
67
|
+
* refuses such a move while enrolled, so only a leftover meets it (an interrupted change, or a move an earlier
|
|
68
|
+
* release made after a rollback).
|
|
69
|
+
*/
|
|
70
|
+
| 'origin_changed'
|
|
71
|
+
/** The controller is not behind a TLS reverse proxy (`agl remote set`), so it has no route. */
|
|
72
|
+
| 'not_proxied'
|
|
73
|
+
/** The enrolment's gateway URL is plain http off loopback, which no credential is sent over: enrol again with https. */
|
|
74
|
+
| 'gateway_url_insecure';
|
|
75
|
+
|
|
76
|
+
export const controllerGatewayProblemCodes = [
|
|
77
|
+
'unreachable', 'gateway_error', 'credential_invalid', 'credential_stale', 'finalize_pending', 'capability_missing',
|
|
78
|
+
'request_headers_unsupported', 'route_refused', 'dns_record_conflict', 'dns_zone_unmanaged', 'dns_target_invalid',
|
|
79
|
+
'origin_changed', 'not_proxied', 'gateway_url_insecure',
|
|
80
|
+
] as const satisfies readonly TControllerGatewayProblemCode[];
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* What follows a sync that ended with a problem: `transient` is retried with backoff, `waiting` is asked again with
|
|
84
|
+
* backoff and then at every six-hour sync, `terminal` waits for the operator (the next start or sync).
|
|
85
|
+
*/
|
|
86
|
+
export type TControllerGatewayProblemKind = 'transient' | 'waiting' | 'terminal';
|
|
87
|
+
|
|
88
|
+
export const controllerGatewayProblemKind: Record<TControllerGatewayProblemCode, TControllerGatewayProblemKind> = {
|
|
89
|
+
unreachable: 'transient',
|
|
90
|
+
gateway_error: 'transient',
|
|
91
|
+
request_headers_unsupported: 'waiting',
|
|
92
|
+
credential_invalid: 'terminal',
|
|
93
|
+
credential_stale: 'terminal',
|
|
94
|
+
finalize_pending: 'terminal',
|
|
95
|
+
capability_missing: 'terminal',
|
|
96
|
+
route_refused: 'terminal',
|
|
97
|
+
dns_record_conflict: 'terminal',
|
|
98
|
+
dns_zone_unmanaged: 'terminal',
|
|
99
|
+
dns_target_invalid: 'terminal',
|
|
100
|
+
origin_changed: 'terminal',
|
|
101
|
+
not_proxied: 'terminal',
|
|
102
|
+
gateway_url_insecure: 'terminal',
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export interface IControllerGatewayProblem {
|
|
106
|
+
code: TControllerGatewayProblemCode;
|
|
107
|
+
/** What failed and what the operator does; never a credential. */
|
|
108
|
+
message: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** What `agl remote gateway enrol` registered: the gateway, the client and the route it keeps. */
|
|
112
|
+
export interface IControllerGatewayEnrolment {
|
|
113
|
+
/** The gateway's API base URL; requests go to `<gatewayUrl>/typedrequest`. */
|
|
114
|
+
gatewayUrl: string;
|
|
115
|
+
/** The gateway client this controller is (`agl-<installation>-<port>`, type `custom`). */
|
|
116
|
+
gatewayClientId: string;
|
|
117
|
+
/** `controller-<port>`. */
|
|
118
|
+
appId: string;
|
|
119
|
+
/** The route's hostname: the public origin's when it enrolled. */
|
|
120
|
+
hostname: string;
|
|
121
|
+
/** Where the gateway forwards the route. */
|
|
122
|
+
target: { host: string; port: number };
|
|
123
|
+
/** Unix epoch milliseconds. */
|
|
124
|
+
enrolledAt: number;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The DNS outcome of the last sync that wrote the route. */
|
|
128
|
+
export interface IControllerGatewayDns {
|
|
129
|
+
/** The gateway's own DNS status, such as `managed-unchanged` or `manual-satisfied`. */
|
|
130
|
+
status: string;
|
|
131
|
+
/** False for `manual-satisfied`: records the gateway does not own satisfy the route. */
|
|
132
|
+
owned: boolean;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface IControllerGatewayStatus {
|
|
136
|
+
state: TControllerGatewayState;
|
|
137
|
+
enrolment?: IControllerGatewayEnrolment;
|
|
138
|
+
/** Unix epoch milliseconds of the last sync that wrote the route. */
|
|
139
|
+
lastSyncedAt?: number;
|
|
140
|
+
dns?: IControllerGatewayDns;
|
|
141
|
+
/**
|
|
142
|
+
* After the last sync that wrote the route: true when the gateway named its source profile as `LOCAL NETWORK`,
|
|
143
|
+
* false when the gateway names no profile (before dcrouter 34.5.0), so the binding is unverified.
|
|
144
|
+
*/
|
|
145
|
+
sourceProfileVerified?: boolean;
|
|
146
|
+
/** Why the last sync failed; absent after one that wrote the route. */
|
|
147
|
+
problem?: IControllerGatewayProblem;
|
|
148
|
+
/** Unix epoch milliseconds of the next sync the controller runs on its own. */
|
|
149
|
+
nextSyncAt?: number;
|
|
150
|
+
/** A trusted-proxy secret rotation waits for a sync that carries the new secret to its route. */
|
|
151
|
+
rotationPending?: boolean;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const gatewayStates: readonly TControllerGatewayState[] = [
|
|
155
|
+
'not_enrolled', 'finalize_pending', 'syncing', 'synced', 'retrying', 'waiting', 'failed', 'stopped',
|
|
156
|
+
];
|
|
157
|
+
|
|
158
|
+
const isRecord = (valueArg: unknown): valueArg is Record<string, unknown> => (
|
|
159
|
+
typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg)
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
const hasOnlyKeys = (valueArg: Record<string, unknown>, requiredArg: readonly string[], optionalArg: readonly string[]): boolean => (
|
|
163
|
+
requiredArg.every((keyArg) => Object.hasOwn(valueArg, keyArg))
|
|
164
|
+
&& Object.keys(valueArg).every((keyArg) => requiredArg.includes(keyArg) || optionalArg.includes(keyArg))
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
const isText = (valueArg: unknown, maximumArg: number): valueArg is string => (
|
|
168
|
+
typeof valueArg === 'string' && valueArg.length > 0 && valueArg.length <= maximumArg
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
const isTime = (valueArg: unknown): valueArg is number => Number.isSafeInteger(valueArg) && (valueArg as number) >= 0;
|
|
172
|
+
|
|
173
|
+
const isEnrolment = (valueArg: unknown): valueArg is IControllerGatewayEnrolment => {
|
|
174
|
+
if (!isRecord(valueArg) || !hasOnlyKeys(valueArg, ['gatewayUrl', 'gatewayClientId', 'appId', 'hostname', 'target', 'enrolledAt'], [])) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
const target = valueArg.target;
|
|
178
|
+
return isText(valueArg.gatewayUrl, 2048) && isText(valueArg.gatewayClientId, 128) && isText(valueArg.appId, 128)
|
|
179
|
+
&& isText(valueArg.hostname, 253) && isTime(valueArg.enrolledAt)
|
|
180
|
+
&& isRecord(target) && hasOnlyKeys(target, ['host', 'port'], []) && isText(target.host, 253)
|
|
181
|
+
&& Number.isSafeInteger(target.port) && (target.port as number) >= 1 && (target.port as number) <= 65_535;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Reads the gateway section of a controller's management status, or `undefined` when it is not one this release
|
|
186
|
+
* reads; never trusts a field it does not check.
|
|
187
|
+
*/
|
|
188
|
+
export const readControllerGatewayStatus = (valueArg: unknown): IControllerGatewayStatus | undefined => {
|
|
189
|
+
if (!isRecord(valueArg)
|
|
190
|
+
|| !hasOnlyKeys(valueArg, ['state'], ['enrolment', 'lastSyncedAt', 'dns', 'sourceProfileVerified', 'problem', 'nextSyncAt', 'rotationPending'])
|
|
191
|
+
|| !gatewayStates.includes(valueArg.state as TControllerGatewayState)
|
|
192
|
+
|| (valueArg.enrolment !== undefined && !isEnrolment(valueArg.enrolment))
|
|
193
|
+
|| (valueArg.lastSyncedAt !== undefined && !isTime(valueArg.lastSyncedAt))
|
|
194
|
+
|| (valueArg.nextSyncAt !== undefined && !isTime(valueArg.nextSyncAt))
|
|
195
|
+
|| (valueArg.rotationPending !== undefined && typeof valueArg.rotationPending !== 'boolean')
|
|
196
|
+
|| (valueArg.sourceProfileVerified !== undefined && typeof valueArg.sourceProfileVerified !== 'boolean')) return undefined;
|
|
197
|
+
const dns = valueArg.dns;
|
|
198
|
+
if (dns !== undefined && !(isRecord(dns) && hasOnlyKeys(dns, ['status', 'owned'], [])
|
|
199
|
+
&& isText(dns.status, 64) && typeof dns.owned === 'boolean')) return undefined;
|
|
200
|
+
const problem = valueArg.problem;
|
|
201
|
+
if (problem !== undefined && !(isRecord(problem) && hasOnlyKeys(problem, ['code', 'message'], [])
|
|
202
|
+
&& controllerGatewayProblemCodes.includes(problem.code as TControllerGatewayProblemCode)
|
|
203
|
+
&& isText(problem.message, 4096))) return undefined;
|
|
204
|
+
return valueArg as unknown as IControllerGatewayStatus;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The management endpoint's status: the controller's status and, from AGL-828 on, its gateway client. A controller
|
|
209
|
+
* of an earlier release reports no `gateway`.
|
|
210
|
+
*/
|
|
211
|
+
export interface IControllerManagementStatus extends IControllerStatus {
|
|
212
|
+
gateway?: IControllerGatewayStatus;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export interface IReq_ControllerManagementStatus extends plugins.typedrequestInterfaces.implementsTR<
|
|
216
|
+
plugins.typedrequestInterfaces.ITypedRequest,
|
|
217
|
+
IReq_ControllerManagementStatus
|
|
218
|
+
> {
|
|
219
|
+
method: 'controller.status';
|
|
220
|
+
request: Record<string, never>;
|
|
221
|
+
response: IControllerManagementStatus;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Syncs the controller's gateway route now (`agl remote gateway sync`), first rotating the trusted-proxy secret when
|
|
226
|
+
* `rotate` is set: the route then carries the new secret, and the previous one is refused once the gateway confirms
|
|
227
|
+
* it. Only over the loopback management endpoint, from a process of the controller's own OS user that holds the
|
|
228
|
+
* request's connection (`localCli` names it); the answer is the gateway status after that sync.
|
|
229
|
+
*/
|
|
230
|
+
export interface IReq_ControllerGatewaySync extends plugins.typedrequestInterfaces.implementsTR<
|
|
231
|
+
plugins.typedrequestInterfaces.ITypedRequest,
|
|
232
|
+
IReq_ControllerGatewaySync
|
|
233
|
+
> {
|
|
234
|
+
method: 'controller.gateway.sync';
|
|
235
|
+
request: {
|
|
236
|
+
rotate: boolean;
|
|
237
|
+
localCli: IConfigurationLocalCliActor;
|
|
238
|
+
};
|
|
239
|
+
response: {
|
|
240
|
+
gateway: IControllerGatewayStatus;
|
|
241
|
+
};
|
|
242
|
+
}
|
package/ts_interfaces/index.ts
CHANGED
|
@@ -11,6 +11,7 @@ export * from './devtools.js';
|
|
|
11
11
|
export * from './requests.js';
|
|
12
12
|
export * from './mcprequests.js';
|
|
13
13
|
export * from './management.js';
|
|
14
|
+
export * from './gateway.js';
|
|
14
15
|
export * from './codex.js';
|
|
15
16
|
export * from './mcpworkspacerequests.js';
|
|
16
17
|
export * from './mcpjobrequests.js';
|
|
@@ -33,5 +34,6 @@ export * from './requests.driver.js';
|
|
|
33
34
|
export * from './sessionfeed.js';
|
|
34
35
|
export * from './nativesessions.js';
|
|
35
36
|
export * from './metrics.js';
|
|
37
|
+
export * from './proxycapacity.js';
|
|
36
38
|
export * from './devices.js';
|
|
37
39
|
export * from './requests.devices.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { IReq_ControllerGatewaySync, IReq_ControllerManagementStatus } from './gateway.js';
|
|
2
2
|
import type {
|
|
3
3
|
IReq_ControllerUpgradeFinalize,
|
|
4
4
|
IReq_ControllerUpgradeLaunch,
|
|
@@ -36,17 +36,19 @@ export const controllerManagementMethods = [
|
|
|
36
36
|
'controller.upgrade.launch',
|
|
37
37
|
'controller.upgrade.prepare.begin',
|
|
38
38
|
'controller.upgrade.finalize',
|
|
39
|
+
'controller.gateway.sync',
|
|
39
40
|
] as const;
|
|
40
41
|
|
|
41
42
|
export type TControllerManagementMethod = typeof controllerManagementMethods[number];
|
|
42
43
|
|
|
43
44
|
export type TControllerManagementTypedRequest =
|
|
44
|
-
|
|
|
45
|
+
| IReq_ControllerManagementStatus
|
|
45
46
|
| IReq_ControllerUpgradeLaunch
|
|
46
47
|
| IReq_ControllerUpgradePrepareBegin
|
|
47
|
-
| IReq_ControllerUpgradeFinalize
|
|
48
|
+
| IReq_ControllerUpgradeFinalize
|
|
49
|
+
| IReq_ControllerGatewaySync;
|
|
48
50
|
|
|
49
|
-
/** A management request carries a token, a version and a mode at most. */
|
|
51
|
+
/** A management request carries a token, a version and a mode, or a local CLI's attribution, at most. */
|
|
50
52
|
export const controllerManagementRequestMaximumBodyBytes = 16 * 1024;
|
|
51
53
|
/** The status document with every harness profile and upgrade blocker it may report. */
|
|
52
54
|
export const controllerManagementResponseMaximumBytes = 1024 * 1024;
|
|
@@ -6,6 +6,7 @@ import { controllerMcpFleetMethods, type TControllerMcpFleetTypedRequest } from
|
|
|
6
6
|
import { controllerMcpSessionFeedMethods, type TControllerMcpSessionFeedTypedRequest } from './sessionfeed.js';
|
|
7
7
|
import { controllerMcpDeployMethods, type TControllerMcpDeployTypedRequest } from './deployments.js';
|
|
8
8
|
import * as plugins from './plugins.js';
|
|
9
|
+
import type { IProxyCapacityAnswer, IProxyCapacityRequest } from './proxycapacity.js';
|
|
9
10
|
import type { IReq_ControllerMcpWorkerEnroll, IWorkerCallerSubject } from './workercallers.js';
|
|
10
11
|
import type { IReq_ControllerSettingsGet, IReq_ControllerSettingsUpdate } from './requests.js';
|
|
11
12
|
import type {
|
|
@@ -44,6 +45,7 @@ export const controllerMcpMethods = [
|
|
|
44
45
|
'agl.mcp.session.scratchpad.read',
|
|
45
46
|
'agl.mcp.session.scratchpad.update',
|
|
46
47
|
'agl.mcp.session.message',
|
|
48
|
+
'agl.mcp.proxy.capacity',
|
|
47
49
|
...controllerMcpWorkspaceMethods,
|
|
48
50
|
...controllerMcpJobMethods,
|
|
49
51
|
'agl.mcp.controller.settings.get',
|
|
@@ -299,6 +301,21 @@ export interface IReq_ControllerMcpStatus extends plugins.typedrequestInterfaces
|
|
|
299
301
|
response: IControllerMcpStatusResponse;
|
|
300
302
|
}
|
|
301
303
|
|
|
304
|
+
/**
|
|
305
|
+
* A pool's room for new work in AGL's inference proxy, or both pools' when `provider` is absent: each counted account by
|
|
306
|
+
* its id with its plan, state, headroom, binding window and when its headroom next grows, the newest provider 429 on
|
|
307
|
+
* the pool, and an `open` or `wait` verdict against `minHeadroom` (15 unless given). The controller asks the proxy; the
|
|
308
|
+
* answer names no account by label or email.
|
|
309
|
+
*/
|
|
310
|
+
export interface IReq_ControllerMcpProxyCapacity extends plugins.typedrequestInterfaces.implementsTR<
|
|
311
|
+
plugins.typedrequestInterfaces.ITypedRequest,
|
|
312
|
+
IReq_ControllerMcpProxyCapacity
|
|
313
|
+
> {
|
|
314
|
+
method: 'agl.mcp.proxy.capacity';
|
|
315
|
+
request: IProxyCapacityRequest;
|
|
316
|
+
response: IProxyCapacityAnswer;
|
|
317
|
+
}
|
|
318
|
+
|
|
302
319
|
/** Lists the projects registered with the running AGL controller. */
|
|
303
320
|
export interface IReq_ControllerMcpProjectsList extends plugins.typedrequestInterfaces.implementsTR<
|
|
304
321
|
plugins.typedrequestInterfaces.ITypedRequest,
|
|
@@ -441,6 +458,7 @@ export type TControllerMcpTypedRequest =
|
|
|
441
458
|
| TControllerMcpWorkspaceTypedRequest
|
|
442
459
|
| TControllerMcpJobTypedRequest
|
|
443
460
|
| IReq_ControllerMcpStatus
|
|
461
|
+
| IReq_ControllerMcpProxyCapacity
|
|
444
462
|
| IReq_ControllerMcpProjectsList
|
|
445
463
|
| IReq_ControllerMcpSessionsList
|
|
446
464
|
| IReq_ControllerMcpSessionRead
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A pool's room for new work in AGL's inference proxy (AGL-1097): `agl proxy capacity`, the proxy's
|
|
3
|
+
* `agl.proxy.capacity` and the MCP tool `proxy_capacity` answer it alike. A leaf: it imports nothing.
|
|
4
|
+
*
|
|
5
|
+
* The answer carries no identity: an account is named by its account id alone, never by its label or email.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** A pool: Claude accounts (`anthropic`) or ChatGPT accounts (`openai`). */
|
|
9
|
+
export type TProxyCapacityProvider = 'anthropic' | 'openai';
|
|
10
|
+
export const proxyCapacityProviders: readonly TProxyCapacityProvider[] = ['anthropic', 'openai'];
|
|
11
|
+
|
|
12
|
+
/** The room a pool needs, in headroom percent summed over its members, when a request names none. */
|
|
13
|
+
export const proxyCapacityDefaultMinimumHeadroom = 15;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* How long a pool waits after the newest provider 429 on one of its members, and how long a proxy must have observed its
|
|
17
|
+
* pools before the absence of a 429 counts: five minutes.
|
|
18
|
+
*/
|
|
19
|
+
export const proxyCapacityQuietMs = 300_000;
|
|
20
|
+
|
|
21
|
+
/** What is asked: one pool, or both when `provider` is absent, against `minHeadroom` (a non-negative integer). */
|
|
22
|
+
export interface IProxyCapacityRequest {
|
|
23
|
+
provider?: TProxyCapacityProvider;
|
|
24
|
+
minHeadroom?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A member's window, as its reading states it. */
|
|
28
|
+
export interface IProxyCapacityWindow {
|
|
29
|
+
/** The provider's name for the window, or its length (`5h`, `7d`) for one without a name. */
|
|
30
|
+
name: string;
|
|
31
|
+
durationSeconds: number;
|
|
32
|
+
/** The percent its limit decides by: the account service's estimate between reads where it has one. */
|
|
33
|
+
usedPercent: number;
|
|
34
|
+
/** Whether `usedPercent` is that estimate. */
|
|
35
|
+
estimated: boolean;
|
|
36
|
+
/** When its current period ends, as the reading states it; `null` when it states none. */
|
|
37
|
+
resetAt: string | null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Whether a member takes new work now, as `agl proxy status` judges it, or why not. */
|
|
41
|
+
export type TProxyCapacityMemberState = 'available' | 'reserved' | 'exhausted' | 'cooling' | 'unavailable';
|
|
42
|
+
export const proxyCapacityMemberStates: readonly TProxyCapacityMemberState[] = ['available', 'reserved', 'exhausted',
|
|
43
|
+
'cooling', 'unavailable'];
|
|
44
|
+
|
|
45
|
+
/** An account the pool counts: a subscription sign-in of a counted plan that has not lapsed. */
|
|
46
|
+
export interface IProxyCapacityMember {
|
|
47
|
+
/** The account's id (64 hex characters). */
|
|
48
|
+
accountId: string;
|
|
49
|
+
/** Its plan as its held reading states it, one the pool counts. */
|
|
50
|
+
plan: string;
|
|
51
|
+
state: TProxyCapacityMemberState;
|
|
52
|
+
/** The headroom it adds to the room now: its headroom below its threshold while it takes new work, else 0. */
|
|
53
|
+
headroom: number;
|
|
54
|
+
/** The window its headroom is bound by; `null` without a usable reading with a counted window. */
|
|
55
|
+
binding: IProxyCapacityWindow | null;
|
|
56
|
+
/**
|
|
57
|
+
* When the headroom it adds next grows, from its reading alone: the binding window's reset, or the moment it takes
|
|
58
|
+
* work again (a provider 429's rest, a provider refusal, its windows' resets); `null` when it cannot grow or that
|
|
59
|
+
* moment is unknown (a stale reading, a sign-in or payment problem).
|
|
60
|
+
*/
|
|
61
|
+
growsAt: string | null;
|
|
62
|
+
/** The headroom it adds from `growsAt` on, at most; `null` with it. */
|
|
63
|
+
growsTo: number | null;
|
|
64
|
+
/** When the account service reads an account it cannot judge now again, when it says; `null` otherwise. */
|
|
65
|
+
recheckAt: string | null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Why a pool waits: too little room, a provider 429 within the quiet period, or a proxy that has not observed it long enough. */
|
|
69
|
+
export type TProxyCapacityWaitReason = 'room' | 'provider-429' | 'quiet-unobserved';
|
|
70
|
+
export const proxyCapacityWaitReasons: readonly TProxyCapacityWaitReason[] = ['room', 'provider-429', 'quiet-unobserved'];
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* `open`: the pool takes new work now. `wait`: not before `until`, `seconds` from the answer's `at`; both `null` when
|
|
74
|
+
* the members' readings foresee no moment the room reaches the minimum.
|
|
75
|
+
*/
|
|
76
|
+
export type TProxyCapacityVerdict =
|
|
77
|
+
| { state: 'open' }
|
|
78
|
+
| { state: 'wait'; reason: TProxyCapacityWaitReason; until: string; seconds: number }
|
|
79
|
+
| { state: 'wait'; reason: TProxyCapacityWaitReason; until: null; seconds: null };
|
|
80
|
+
|
|
81
|
+
export interface IProxyCapacityPool {
|
|
82
|
+
provider: TProxyCapacityProvider;
|
|
83
|
+
minHeadroom: number;
|
|
84
|
+
/** The members' headroom summed, in percent. */
|
|
85
|
+
room: number;
|
|
86
|
+
members: IProxyCapacityMember[];
|
|
87
|
+
/** The newest provider 429 on a member the proxy saw since `observedSince`, or `null`. */
|
|
88
|
+
last429At: string | null;
|
|
89
|
+
verdict: TProxyCapacityVerdict;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface IProxyCapacityAnswer {
|
|
93
|
+
/** When the proxy judged it, by its own clock. */
|
|
94
|
+
at: string;
|
|
95
|
+
/** Since when the proxy observes its pools' provider 429s: its start. */
|
|
96
|
+
observedSince: string;
|
|
97
|
+
pools: IProxyCapacityPool[];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** At most this many members a pool lists (the proxy routes a pool of at most 64 accounts per request). */
|
|
101
|
+
const maximumMembers = 256;
|
|
102
|
+
|
|
103
|
+
const isRecord = (valueArg: unknown): valueArg is Record<string, unknown> =>
|
|
104
|
+
typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg);
|
|
105
|
+
const hasExactKeys = (valueArg: Record<string, unknown>, requiredArg: readonly string[],
|
|
106
|
+
optionalArg: readonly string[] = []): boolean => requiredArg.every((keyArg) => Object.hasOwn(valueArg, keyArg))
|
|
107
|
+
&& Object.keys(valueArg).every((keyArg) => requiredArg.includes(keyArg) || optionalArg.includes(keyArg));
|
|
108
|
+
const isTime = (valueArg: unknown): valueArg is string => typeof valueArg === 'string' && valueArg.length <= 32
|
|
109
|
+
&& Number.isFinite(Date.parse(valueArg));
|
|
110
|
+
const isTimeOrNull = (valueArg: unknown): boolean => valueArg === null || isTime(valueArg);
|
|
111
|
+
const isPercent = (valueArg: unknown): valueArg is number => typeof valueArg === 'number' && Number.isFinite(valueArg);
|
|
112
|
+
const isMinimum = (valueArg: unknown): valueArg is number => typeof valueArg === 'number' && Number.isSafeInteger(valueArg)
|
|
113
|
+
&& valueArg >= 0;
|
|
114
|
+
|
|
115
|
+
/** Whether a value is a capacity request as `IProxyCapacityRequest` states it, and nothing else. */
|
|
116
|
+
export const isProxyCapacityRequest = (valueArg: unknown): valueArg is IProxyCapacityRequest => isRecord(valueArg)
|
|
117
|
+
&& hasExactKeys(valueArg, [], ['provider', 'minHeadroom'])
|
|
118
|
+
&& (valueArg.provider === undefined || proxyCapacityProviders.includes(valueArg.provider as TProxyCapacityProvider))
|
|
119
|
+
&& (valueArg.minHeadroom === undefined || isMinimum(valueArg.minHeadroom));
|
|
120
|
+
|
|
121
|
+
const isWindow = (valueArg: unknown): boolean => isRecord(valueArg)
|
|
122
|
+
&& hasExactKeys(valueArg, ['name', 'durationSeconds', 'usedPercent', 'estimated', 'resetAt'])
|
|
123
|
+
&& typeof valueArg.name === 'string' && valueArg.name.length <= 128
|
|
124
|
+
&& isPercent(valueArg.durationSeconds) && isPercent(valueArg.usedPercent) && typeof valueArg.estimated === 'boolean'
|
|
125
|
+
&& isTimeOrNull(valueArg.resetAt);
|
|
126
|
+
|
|
127
|
+
const isMember = (valueArg: unknown): boolean => isRecord(valueArg)
|
|
128
|
+
&& hasExactKeys(valueArg, ['accountId', 'plan', 'state', 'headroom', 'binding', 'growsAt', 'growsTo', 'recheckAt'])
|
|
129
|
+
&& typeof valueArg.accountId === 'string' && /^[a-f0-9]{64}$/.test(valueArg.accountId)
|
|
130
|
+
&& typeof valueArg.plan === 'string' && valueArg.plan.length > 0 && valueArg.plan.length <= 128
|
|
131
|
+
&& proxyCapacityMemberStates.includes(valueArg.state as TProxyCapacityMemberState)
|
|
132
|
+
&& isPercent(valueArg.headroom) && valueArg.headroom >= 0
|
|
133
|
+
&& (valueArg.binding === null || isWindow(valueArg.binding))
|
|
134
|
+
&& isTimeOrNull(valueArg.growsAt) && (valueArg.growsAt === null) === (valueArg.growsTo === null)
|
|
135
|
+
&& (valueArg.growsTo === null || isPercent(valueArg.growsTo))
|
|
136
|
+
&& isTimeOrNull(valueArg.recheckAt);
|
|
137
|
+
|
|
138
|
+
const isVerdict = (valueArg: unknown): boolean => {
|
|
139
|
+
if (!isRecord(valueArg)) return false;
|
|
140
|
+
if (valueArg.state === 'open') return hasExactKeys(valueArg, ['state']);
|
|
141
|
+
return valueArg.state === 'wait' && hasExactKeys(valueArg, ['state', 'reason', 'until', 'seconds'])
|
|
142
|
+
&& proxyCapacityWaitReasons.includes(valueArg.reason as TProxyCapacityWaitReason)
|
|
143
|
+
&& (valueArg.until === null
|
|
144
|
+
? valueArg.seconds === null
|
|
145
|
+
: isTime(valueArg.until) && typeof valueArg.seconds === 'number' && Number.isSafeInteger(valueArg.seconds)
|
|
146
|
+
&& valueArg.seconds >= 0);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const isPool = (valueArg: unknown): boolean => isRecord(valueArg)
|
|
150
|
+
&& hasExactKeys(valueArg, ['provider', 'minHeadroom', 'room', 'members', 'last429At', 'verdict'])
|
|
151
|
+
&& proxyCapacityProviders.includes(valueArg.provider as TProxyCapacityProvider)
|
|
152
|
+
&& isMinimum(valueArg.minHeadroom) && isPercent(valueArg.room) && valueArg.room >= 0
|
|
153
|
+
&& Array.isArray(valueArg.members) && valueArg.members.length <= maximumMembers && valueArg.members.every(isMember)
|
|
154
|
+
&& isTimeOrNull(valueArg.last429At) && isVerdict(valueArg.verdict);
|
|
155
|
+
|
|
156
|
+
/** Whether a value is a capacity answer as `IProxyCapacityAnswer` states it, and nothing else. */
|
|
157
|
+
export const isProxyCapacityAnswer = (valueArg: unknown): valueArg is IProxyCapacityAnswer => isRecord(valueArg)
|
|
158
|
+
&& hasExactKeys(valueArg, ['at', 'observedSince', 'pools'])
|
|
159
|
+
&& isTime(valueArg.at) && isTime(valueArg.observedSince)
|
|
160
|
+
&& Array.isArray(valueArg.pools) && valueArg.pools.length >= 1 && valueArg.pools.length <= proxyCapacityProviders.length
|
|
161
|
+
&& valueArg.pools.every(isPool)
|
|
162
|
+
&& new Set(valueArg.pools.map((poolArg) => (poolArg as IProxyCapacityPool).provider)).size === valueArg.pools.length;
|