tuxevil-rotator 3.0.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.
Files changed (142) hide show
  1. package/CHANGELOG.md +773 -0
  2. package/LICENSE +21 -0
  3. package/README.md +341 -0
  4. package/bin/pi-antigravity-rotator.js +27 -0
  5. package/bin/tuxevil-rotator.js +5 -0
  6. package/package.json +63 -0
  7. package/src/account-store.ts +542 -0
  8. package/src/admin-auth.ts +126 -0
  9. package/src/body-limit.ts +52 -0
  10. package/src/cli.ts +293 -0
  11. package/src/compat/cache.ts +38 -0
  12. package/src/compat/model-specs.ts +82 -0
  13. package/src/compat/schema-sanitizer.ts +284 -0
  14. package/src/compat.ts +2357 -0
  15. package/src/compression/index.ts +92 -0
  16. package/src/compression/lite.ts +210 -0
  17. package/src/compression/preservation.ts +183 -0
  18. package/src/compression/rtk/command-detector.ts +170 -0
  19. package/src/compression/rtk/deduplicator.ts +39 -0
  20. package/src/compression/rtk/filter-loader.ts +84 -0
  21. package/src/compression/rtk/filter-types.ts +25 -0
  22. package/src/compression/rtk/filters/aws.json +34 -0
  23. package/src/compression/rtk/filters/biome.json +35 -0
  24. package/src/compression/rtk/filters/build-eslint.json +37 -0
  25. package/src/compression/rtk/filters/build-typescript.json +33 -0
  26. package/src/compression/rtk/filters/build-vite.json +41 -0
  27. package/src/compression/rtk/filters/build-webpack.json +34 -0
  28. package/src/compression/rtk/filters/bundle-install.json +35 -0
  29. package/src/compression/rtk/filters/composer.json +81 -0
  30. package/src/compression/rtk/filters/curl.json +34 -0
  31. package/src/compression/rtk/filters/df.json +34 -0
  32. package/src/compression/rtk/filters/docker-build.json +78 -0
  33. package/src/compression/rtk/filters/docker-logs.json +42 -0
  34. package/src/compression/rtk/filters/docker-ps.json +33 -0
  35. package/src/compression/rtk/filters/dotnet.json +47 -0
  36. package/src/compression/rtk/filters/du.json +33 -0
  37. package/src/compression/rtk/filters/error-stacktrace.json +41 -0
  38. package/src/compression/rtk/filters/gcloud.json +34 -0
  39. package/src/compression/rtk/filters/generic-output.json +32 -0
  40. package/src/compression/rtk/filters/gh.json +98 -0
  41. package/src/compression/rtk/filters/git-branch.json +45 -0
  42. package/src/compression/rtk/filters/git-diff.json +33 -0
  43. package/src/compression/rtk/filters/git-log.json +33 -0
  44. package/src/compression/rtk/filters/git-status.json +40 -0
  45. package/src/compression/rtk/filters/golangci-lint.json +34 -0
  46. package/src/compression/rtk/filters/gradle.json +47 -0
  47. package/src/compression/rtk/filters/json-output.json +40 -0
  48. package/src/compression/rtk/filters/kubectl.json +99 -0
  49. package/src/compression/rtk/filters/make.json +38 -0
  50. package/src/compression/rtk/filters/mypy.json +34 -0
  51. package/src/compression/rtk/filters/npm-audit.json +43 -0
  52. package/src/compression/rtk/filters/npm-install.json +41 -0
  53. package/src/compression/rtk/filters/nx.json +34 -0
  54. package/src/compression/rtk/filters/pip.json +41 -0
  55. package/src/compression/rtk/filters/playwright.json +42 -0
  56. package/src/compression/rtk/filters/poetry-install.json +35 -0
  57. package/src/compression/rtk/filters/prettier.json +40 -0
  58. package/src/compression/rtk/filters/ps.json +34 -0
  59. package/src/compression/rtk/filters/rsync.json +33 -0
  60. package/src/compression/rtk/filters/rubocop.json +34 -0
  61. package/src/compression/rtk/filters/ruff.json +40 -0
  62. package/src/compression/rtk/filters/shell-find.json +33 -0
  63. package/src/compression/rtk/filters/shell-grep.json +37 -0
  64. package/src/compression/rtk/filters/shell-ls.json +33 -0
  65. package/src/compression/rtk/filters/ssh.json +43 -0
  66. package/src/compression/rtk/filters/systemctl-status.json +43 -0
  67. package/src/compression/rtk/filters/terraform-plan.json +34 -0
  68. package/src/compression/rtk/filters/test-cargo.json +42 -0
  69. package/src/compression/rtk/filters/test-go.json +41 -0
  70. package/src/compression/rtk/filters/test-jest.json +43 -0
  71. package/src/compression/rtk/filters/test-pytest.json +41 -0
  72. package/src/compression/rtk/filters/test-vitest.json +43 -0
  73. package/src/compression/rtk/filters/tofu-plan.json +34 -0
  74. package/src/compression/rtk/filters/turbo.json +34 -0
  75. package/src/compression/rtk/filters/uv-sync.json +35 -0
  76. package/src/compression/rtk/filters/wget.json +34 -0
  77. package/src/compression/rtk/index.ts +103 -0
  78. package/src/compression/rtk/line-filter.ts +124 -0
  79. package/src/compression/rtk/smart-truncate.ts +71 -0
  80. package/src/config-defaults.ts +93 -0
  81. package/src/config-normalize.ts +48 -0
  82. package/src/config-storage.ts +21 -0
  83. package/src/dashboard.ts +1169 -0
  84. package/src/db-store.ts +188 -0
  85. package/src/doctor.ts +115 -0
  86. package/src/env.ts +15 -0
  87. package/src/exposure.ts +17 -0
  88. package/src/fetch-with-retry.ts +93 -0
  89. package/src/idempotency.ts +137 -0
  90. package/src/index.ts +267 -0
  91. package/src/key-auth.ts +159 -0
  92. package/src/key-migrations.ts +84 -0
  93. package/src/logger.ts +92 -0
  94. package/src/login.ts +48 -0
  95. package/src/notification-poller.ts +155 -0
  96. package/src/onboarding.ts +754 -0
  97. package/src/paths.ts +153 -0
  98. package/src/providers/adapter.ts +131 -0
  99. package/src/providers/credential-helpers.ts +72 -0
  100. package/src/providers/google-antigravity/code-assist.ts +161 -0
  101. package/src/providers/google-antigravity/credentials.ts +24 -0
  102. package/src/providers/google-antigravity/forward.ts +424 -0
  103. package/src/providers/google-antigravity/index.ts +168 -0
  104. package/src/providers/google-antigravity/login.ts +285 -0
  105. package/src/providers/google-antigravity/oauth.ts +314 -0
  106. package/src/providers/google-antigravity/quota.ts +152 -0
  107. package/src/providers/google-antigravity/translators.ts +1663 -0
  108. package/src/providers/ollama/api-key-validation.ts +59 -0
  109. package/src/providers/ollama/credentials.ts +28 -0
  110. package/src/providers/ollama/forward.ts +306 -0
  111. package/src/providers/ollama/index.ts +85 -0
  112. package/src/providers/ollama/login.ts +55 -0
  113. package/src/providers/ollama/prediction.ts +188 -0
  114. package/src/providers/ollama/quota-poll-store.ts +160 -0
  115. package/src/providers/ollama/quota.ts +189 -0
  116. package/src/providers/ollama/translators.ts +256 -0
  117. package/src/providers/ollama/usage-windows.ts +48 -0
  118. package/src/providers/proxy-dispatcher.ts +269 -0
  119. package/src/providers/registry.ts +70 -0
  120. package/src/proxy.ts +2446 -0
  121. package/src/rate-limit-parser.ts +126 -0
  122. package/src/response-headers.ts +98 -0
  123. package/src/responses-store.ts +159 -0
  124. package/src/rotator.ts +3614 -0
  125. package/src/settings-repository.ts +327 -0
  126. package/src/spend-logger.ts +696 -0
  127. package/src/static/dashboard-keys.js +384 -0
  128. package/src/static/dashboard-logs.js +797 -0
  129. package/src/static/dashboard.css +2740 -0
  130. package/src/static/dashboard.js +3177 -0
  131. package/src/storage.ts +105 -0
  132. package/src/systemd-env.ts +214 -0
  133. package/src/telemetry.ts +384 -0
  134. package/src/token-encryption.ts +199 -0
  135. package/src/types.ts +897 -0
  136. package/src/validators.ts +218 -0
  137. package/src/version-check.ts +225 -0
  138. package/src/virtual-keys.ts +312 -0
  139. package/tools/telemetry-receiver/README.md +147 -0
  140. package/tools/telemetry-receiver/package.json +13 -0
  141. package/tools/telemetry-receiver/receiver.js +2334 -0
  142. package/tuxevil-rotator_logo.png +0 -0
