@certalis/agent-smith 0.1.13 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -11
- package/dist/bin.js +303 -38
- package/dist/client-launcher.js +2295 -0
- package/dist/edge.js +1 -0
- package/dist/https.js +9 -8
- package/dist/pool.js +151 -29
- package/dist/post-processor.worker.js +30 -9
- package/dist/server.js +150 -28
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -238,9 +238,11 @@ var init_errors = __esm(() => {
|
|
|
238
238
|
};
|
|
239
239
|
ServiceUnavailableError = class ServiceUnavailableError extends AppError {
|
|
240
240
|
service;
|
|
241
|
-
|
|
241
|
+
retryAfterSeconds;
|
|
242
|
+
constructor(message, service, retryAfterSeconds) {
|
|
242
243
|
super(message, "SERVICE_UNAVAILABLE", 503, { service });
|
|
243
244
|
this.service = service;
|
|
245
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
244
246
|
}
|
|
245
247
|
};
|
|
246
248
|
});
|
|
@@ -1105,7 +1107,8 @@ function errorResponse(error) {
|
|
|
1105
1107
|
}
|
|
1106
1108
|
if (error instanceof Error && "statusCode" in error && typeof error.statusCode === "number") {
|
|
1107
1109
|
const details = "context" in error && error.context !== undefined ? { details: error.context } : {};
|
|
1108
|
-
|
|
1110
|
+
const retryAfter = error.statusCode === 503 && "retryAfterSeconds" in error && typeof error.retryAfterSeconds === "number" && Number.isSafeInteger(error.retryAfterSeconds) && error.retryAfterSeconds > 0 ? { "Retry-After": String(error.retryAfterSeconds) } : undefined;
|
|
1111
|
+
return jsonResponse({ error: error.message, ...details }, error.statusCode, retryAfter);
|
|
1109
1112
|
}
|
|
1110
1113
|
const message = error instanceof Error ? error.message : "Internal server error";
|
|
1111
1114
|
const status = 500;
|
|
@@ -1931,7 +1934,7 @@ function utilization(value) {
|
|
|
1931
1934
|
const raw = number(value.used_percentage ?? value.usedPercentage ?? value.percent ?? value.utilization);
|
|
1932
1935
|
if (raw === null)
|
|
1933
1936
|
return null;
|
|
1934
|
-
return raw
|
|
1937
|
+
return raw / 100;
|
|
1935
1938
|
}
|
|
1936
1939
|
function resetAt(value) {
|
|
1937
1940
|
const raw = value.resets_at ?? value.reset_at ?? value.resetAt;
|
|
@@ -4006,6 +4009,9 @@ function sanitizeClientRouting(value) {
|
|
|
4006
4009
|
if (typeof value.proxy_key_file === "string" && (value.proxy_key_file.startsWith("/") || value.proxy_key_file.startsWith("~/")) && value.proxy_key_file.length <= 1024) {
|
|
4007
4010
|
client2.proxy_key_file = value.proxy_key_file;
|
|
4008
4011
|
}
|
|
4012
|
+
if (typeof value.keep_claude_login === "boolean") {
|
|
4013
|
+
client2.keep_claude_login = value.keep_claude_login;
|
|
4014
|
+
}
|
|
4009
4015
|
return Object.keys(client2).length > 0 ? client2 : undefined;
|
|
4010
4016
|
}
|
|
4011
4017
|
function sanitizeProviderModels(value) {
|
|
@@ -4096,6 +4102,26 @@ function sanitizeTeamRouting(value) {
|
|
|
4096
4102
|
}
|
|
4097
4103
|
return result;
|
|
4098
4104
|
}
|
|
4105
|
+
function readClientRouting(configPath = resolveConfigPath()) {
|
|
4106
|
+
let data;
|
|
4107
|
+
try {
|
|
4108
|
+
data = JSON.parse(readFileSync(configPath, "utf8"));
|
|
4109
|
+
} catch {
|
|
4110
|
+
throw new Error("Agent Smith client config could not be read");
|
|
4111
|
+
}
|
|
4112
|
+
if (!isRecord(data) || data.client !== undefined && !isRecord(data.client) || isRecord(data.client) && data.client.enabled !== undefined && typeof data.client.enabled !== "boolean") {
|
|
4113
|
+
throw new Error("Agent Smith client config is invalid");
|
|
4114
|
+
}
|
|
4115
|
+
return clientRouting(sanitizeClientRouting(data.client));
|
|
4116
|
+
}
|
|
4117
|
+
function clientRouting(client2) {
|
|
4118
|
+
return {
|
|
4119
|
+
enabled: client2?.enabled === true,
|
|
4120
|
+
...client2?.base_url && { baseUrl: client2.base_url },
|
|
4121
|
+
...client2?.proxy_key_file && { proxyKeyFile: client2.proxy_key_file },
|
|
4122
|
+
...client2?.keep_claude_login === true && { keepClaudeLogin: true }
|
|
4123
|
+
};
|
|
4124
|
+
}
|
|
4099
4125
|
var log6, PROVIDER_STRATEGIES, BUILTIN_PROVIDER_CONFIGS, BUILTIN_PROVIDER_CONFIG_BY_NAME, PROVIDER_NAME_PATTERN, TEAM_ROUTING_DEFAULTS, MAX_ENROLLMENT_EMAILS = 200, MAX_ALERT_VALUE_LENGTH = 512, Config;
|
|
4100
4126
|
var init_src7 = __esm(() => {
|
|
4101
4127
|
init_src2();
|
|
@@ -4298,14 +4324,10 @@ var init_src7 = __esm(() => {
|
|
|
4298
4324
|
};
|
|
4299
4325
|
}
|
|
4300
4326
|
getClientRouting() {
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
|
|
4305
|
-
...client2?.proxy_key_file && {
|
|
4306
|
-
proxyKeyFile: client2.proxy_key_file
|
|
4307
|
-
}
|
|
4308
|
-
};
|
|
4327
|
+
return clientRouting(this.data.client);
|
|
4328
|
+
}
|
|
4329
|
+
setClientRoutingEnabled(enabled) {
|
|
4330
|
+
this.persistStrictly("client", { ...this.data.client, enabled });
|
|
4309
4331
|
}
|
|
4310
4332
|
getBlockWhenAllFull() {
|
|
4311
4333
|
return this.data.block_when_all_full !== false;
|
|
@@ -8110,6 +8132,18 @@ class APIRouter {
|
|
|
8110
8132
|
this.registerHandlers();
|
|
8111
8133
|
}
|
|
8112
8134
|
registerHandlers() {
|
|
8135
|
+
if (this.context.deploymentDrain) {
|
|
8136
|
+
const control = this.context.deploymentDrain;
|
|
8137
|
+
for (const [method, action] of [
|
|
8138
|
+
["GET", "status"],
|
|
8139
|
+
["POST", "start"],
|
|
8140
|
+
["DELETE", "resume"]
|
|
8141
|
+
]) {
|
|
8142
|
+
this.staticHandlers.set(`${method}:/api/drain`, () => Response.json(control(action), {
|
|
8143
|
+
headers: { "cache-control": "no-store" }
|
|
8144
|
+
}));
|
|
8145
|
+
}
|
|
8146
|
+
}
|
|
8113
8147
|
if (this.enrollment) {
|
|
8114
8148
|
this.staticHandlers.set("POST:/api/enrollment-invitations", this.enrollment.issue);
|
|
8115
8149
|
this.staticHandlers.set("POST:/api/enrollment-emails", this.enrollment.emails);
|
|
@@ -8144,6 +8178,9 @@ class APIRouter {
|
|
|
8144
8178
|
this.staticHandlers.set("GET:/api/stats", () => statsHandler());
|
|
8145
8179
|
this.staticHandlers.set("GET:/api/status", () => fleetStatusHandler());
|
|
8146
8180
|
this.staticHandlers.set("GET:/api/pool-status", (_req, url) => {
|
|
8181
|
+
if (this.context.deploymentDrain?.("status").draining) {
|
|
8182
|
+
return errorResponse(new ServiceUnavailableError("Pool is draining for deployment", undefined, 30));
|
|
8183
|
+
}
|
|
8147
8184
|
const provider = url.searchParams.get("provider") ?? "";
|
|
8148
8185
|
const model = url.searchParams.get("model") ?? undefined;
|
|
8149
8186
|
if (!isAccountProvider(provider) || !getProviders().includes(provider) || model !== undefined && (model.trim().length === 0 || model.length > 200)) {
|
|
@@ -9833,6 +9870,15 @@ var init_proxy_operations = __esm(() => {
|
|
|
9833
9870
|
});
|
|
9834
9871
|
|
|
9835
9872
|
// ../../packages/proxy/src/handlers/proxy-attempts.ts
|
|
9873
|
+
function admissionRetrySeconds(req) {
|
|
9874
|
+
if (req.signal.aborted)
|
|
9875
|
+
return;
|
|
9876
|
+
const count = req.headers.get("x-stainless-retry-count") ?? "0";
|
|
9877
|
+
if (!/^\d+$/.test(count))
|
|
9878
|
+
return 1;
|
|
9879
|
+
const ordinal = Number(count);
|
|
9880
|
+
return ordinal === 0 ? 1 : ordinal === 1 ? 10 : 30;
|
|
9881
|
+
}
|
|
9836
9882
|
function createBodyStream(body) {
|
|
9837
9883
|
if (!body)
|
|
9838
9884
|
return;
|
|
@@ -9892,7 +9938,7 @@ async function attemptAccount(request2, account2, failoverAttempts) {
|
|
|
9892
9938
|
permit = request2.ctx.runtime.teamRouting.enabled ? await admission?.acquire(account2.id, request2.ctx.runtime.teamRouting, request2.req.signal) : undefined;
|
|
9893
9939
|
} catch (error) {
|
|
9894
9940
|
if (error instanceof TeamAdmissionError) {
|
|
9895
|
-
throw new ServiceUnavailableError(error.message, request2.ctx.providerName);
|
|
9941
|
+
throw new ServiceUnavailableError(error.message, request2.ctx.providerName, admissionRetrySeconds(request2.req));
|
|
9896
9942
|
}
|
|
9897
9943
|
throw error;
|
|
9898
9944
|
}
|
|
@@ -11863,7 +11909,7 @@ function exposureFailure(req, url, server, config) {
|
|
|
11863
11909
|
if (!isSameOriginManagementRequest(req, url)) {
|
|
11864
11910
|
return jsonError(403, "Cross-origin management requests are not allowed");
|
|
11865
11911
|
}
|
|
11866
|
-
if (MUTATING_METHODS.has(req.method)) {
|
|
11912
|
+
if (url.pathname === "/api/drain" || MUTATING_METHODS.has(req.method)) {
|
|
11867
11913
|
if (role !== "admin") {
|
|
11868
11914
|
return jsonError(401, "An admin key is required for management writes");
|
|
11869
11915
|
}
|
|
@@ -11920,7 +11966,7 @@ function wireStrategyHotReload(config, log26, dbOps, proxyContext, runtimeConfig
|
|
|
11920
11966
|
}
|
|
11921
11967
|
});
|
|
11922
11968
|
}
|
|
11923
|
-
function bootstrapRuntime(port, serverLog) {
|
|
11969
|
+
function bootstrapRuntime(port, serverLog, deploymentDrain) {
|
|
11924
11970
|
container.registerInstance(SERVICE_KEYS.Config, new Config);
|
|
11925
11971
|
container.registerInstance(SERVICE_KEYS.Logger, serverLog);
|
|
11926
11972
|
const config = container.resolve(SERVICE_KEYS.Config);
|
|
@@ -11939,6 +11985,7 @@ function bootstrapRuntime(port, serverLog) {
|
|
|
11939
11985
|
container.registerInstance(SERVICE_KEYS.PricingLogger, pricingLogger);
|
|
11940
11986
|
setPricingLogger(pricingLogger);
|
|
11941
11987
|
const apiRouter = new APIRouter({
|
|
11988
|
+
deploymentDrain,
|
|
11942
11989
|
enrollmentOrigin: process.env.AGENT_SMITH_ENROLLMENT_ORIGIN,
|
|
11943
11990
|
config,
|
|
11944
11991
|
dbOps,
|
|
@@ -12099,6 +12146,48 @@ var init_dashboard_assets = __esm(() => {
|
|
|
12099
12146
|
init_src2();
|
|
12100
12147
|
});
|
|
12101
12148
|
|
|
12149
|
+
// ../../packages/runtime-server/src/deployment-drain.ts
|
|
12150
|
+
class DeploymentDrain {
|
|
12151
|
+
activity;
|
|
12152
|
+
now;
|
|
12153
|
+
expiresAt = null;
|
|
12154
|
+
stopping = false;
|
|
12155
|
+
log = new Logger("DeploymentDrain");
|
|
12156
|
+
constructor(activity, now = () => Date.now()) {
|
|
12157
|
+
this.activity = activity;
|
|
12158
|
+
this.now = now;
|
|
12159
|
+
}
|
|
12160
|
+
isDraining = () => {
|
|
12161
|
+
if (this.expiresAt !== null && this.now() >= this.expiresAt) {
|
|
12162
|
+
this.expiresAt = null;
|
|
12163
|
+
this.log.info("Deployment pause expired; accepting new inference");
|
|
12164
|
+
}
|
|
12165
|
+
return this.stopping || this.expiresAt !== null;
|
|
12166
|
+
};
|
|
12167
|
+
control = (action) => {
|
|
12168
|
+
if (action === "start" && !this.stopping) {
|
|
12169
|
+
if (!this.isDraining())
|
|
12170
|
+
this.log.info("Deployment pause started");
|
|
12171
|
+
this.expiresAt = this.now() + 120000;
|
|
12172
|
+
} else if (action === "resume" && !this.stopping) {
|
|
12173
|
+
this.expiresAt = null;
|
|
12174
|
+
this.log.info("Deployment pause cancelled; accepting new inference");
|
|
12175
|
+
}
|
|
12176
|
+
return {
|
|
12177
|
+
draining: this.isDraining(),
|
|
12178
|
+
expiresAt: this.expiresAt,
|
|
12179
|
+
...this.activity()
|
|
12180
|
+
};
|
|
12181
|
+
};
|
|
12182
|
+
shutdown() {
|
|
12183
|
+
this.stopping = true;
|
|
12184
|
+
this.expiresAt = null;
|
|
12185
|
+
}
|
|
12186
|
+
}
|
|
12187
|
+
var init_deployment_drain = __esm(() => {
|
|
12188
|
+
init_src4();
|
|
12189
|
+
});
|
|
12190
|
+
|
|
12102
12191
|
// ../../packages/runtime-server/src/oauth-token.ts
|
|
12103
12192
|
async function forwardOAuthTokenRequest(req, body) {
|
|
12104
12193
|
const headers3 = new Headers(req.headers);
|
|
@@ -12147,7 +12236,8 @@ function createServerFetchHandler({
|
|
|
12147
12236
|
handleProxyRequest = handleProxy,
|
|
12148
12237
|
handleWebSocketUpgrade = handleWebSocketUpgradeRequest,
|
|
12149
12238
|
handleOAuthTokenRequest = forwardOAuthTokenRequest,
|
|
12150
|
-
serveDashboardAsset = serveDashboardRoute
|
|
12239
|
+
serveDashboardAsset = serveDashboardRoute,
|
|
12240
|
+
isDeploymentDraining = () => false
|
|
12151
12241
|
}) {
|
|
12152
12242
|
return async (req, server) => {
|
|
12153
12243
|
const url = new URL(req.url);
|
|
@@ -12175,6 +12265,9 @@ function createServerFetchHandler({
|
|
|
12175
12265
|
}
|
|
12176
12266
|
const isProviderRoute = url.pathname === "/v1" || url.pathname.startsWith("/v1/") || url.pathname.startsWith("/tc-acct/");
|
|
12177
12267
|
if (isProviderRoute) {
|
|
12268
|
+
if (isDeploymentDraining()) {
|
|
12269
|
+
return errorResponse(new ServiceUnavailableError("Pool is draining for deployment", undefined, 30));
|
|
12270
|
+
}
|
|
12178
12271
|
if (server) {
|
|
12179
12272
|
const websocketResponse = await handleWebSocketUpgrade(req, url, proxyContext, server);
|
|
12180
12273
|
if (websocketResponse) {
|
|
@@ -12289,17 +12382,38 @@ async function stopServerRuntime() {
|
|
|
12289
12382
|
const errors2 = [];
|
|
12290
12383
|
const activeServer = serverInstance;
|
|
12291
12384
|
const activeUnixServer = unixServerInstance;
|
|
12292
|
-
|
|
12293
|
-
|
|
12294
|
-
|
|
12295
|
-
await activeUnixServer?.stop(true);
|
|
12296
|
-
} catch (error) {
|
|
12297
|
-
errors2.push(toError("Failed to stop Unix server", error));
|
|
12298
|
-
}
|
|
12385
|
+
deploymentDrain?.shutdown();
|
|
12386
|
+
const listeners = [activeUnixServer, activeServer].filter((listener) => listener !== null);
|
|
12387
|
+
const shutdownDeadline = Date.now() + 25000;
|
|
12299
12388
|
try {
|
|
12300
|
-
await
|
|
12301
|
-
|
|
12302
|
-
|
|
12389
|
+
await new Promise((resolve3) => {
|
|
12390
|
+
let timer;
|
|
12391
|
+
const finish = () => {
|
|
12392
|
+
clearInterval(timer);
|
|
12393
|
+
resolve3();
|
|
12394
|
+
};
|
|
12395
|
+
const check = () => {
|
|
12396
|
+
const pending = listeners.some((listener) => listener.pendingRequests > 0 || listener.pendingWebSockets > 0);
|
|
12397
|
+
if (!pending)
|
|
12398
|
+
finish();
|
|
12399
|
+
else if (Date.now() >= shutdownDeadline) {
|
|
12400
|
+
lifecycleLog.warn("Shutdown grace expired; closing remaining connections");
|
|
12401
|
+
finish();
|
|
12402
|
+
}
|
|
12403
|
+
};
|
|
12404
|
+
checkShutdownDrain = check;
|
|
12405
|
+
timer = setInterval(check, 50);
|
|
12406
|
+
check();
|
|
12407
|
+
});
|
|
12408
|
+
const stopped = await Promise.allSettled(listeners.map((listener) => listener.stop(true)));
|
|
12409
|
+
for (const result of stopped) {
|
|
12410
|
+
if (result.status === "rejected")
|
|
12411
|
+
errors2.push(toError("Failed to stop listener", result.reason));
|
|
12412
|
+
}
|
|
12413
|
+
} finally {
|
|
12414
|
+
checkShutdownDrain = undefined;
|
|
12415
|
+
serverInstance = null;
|
|
12416
|
+
unixServerInstance = null;
|
|
12303
12417
|
}
|
|
12304
12418
|
stopRetentionMaintenance();
|
|
12305
12419
|
try {
|
|
@@ -12347,6 +12461,11 @@ function startServer(options) {
|
|
|
12347
12461
|
if (withDashboard) {
|
|
12348
12462
|
loadDashboardAssets();
|
|
12349
12463
|
}
|
|
12464
|
+
const drain = new DeploymentDrain(() => ({
|
|
12465
|
+
activeRequests: Math.max(0, (serverInstance?.pendingRequests ?? 0) + (unixServerInstance?.pendingRequests ?? 0) - 1),
|
|
12466
|
+
activeWebSockets: (serverInstance?.pendingWebSockets ?? 0) + (unixServerInstance?.pendingWebSockets ?? 0)
|
|
12467
|
+
}));
|
|
12468
|
+
deploymentDrain = drain;
|
|
12350
12469
|
const {
|
|
12351
12470
|
config,
|
|
12352
12471
|
dbOps,
|
|
@@ -12356,13 +12475,14 @@ function startServer(options) {
|
|
|
12356
12475
|
proxyContext,
|
|
12357
12476
|
providerRegistry,
|
|
12358
12477
|
runtimeConfig: runtimeConfig2
|
|
12359
|
-
} = bootstrapRuntime(port, serverLog);
|
|
12478
|
+
} = bootstrapRuntime(port, serverLog, drain.control);
|
|
12360
12479
|
stopRetentionJob = runStartupMaintenance(config, dbOps);
|
|
12361
12480
|
const fetchHandler = createServerFetchHandler({
|
|
12362
12481
|
apiRouter,
|
|
12363
12482
|
exposureConfig,
|
|
12364
12483
|
proxyContext,
|
|
12365
|
-
withDashboard
|
|
12484
|
+
withDashboard,
|
|
12485
|
+
isDeploymentDraining: drain.isDraining
|
|
12366
12486
|
});
|
|
12367
12487
|
try {
|
|
12368
12488
|
if (options?.additionalUnixSocket) {
|
|
@@ -12401,6 +12521,7 @@ function startServer(options) {
|
|
|
12401
12521
|
advanceRoutingClock: () => {
|
|
12402
12522
|
proxyContext.teamAdmission?.advance();
|
|
12403
12523
|
proxyContext.quotaHold?.advance();
|
|
12524
|
+
checkShutdownDrain?.();
|
|
12404
12525
|
}
|
|
12405
12526
|
};
|
|
12406
12527
|
}
|
|
@@ -12415,7 +12536,7 @@ async function handleGracefulShutdown(signal) {
|
|
|
12415
12536
|
process.exit(1);
|
|
12416
12537
|
}
|
|
12417
12538
|
}
|
|
12418
|
-
var serverLog, lifecycleLog, serverInstance = null, unixServerInstance = null, stopRetentionJob = null, serverStopPromise = null;
|
|
12539
|
+
var serverLog, lifecycleLog, serverInstance = null, unixServerInstance = null, stopRetentionJob = null, serverStopPromise = null, deploymentDrain, checkShutdownDrain;
|
|
12419
12540
|
var init_src11 = __esm(() => {
|
|
12420
12541
|
init_src9();
|
|
12421
12542
|
init_src2();
|
|
@@ -12424,6 +12545,7 @@ var init_src11 = __esm(() => {
|
|
|
12424
12545
|
init_src10();
|
|
12425
12546
|
init_bootstrap_runtime();
|
|
12426
12547
|
init_dashboard_assets();
|
|
12548
|
+
init_deployment_drain();
|
|
12427
12549
|
init_fetch_handler();
|
|
12428
12550
|
init_startup_banner();
|
|
12429
12551
|
init_startup_maintenance();
|
package/package.json
CHANGED