@certalis/agent-smith 0.3.0 → 0.3.3
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 +8 -3
- package/dist/bin.js +792 -177
- package/dist/claude-companion/hooks/provider-choice.js +6 -1
- package/dist/client-launcher.js +689 -66
- package/dist/edge.js +15 -3
- package/dist/https.js +17 -4
- package/dist/pool.js +217 -86
- package/dist/post-processor.worker.js +92 -42
- package/dist/server.js +217 -86
- package/package.json +2 -1
|
@@ -1844,6 +1844,13 @@ import {
|
|
|
1844
1844
|
writeFileSync as writeFileSync2
|
|
1845
1845
|
} from "fs";
|
|
1846
1846
|
import { dirname as dirname2 } from "path";
|
|
1847
|
+
function callerWeeklyCap(caps, callerId) {
|
|
1848
|
+
if (!Object.hasOwn(caps, callerId))
|
|
1849
|
+
return;
|
|
1850
|
+
const cap = caps[callerId];
|
|
1851
|
+
const shared = typeof cap === "string" && Object.hasOwn(caps, cap) ? caps[cap] : cap;
|
|
1852
|
+
return typeof shared === "number" ? shared : undefined;
|
|
1853
|
+
}
|
|
1847
1854
|
function runtimeForProvider(runtime, provider) {
|
|
1848
1855
|
return {
|
|
1849
1856
|
...runtime,
|
|
@@ -2030,6 +2037,9 @@ function sanitizeClientRouting(value) {
|
|
|
2030
2037
|
if (typeof value.keep_claude_login === "boolean") {
|
|
2031
2038
|
client.keep_claude_login = value.keep_claude_login;
|
|
2032
2039
|
}
|
|
2040
|
+
if (typeof value.remote_control === "boolean") {
|
|
2041
|
+
client.remote_control = value.remote_control;
|
|
2042
|
+
}
|
|
2033
2043
|
if (typeof value.pool_ssh_host === "string" && POOL_SSH_HOST_PATTERN.test(value.pool_ssh_host)) {
|
|
2034
2044
|
client.pool_ssh_host = value.pool_ssh_host;
|
|
2035
2045
|
}
|
|
@@ -2145,8 +2155,9 @@ function sanitizeTeamRouting(value) {
|
|
|
2145
2155
|
if (weeklyMaxUsageByEmail)
|
|
2146
2156
|
result.weekly_max_usage_by_email = weeklyMaxUsageByEmail;
|
|
2147
2157
|
if (isRecord(value.caller_weekly_max_usage)) {
|
|
2148
|
-
const
|
|
2149
|
-
|
|
2158
|
+
const caps = value.caller_weekly_max_usage;
|
|
2159
|
+
const entries = Object.entries(caps);
|
|
2160
|
+
if (entries.length <= MAX_CALLERS && entries.every(([caller, cap]) => CALLER_ID_PATTERN.test(caller) && (isCallerWeeklyCap(cap) || sharesACallerCap(caps, cap)))) {
|
|
2150
2161
|
result.caller_weekly_max_usage = Object.fromEntries(entries);
|
|
2151
2162
|
}
|
|
2152
2163
|
}
|
|
@@ -2300,10 +2311,11 @@ function clientRouting(client) {
|
|
|
2300
2311
|
...client?.base_url && { baseUrl: client.base_url },
|
|
2301
2312
|
...client?.proxy_key_file && { proxyKeyFile: client.proxy_key_file },
|
|
2302
2313
|
...client?.keep_claude_login === true && { keepClaudeLogin: true },
|
|
2314
|
+
...client?.remote_control === false && { remoteControl: false },
|
|
2303
2315
|
...client?.pool_ssh_host && { poolSshHost: client.pool_ssh_host }
|
|
2304
2316
|
};
|
|
2305
2317
|
}
|
|
2306
|
-
var log2, PROVIDER_STRATEGIES, BUILTIN_PROVIDER_CONFIG_BY_NAME, PROVIDER_NAME_PATTERN, POOL_SSH_HOST_PATTERN, CALLER_ID_PATTERN, MAX_CALLERS = 128, POOL_DEFAULTS, MAX_ENROLLMENT_EMAILS = 200, MAX_ALERT_VALUE_LENGTH = 512, Config;
|
|
2318
|
+
var log2, PROVIDER_STRATEGIES, BUILTIN_PROVIDER_CONFIG_BY_NAME, PROVIDER_NAME_PATTERN, POOL_SSH_HOST_PATTERN, CALLER_ID_PATTERN, MAX_CALLERS = 128, isCallerWeeklyCap = (cap) => isFiniteNumber(cap) && cap > 0 && cap <= 0.95, sharesACallerCap = (caps, cap) => typeof cap === "string" && Object.hasOwn(caps, cap) && isCallerWeeklyCap(caps[cap]), POOL_DEFAULTS, MAX_ENROLLMENT_EMAILS = 200, MAX_ALERT_VALUE_LENGTH = 512, Config;
|
|
2307
2319
|
var init_src4 = __esm(() => {
|
|
2308
2320
|
init_src2();
|
|
2309
2321
|
init_src3();
|
|
@@ -2827,10 +2839,11 @@ function addPerformanceIndexes(db) {
|
|
|
2827
2839
|
`);
|
|
2828
2840
|
log3.info("Added index: idx_accounts_paused");
|
|
2829
2841
|
db.run(`
|
|
2830
|
-
CREATE INDEX IF NOT EXISTS
|
|
2831
|
-
ON requests(account_used, timestamp DESC)
|
|
2842
|
+
CREATE INDEX IF NOT EXISTS idx_requests_account_timestamp_cost
|
|
2843
|
+
ON requests(account_used, timestamp DESC, cost_usd)
|
|
2832
2844
|
`);
|
|
2833
|
-
|
|
2845
|
+
db.run("DROP INDEX IF EXISTS idx_requests_account_timestamp");
|
|
2846
|
+
log3.info("Added index: idx_requests_account_timestamp_cost");
|
|
2834
2847
|
db.run(`
|
|
2835
2848
|
CREATE INDEX IF NOT EXISTS idx_requests_cost_model
|
|
2836
2849
|
ON requests(cost_usd, model, timestamp DESC)
|
|
@@ -3221,6 +3234,13 @@ function ensureAccountExtensionColumns(db) {
|
|
|
3221
3234
|
}
|
|
3222
3235
|
}
|
|
3223
3236
|
}
|
|
3237
|
+
function backfillRequestLinkageColumnsOnce(db) {
|
|
3238
|
+
const { user_version } = db.query("PRAGMA user_version").get();
|
|
3239
|
+
if (user_version >= LINKAGE_BACKFILLED_VERSION)
|
|
3240
|
+
return;
|
|
3241
|
+
backfillRequestLinkageColumns(db);
|
|
3242
|
+
db.run(`PRAGMA user_version = ${LINKAGE_BACKFILLED_VERSION}`);
|
|
3243
|
+
}
|
|
3224
3244
|
function backfillRequestLinkageColumns(db) {
|
|
3225
3245
|
const missingCount = db.query(`
|
|
3226
3246
|
SELECT COUNT(*) AS count
|
|
@@ -3644,7 +3664,7 @@ function runMigrations(db) {
|
|
|
3644
3664
|
migrateRequestsTable(db, requestsInfo);
|
|
3645
3665
|
}
|
|
3646
3666
|
ensureRequestLinkageColumns(db);
|
|
3647
|
-
|
|
3667
|
+
backfillRequestLinkageColumnsOnce(db);
|
|
3648
3668
|
db.run("DROP TABLE IF EXISTS agent_preferences");
|
|
3649
3669
|
db.run("DROP TABLE IF EXISTS oauth_sessions");
|
|
3650
3670
|
db.run("DROP INDEX IF EXISTS idx_oauth_sessions_expires");
|
|
@@ -3660,7 +3680,7 @@ function runMigrations(db) {
|
|
|
3660
3680
|
db.run(`CREATE INDEX IF NOT EXISTS idx_requests_timestamp ON requests(timestamp DESC)`);
|
|
3661
3681
|
addPerformanceIndexes(db);
|
|
3662
3682
|
}
|
|
3663
|
-
var log5;
|
|
3683
|
+
var log5, LINKAGE_BACKFILLED_VERSION = 1;
|
|
3664
3684
|
var init_migrations = __esm(() => {
|
|
3665
3685
|
init_src3();
|
|
3666
3686
|
init_src();
|
|
@@ -4507,7 +4527,7 @@ function requestLinkageFromPayload(payload) {
|
|
|
4507
4527
|
clientSessionId: typeof trace.clientSessionId === "string" ? trace.clientSessionId : fromBodies.clientSessionId
|
|
4508
4528
|
};
|
|
4509
4529
|
}
|
|
4510
|
-
var log6, RequestRepository;
|
|
4530
|
+
var log6, ACCOUNT_COST_SINCE_SQL = "SELECT SUM(cost_usd) AS cost FROM requests WHERE account_used = ? AND timestamp >= ?", RequestRepository;
|
|
4511
4531
|
var init_request_repository = __esm(() => {
|
|
4512
4532
|
init_src3();
|
|
4513
4533
|
init_src();
|
|
@@ -4882,11 +4902,10 @@ var init_request_repository = __esm(() => {
|
|
|
4882
4902
|
return errors.map((e) => e.error_message);
|
|
4883
4903
|
}
|
|
4884
4904
|
sumAccountCostSince(accountId, since) {
|
|
4885
|
-
return this.get(
|
|
4886
|
-
|
|
4887
|
-
|
|
4888
|
-
|
|
4889
|
-
`, [accountId, since])?.cost ?? 0;
|
|
4905
|
+
return this.get(ACCOUNT_COST_SINCE_SQL, [
|
|
4906
|
+
accountId,
|
|
4907
|
+
since
|
|
4908
|
+
])?.cost ?? 0;
|
|
4890
4909
|
}
|
|
4891
4910
|
getRequestsByAccount(since) {
|
|
4892
4911
|
const whereClause = since ? "WHERE r.timestamp > ?" : "";
|
|
@@ -4910,23 +4929,26 @@ var init_request_repository = __esm(() => {
|
|
|
4910
4929
|
successRate: row.success_rate
|
|
4911
4930
|
}));
|
|
4912
4931
|
}
|
|
4913
|
-
deleteOlderThan(cutoffTs) {
|
|
4914
|
-
return this.runWithChanges(`DELETE FROM requests WHERE
|
|
4915
|
-
|
|
4916
|
-
|
|
4932
|
+
deleteOlderThan(cutoffTs, limit) {
|
|
4933
|
+
return this.runWithChanges(`DELETE FROM requests WHERE rowid IN (
|
|
4934
|
+
SELECT rowid FROM requests WHERE timestamp < ? LIMIT ?
|
|
4935
|
+
)`, [cutoffTs, limit]);
|
|
4917
4936
|
}
|
|
4918
4937
|
clear() {
|
|
4919
4938
|
return this.runWithChanges(`DELETE FROM requests`);
|
|
4920
4939
|
}
|
|
4921
|
-
|
|
4922
|
-
|
|
4923
|
-
|
|
4924
|
-
|
|
4925
|
-
|
|
4926
|
-
|
|
4927
|
-
|
|
4928
|
-
|
|
4929
|
-
|
|
4940
|
+
deleteOldestExpiredPayloads(cutoffTs, limit) {
|
|
4941
|
+
const oldest = this.query(`SELECT rp.rowid AS rowid, r.timestamp AS timestamp
|
|
4942
|
+
FROM request_payloads rp
|
|
4943
|
+
LEFT JOIN requests r ON r.id = rp.id
|
|
4944
|
+
ORDER BY rp.rowid
|
|
4945
|
+
LIMIT ?`, [limit]);
|
|
4946
|
+
const firstLive = oldest.findIndex(({ timestamp }) => timestamp !== null && timestamp >= cutoffTs);
|
|
4947
|
+
const expiredCount = firstLive === -1 ? oldest.length : firstLive;
|
|
4948
|
+
const lastExpired = oldest[expiredCount - 1];
|
|
4949
|
+
if (!lastExpired)
|
|
4950
|
+
return 0;
|
|
4951
|
+
return this.runWithChanges(`DELETE FROM request_payloads WHERE rowid <= ?`, [lastExpired.rowid]);
|
|
4930
4952
|
}
|
|
4931
4953
|
};
|
|
4932
4954
|
});
|
|
@@ -5055,6 +5077,7 @@ var init_strategy_repository = __esm(() => {
|
|
|
5055
5077
|
import { Database } from "bun:sqlite";
|
|
5056
5078
|
import { mkdirSync as mkdirSync3, statfsSync } from "fs";
|
|
5057
5079
|
import { dirname as dirname3 } from "path";
|
|
5080
|
+
import { setImmediate as yieldToEventLoop } from "timers/promises";
|
|
5058
5081
|
function freeDiskBytes(directory) {
|
|
5059
5082
|
const { bavail, bsize } = statfsSync(directory);
|
|
5060
5083
|
return bavail * bsize;
|
|
@@ -5074,7 +5097,7 @@ class DatabaseOperations {
|
|
|
5074
5097
|
conversationProviderPreferences;
|
|
5075
5098
|
strategy;
|
|
5076
5099
|
stats;
|
|
5077
|
-
constructor(dbPath) {
|
|
5100
|
+
constructor(dbPath, { migrate = true } = {}) {
|
|
5078
5101
|
const resolvedPath = dbPath ?? resolveDbPath();
|
|
5079
5102
|
this.dbPath = resolvedPath;
|
|
5080
5103
|
const dir = dirname3(resolvedPath);
|
|
@@ -5084,8 +5107,11 @@ class DatabaseOperations {
|
|
|
5084
5107
|
this.db.exec("PRAGMA journal_mode = WAL");
|
|
5085
5108
|
this.db.exec("PRAGMA busy_timeout = 5000");
|
|
5086
5109
|
this.db.exec("PRAGMA synchronous = NORMAL");
|
|
5087
|
-
|
|
5088
|
-
|
|
5110
|
+
this.db.exec(`PRAGMA journal_size_limit = ${WAL_SIZE_LIMIT_BYTES}`);
|
|
5111
|
+
if (migrate) {
|
|
5112
|
+
ensureSchema(this.db);
|
|
5113
|
+
runMigrations(this.db);
|
|
5114
|
+
}
|
|
5089
5115
|
this.accounts = new AccountRepository(this.db);
|
|
5090
5116
|
this.analytics = new AnalyticsRepository(this.db);
|
|
5091
5117
|
this.requests = new RequestRepository(this.db);
|
|
@@ -5301,18 +5327,40 @@ class DatabaseOperations {
|
|
|
5301
5327
|
getAnalytics(options) {
|
|
5302
5328
|
return this.analytics.getAnalytics(options);
|
|
5303
5329
|
}
|
|
5304
|
-
cleanupOldRequests(payloadRetentionMs, requestRetentionMs) {
|
|
5305
|
-
const
|
|
5306
|
-
const
|
|
5307
|
-
|
|
5308
|
-
|
|
5309
|
-
|
|
5310
|
-
|
|
5330
|
+
cleanupOldRequests(payloadRetentionMs, requestRetentionMs, batchRows = CLEANUP_BATCH_ROWS) {
|
|
5331
|
+
const total = { removedRequests: 0, removedPayloads: 0 };
|
|
5332
|
+
for (const batch of this.cleanupBatches(payloadRetentionMs, requestRetentionMs, batchRows)) {
|
|
5333
|
+
total.removedRequests += batch.removedRequests;
|
|
5334
|
+
total.removedPayloads += batch.removedPayloads;
|
|
5335
|
+
}
|
|
5336
|
+
return total;
|
|
5337
|
+
}
|
|
5338
|
+
async cleanupOldRequestsInBatches(payloadRetentionMs, requestRetentionMs, {
|
|
5339
|
+
batchRows = CLEANUP_BATCH_ROWS,
|
|
5340
|
+
signal
|
|
5341
|
+
} = {}) {
|
|
5342
|
+
const total = { removedRequests: 0, removedPayloads: 0 };
|
|
5343
|
+
for (const batch of this.cleanupBatches(payloadRetentionMs, requestRetentionMs, batchRows)) {
|
|
5344
|
+
total.removedRequests += batch.removedRequests;
|
|
5345
|
+
total.removedPayloads += batch.removedPayloads;
|
|
5346
|
+
await yieldToEventLoop();
|
|
5347
|
+
if (signal?.aborted)
|
|
5348
|
+
break;
|
|
5349
|
+
}
|
|
5350
|
+
return total;
|
|
5351
|
+
}
|
|
5352
|
+
*cleanupBatches(payloadRetentionMs, requestRetentionMs, batchRows) {
|
|
5353
|
+
for (;; ) {
|
|
5354
|
+
const now = Date.now();
|
|
5355
|
+
let removedRequests = 0;
|
|
5356
|
+
if (typeof requestRetentionMs === "number" && Number.isFinite(requestRetentionMs)) {
|
|
5357
|
+
removedRequests = this.requests.deleteOlderThan(now - requestRetentionMs, batchRows);
|
|
5358
|
+
}
|
|
5359
|
+
const removedPayloads = this.requests.deleteOldestExpiredPayloads(now - payloadRetentionMs, batchRows);
|
|
5360
|
+
yield { removedRequests, removedPayloads };
|
|
5361
|
+
if (removedRequests < batchRows && removedPayloads < batchRows)
|
|
5362
|
+
return;
|
|
5311
5363
|
}
|
|
5312
|
-
const removedPayloadsByAge = this.requests.deletePayloadsOlderThan(payloadCutoff);
|
|
5313
|
-
const removedOrphans = this.requests.deleteOrphanedPayloads();
|
|
5314
|
-
const removedPayloads = removedPayloadsByAge + removedOrphans;
|
|
5315
|
-
return { removedRequests, removedPayloads };
|
|
5316
5364
|
}
|
|
5317
5365
|
close() {
|
|
5318
5366
|
if (this.closed) {
|
|
@@ -5351,6 +5399,7 @@ class DatabaseOperations {
|
|
|
5351
5399
|
return this.stats;
|
|
5352
5400
|
}
|
|
5353
5401
|
}
|
|
5402
|
+
var CLEANUP_BATCH_ROWS = 50, WAL_SIZE_LIMIT_BYTES;
|
|
5354
5403
|
var init_database_operations = __esm(() => {
|
|
5355
5404
|
init_migrations();
|
|
5356
5405
|
init_paths2();
|
|
@@ -5363,6 +5412,7 @@ var init_database_operations = __esm(() => {
|
|
|
5363
5412
|
init_request_repository();
|
|
5364
5413
|
init_stats_repository();
|
|
5365
5414
|
init_strategy_repository();
|
|
5415
|
+
WAL_SIZE_LIMIT_BYTES = 64 * 1024 * 1024;
|
|
5366
5416
|
});
|
|
5367
5417
|
|
|
5368
5418
|
// ../../packages/database/src/factory.ts
|
|
@@ -5563,7 +5613,7 @@ var log7 = new Logger("PostProcessor");
|
|
|
5563
5613
|
var requests = new Map;
|
|
5564
5614
|
var tokenEncoder = get_encoding("cl100k_base");
|
|
5565
5615
|
var streamDecoder = new TextDecoder("utf-8", { fatal: false });
|
|
5566
|
-
var dbOps = new DatabaseOperations;
|
|
5616
|
+
var dbOps = new DatabaseOperations(undefined, { migrate: false });
|
|
5567
5617
|
var asyncWriter = new AsyncDbWriter;
|
|
5568
5618
|
var MAX_BUFFER_SIZE = Number(process.env.AGENT_SMITH_STREAM_USAGE_BUFFER_KB || BUFFER_SIZES.STREAM_USAGE_BUFFER_KB) * 1024;
|
|
5569
5619
|
var TIMEOUT_MS = Number(process.env.AGENT_SMITH_STREAM_TIMEOUT_MS || TIME_CONSTANTS.STREAM_TIMEOUT_DEFAULT);
|