@@ -0,0 +1,71 @@
1
+ export interface SmartTruncateOptions {
2
+ maxLines?: number;
3
+ maxChars?: number;
4
+ preserveHead?: number;
5
+ preserveTail?: number;
6
+ priorityPatterns?: RegExp[];
7
+ }
8
+
9
+ export function smartTruncate(
10
+ text: string,
11
+ options: SmartTruncateOptions = {},
12
+ ): {
13
+ text: string;
14
+ truncated: boolean;
15
+ droppedLines: number;
16
+ } {
17
+ const maxChars = Math.max(0, Math.floor(options.maxChars ?? 0));
18
+ const maxLines = Math.max(0, Math.floor(options.maxLines ?? 0));
19
+ const lines = text.split(/\r?\n/);
20
+ const overLineLimit = maxLines > 0 && lines.length > maxLines;
21
+ const overCharLimit = maxChars > 0 && text.length > maxChars;
22
+ if (!overLineLimit && !overCharLimit) {
23
+ return { text, truncated: false, droppedLines: 0 };
24
+ }
25
+
26
+ const preserveHead = Math.max(0, Math.floor(options.preserveHead ?? 20));
27
+ const preserveTail = Math.max(0, Math.floor(options.preserveTail ?? 20));
28
+ const priorityPatterns = options.priorityPatterns ?? [];
29
+ const priorityLines = priorityPatterns.length
30
+ ? lines.filter((line) =>
31
+ priorityPatterns.some((pattern) => pattern.test(line)),
32
+ )
33
+ : [];
34
+
35
+ const head = lines.slice(0, preserveHead);
36
+ const tail = preserveTail > 0 ? lines.slice(-preserveTail) : [];
37
+ const selected = [...head];
38
+ for (const line of priorityLines) {
39
+ if (!selected.includes(line)) selected.push(line);
40
+ }
41
+ const tailStart = lines.length - tail.length;
42
+ tail.forEach((line, offset) => {
43
+ const originalIndex = tailStart + offset;
44
+ if (originalIndex >= preserveHead && !selected.includes(line)) {
45
+ selected.push(line);
46
+ }
47
+ });
48
+
49
+ const droppedLines = Math.max(0, lines.length - selected.length);
50
+ let result = [
51
+ ...selected.slice(0, head.length),
52
+ `[rtk:truncated ${droppedLines} lines]`,
53
+ ...selected.slice(head.length),
54
+ ].join("\n");
55
+
56
+ if (maxChars > 0 && result.length > maxChars) {
57
+ const marker = "\n[rtk:truncated by chars]\n";
58
+ const budget = Math.max(0, maxChars - marker.length);
59
+ if (budget === 0) {
60
+ result = marker.slice(0, maxChars);
61
+ return { text: result, truncated: true, droppedLines };
62
+ }
63
+ const headChars = Math.ceil(budget * 0.55);
64
+ const tailChars = Math.max(0, budget - headChars);
65
+ const tailText = tailChars > 0 ? result.slice(-tailChars) : "";
66
+ result = `${result.slice(0, headChars)}${marker}${tailText}`;
67
+ if (result.length > maxChars) result = result.slice(0, maxChars);
68
+ }
69
+
70
+ return { text: result, truncated: true, droppedLines };
71
+ }
@@ -0,0 +1,93 @@
1
+ import { rotatorEnv } from "./env.js";
2
+ import { normalizeAccountConfig } from "./config-normalize.js";
3
+ import {
4
+ DEFAULT_QUOTA_POLL_INTERVAL_MS,
5
+ MAX_QUOTA_POLL_INTERVAL_MS,
6
+ MIN_QUOTA_POLL_INTERVAL_MS,
7
+ type Config,
8
+ } from "./types.js";
9
+
10
+ function safeQuotaPollIntervalMs(value: number | undefined): number {
11
+ if (
12
+ value === undefined ||
13
+ !Number.isFinite(value) ||
14
+ value < MIN_QUOTA_POLL_INTERVAL_MS ||
15
+ value > MAX_QUOTA_POLL_INTERVAL_MS
16
+ ) {
17
+ return DEFAULT_QUOTA_POLL_INTERVAL_MS;
18
+ }
19
+ return Math.floor(value);
20
+ }
21
+
22
+ function safeStreamRecoveryMaxRetries(value: number | undefined): number {
23
+ if (value === undefined || !Number.isInteger(value) || value < 0) return 2;
24
+ return value;
25
+ }
26
+
27
+ export function applyConfigDefaults(config: Config): Config {
28
+ return {
29
+ proxyPort: config.proxyPort || 51200,
30
+ bindHost: config.bindHost || rotatorEnv("BIND_HOST") || "0.0.0.0",
31
+ routingPolicy: config.routingPolicy || "timer-first",
32
+ requestsPerRotation: config.requestsPerRotation || 5,
33
+ rotateOnQuotaDrop: config.rotateOnQuotaDrop ?? 20,
34
+ quotaPollIntervalMs: safeQuotaPollIntervalMs(config.quotaPollIntervalMs),
35
+ maxConcurrentRequestsPerAccount: config.maxConcurrentRequestsPerAccount ?? 1,
36
+ maxConcurrentRequestsPerProjectModel: config.maxConcurrentRequestsPerProjectModel ?? 1,
37
+ projectCircuitBreaker429Threshold: config.projectCircuitBreaker429Threshold ?? 3,
38
+ projectCircuitBreakerWindowMs: config.projectCircuitBreakerWindowMs ?? 10 * 60 * 1000,
39
+ projectCircuitBreakerCooldownMs: config.projectCircuitBreakerCooldownMs ?? 60 * 60 * 1000,
40
+ modelCircuitBreaker429Threshold: config.modelCircuitBreaker429Threshold ?? 3,
41
+ modelCircuitBreakerCooldownMs: config.modelCircuitBreakerCooldownMs ?? 6 * 60 * 60 * 1000,
42
+ dailyAccountSlowRequests: config.dailyAccountSlowRequests ?? 250,
43
+ dailyAccountStopRequests: config.dailyAccountStopRequests ?? 350,
44
+ dailyProjectSlowRequests: config.dailyProjectSlowRequests ?? 900,
45
+ dailyProjectStopRequests: config.dailyProjectStopRequests ?? 1200,
46
+ slowModeJitterMinMs: config.slowModeJitterMinMs ?? 8_000,
47
+ slowModeJitterMaxMs: config.slowModeJitterMaxMs ?? 25_000,
48
+ protectivePauseMs: config.protectivePauseMs ?? 21600000,
49
+ useRequestCountRotationWhenQuotaUnknownOnly: config.useRequestCountRotationWhenQuotaUnknownOnly ?? true,
50
+ tokenBucketEnabled: config.tokenBucketEnabled ?? false,
51
+ tokenBucketMaxTokens: config.tokenBucketMaxTokens ?? 50,
52
+ tokenBucketRefillPerMinute: config.tokenBucketRefillPerMinute ?? 6,
53
+ tokenBucketInitialTokens: config.tokenBucketInitialTokens ?? (config.tokenBucketMaxTokens ?? 50),
54
+ idempotencyEnabled: config.idempotencyEnabled ?? true,
55
+ idempotencyWindowMs: config.idempotencyWindowMs ?? 2000,
56
+ streamRecoveryMaxRetries: safeStreamRecoveryMaxRetries(config.streamRecoveryMaxRetries),
57
+ compressionMode: config.compressionMode ?? "off",
58
+ accounts: config.accounts ? config.accounts.map((account) => ({
59
+ ...normalizeAccountConfig(account),
60
+ tier: account.tier || "unknown",
61
+ })) : [],
62
+ };
63
+ }
64
+
65
+ export function getDefaultConfig(): Config {
66
+ return applyConfigDefaults({
67
+ proxyPort: 51200,
68
+ accounts: [],
69
+ requestsPerRotation: 5,
70
+ rotateOnQuotaDrop: 20,
71
+ quotaPollIntervalMs: DEFAULT_QUOTA_POLL_INTERVAL_MS,
72
+ maxConcurrentRequestsPerAccount: 1,
73
+ maxConcurrentRequestsPerProjectModel: 1,
74
+ projectCircuitBreaker429Threshold: 3,
75
+ projectCircuitBreakerWindowMs: 10 * 60 * 1000,
76
+ projectCircuitBreakerCooldownMs: 60 * 60 * 1000,
77
+ modelCircuitBreaker429Threshold: 3,
78
+ modelCircuitBreakerCooldownMs: 6 * 60 * 60 * 1000,
79
+ dailyAccountSlowRequests: 250,
80
+ dailyAccountStopRequests: 350,
81
+ dailyProjectSlowRequests: 900,
82
+ dailyProjectStopRequests: 1200,
83
+ slowModeJitterMinMs: 8_000,
84
+ slowModeJitterMaxMs: 25_000,
85
+ protectivePauseMs: 21600000,
86
+ useRequestCountRotationWhenQuotaUnknownOnly: true,
87
+ tokenBucketEnabled: false,
88
+ tokenBucketMaxTokens: 50,
89
+ tokenBucketRefillPerMinute: 6,
90
+ tokenBucketInitialTokens: 50,
91
+ streamRecoveryMaxRetries: 2,
92
+ });
93
+ }
@@ -0,0 +1,48 @@
1
+ // Normalization of account configs into the parent-account credential model.
2
+ // Kept in its own module so both config-defaults (load path) and
3
+ // account-store (write path) can use it without import cycles.
4
+
5
+ import type { AccountConfig } from "./types.js";
6
+ import { DEFAULT_PROVIDER } from "./providers/credential-helpers.js";
7
+
8
+ /**
9
+ * Migrate a flat legacy account shape (provider/apiKey/refreshToken at the
10
+ * top level) into the parent-account model where `email` owns a list of
11
+ * per-provider credentials. Accounts that already carry `credentials` are
12
+ * returned untouched. Runs on every config load (applyConfigDefaults), so
13
+ * both the persisted DB config and legacy files normalize transparently.
14
+ */
15
+ export function normalizeAccountConfig(account: AccountConfig): AccountConfig {
16
+ if (Array.isArray(account.credentials) && account.credentials.length > 0) {
17
+ return account;
18
+ }
19
+ const provider = account.provider ?? DEFAULT_PROVIDER;
20
+ // Mirror the flat legacy fields onto the parent-account credentials and
21
+ // KEEP the top-level fields: Google reads (forward/quota/index) still
22
+ // consult them, so both shapes coexist until the runtime migrates.
23
+ if (provider === "ollama" && typeof account.apiKey === "string" && account.apiKey.trim() !== "") {
24
+ return {
25
+ ...account,
26
+ credentials: [{
27
+ provider: "ollama",
28
+ apiKey: account.apiKey,
29
+ proxyUrl: account.proxyUrl,
30
+ }],
31
+ };
32
+ }
33
+ if (provider !== "ollama" && (account.refreshToken !== undefined || account.projectId !== undefined)) {
34
+ return {
35
+ ...account,
36
+ credentials: [
37
+ {
38
+ provider: DEFAULT_PROVIDER,
39
+ refreshToken: account.refreshToken,
40
+ projectId: account.projectId,
41
+ projectSource: account.projectSource,
42
+ proxyUrl: account.proxyUrl,
43
+ },
44
+ ],
45
+ };
46
+ }
47
+ return account;
48
+ }
@@ -0,0 +1,21 @@
1
+ import type { Config } from "./types.js";
2
+ import { getCachedConfig, setCachedConfig } from "./db-store.js";
3
+ import { getDefaultConfig } from "./config-defaults.js";
4
+
5
+ export function loadConfig(): Config {
6
+ const cached = getCachedConfig();
7
+ if (cached) return cached;
8
+ return getDefaultConfig();
9
+ }
10
+
11
+ export function loadOrCreateAccountsConfig(): Config {
12
+ try {
13
+ return loadConfig();
14
+ } catch {
15
+ return getDefaultConfig();
16
+ }
17
+ }
18
+
19
+ export async function saveAccountsConfig(config: Config): Promise<void> {
20
+ await setCachedConfig(config);
21
+ }