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,152 @@
1
+ // Google Antigravity quota polling: fetch per-model quota from the internal
2
+ // fetchAvailableModels endpoint and classify timer windows.
3
+
4
+ import type {
5
+ AccountRuntime,
6
+ GoogleQuotaResponse,
7
+ ModelQuota,
8
+ } from "../../types.js";
9
+ import {
10
+ QUOTA_API_URL,
11
+ QUOTA_USER_AGENT,
12
+ QUOTA_MODEL_KEYS,
13
+ } from "../../types.js";
14
+ import { fetchWithRetry } from "../../fetch-with-retry.js";
15
+ import type { QuotaFetchContext } from "../adapter.js";
16
+ import { getAccountProxyDispatcher } from "../proxy-dispatcher.js";
17
+
18
+ /**
19
+ * Extract per-model quotas from a Google quota response, preserving the
20
+ * previously classified timer type when the reset time is unchanged.
21
+ */
22
+ export function extractQuotas(
23
+ data: GoogleQuotaResponse,
24
+ oldQuota: ModelQuota[],
25
+ ): ModelQuota[] {
26
+ const quotas: ModelQuota[] = [];
27
+ const now = Date.now();
28
+
29
+ for (const [, config] of Object.entries(QUOTA_MODEL_KEYS)) {
30
+ let modelInfo = data.models[config.key];
31
+
32
+ if (!modelInfo) {
33
+ for (const altKey of config.altKeys) {
34
+ modelInfo = data.models[altKey];
35
+ if (modelInfo) break;
36
+ }
37
+ }
38
+
39
+ if (modelInfo?.quotaInfo) {
40
+ const resetTime = modelInfo.quotaInfo.resetTime ?? null;
41
+ let timerType: ModelQuota["timerType"] = "fresh";
42
+
43
+ if (resetTime) {
44
+ const oldQ = oldQuota.find((q) => q.modelKey === config.key);
45
+ // If the resetTime is exactly the same as the previous poll, preserve
46
+ // the old timerType. A timer doesn't change its nature just because it
47
+ // gets closer to zero.
48
+ if (oldQ && oldQ.resetTime === resetTime && oldQ.timerType !== "fresh") {
49
+ timerType = oldQ.timerType;
50
+ } else {
51
+ // Brand new timer (or service restart): measure the distance to
52
+ // determine its type. < 6 hours → 5h timer, otherwise 7d.
53
+ const resetMs = new Date(resetTime).getTime();
54
+ if (resetMs > now) {
55
+ const durationMs = resetMs - now;
56
+ timerType = durationMs < 6 * 60 * 60 * 1000 ? "5h" : "7d";
57
+ }
58
+ }
59
+ }
60
+
61
+ quotas.push({
62
+ modelKey: config.key,
63
+ displayName: config.display,
64
+ percentRemaining: Math.round(
65
+ (modelInfo.quotaInfo.remainingFraction ?? 0) * 100,
66
+ ),
67
+ resetTime,
68
+ timerType,
69
+ });
70
+ }
71
+ }
72
+
73
+ return quotas;
74
+ }
75
+
76
+ /**
77
+ * Poll quota for one account and write the result into account.quota.
78
+ * Non-OK responses flag the account via the core-provided callbacks.
79
+ */
80
+ export async function fetchProviderQuota(
81
+ account: AccountRuntime,
82
+ ctx: QuotaFetchContext,
83
+ ): Promise<void> {
84
+ if (!account.accessToken) return;
85
+
86
+ try {
87
+ const response = await fetchWithRetry(QUOTA_API_URL, {
88
+ method: "POST",
89
+ headers: {
90
+ "Content-Type": "application/json",
91
+ Authorization: `Bearer ${account.accessToken}`,
92
+ "User-Agent": QUOTA_USER_AGENT,
93
+ },
94
+ body: JSON.stringify({ project: account.config.projectId }),
95
+ timeoutMs: 8000,
96
+ dispatcher: getAccountProxyDispatcher(account, "google-antigravity"),
97
+ });
98
+
99
+ if (!response.ok) {
100
+ if (response.status === 401 || response.status === 403) {
101
+ const errorText = await response.text();
102
+ ctx.log(
103
+ `${account.config.email}: quota API returned ${response.status}, flagging account`,
104
+ );
105
+ ctx.reportQuotaPollFlag(account, response.status, errorText);
106
+ ctx.markFlagged(
107
+ account,
108
+ `Quota API ${response.status}: ${errorText}`,
109
+ { triggerProtectivePause: false },
110
+ );
111
+ }
112
+ return;
113
+ }
114
+
115
+ const data = (await response.json()) as GoogleQuotaResponse;
116
+ const oldQuota = account.quota || [];
117
+ const fresh = extractQuotas(data, oldQuota);
118
+ // Drop the previous Antigravity entries so the new ones fully replace
119
+ // them; keep entries from OTHER providers (Ollama) so multi-provider
120
+ // accounts accumulate quotas across credentials without overwriting
121
+ // one another.
122
+ const otherProviders = (oldQuota || []).filter(
123
+ (q) => (q as { providerId?: string }).providerId &&
124
+ (q as { providerId?: string }).providerId !== "google-antigravity",
125
+ );
126
+ fresh.forEach(
127
+ (q) => ((q as { providerId?: string }).providerId = "google-antigravity"),
128
+ );
129
+ account.quota = [...otherProviders, ...fresh];
130
+ account.lastQuotaPoll = Date.now();
131
+
132
+ // Stash the provider-local poll log for the rotator to emit as a
133
+ // single consolidated line per cycle.
134
+ account.lastPollByProvider ??= {};
135
+ account.lastPollByProvider["google-antigravity"] = account.quota
136
+ .filter(
137
+ (q) =>
138
+ (q as { providerId?: string }).providerId === "google-antigravity",
139
+ )
140
+ .map((q) => {
141
+ const remain = q.resetTime
142
+ ? Math.round(
143
+ (new Date(q.resetTime).getTime() - Date.now()) / 60000,
144
+ ) + "m"
145
+ : "no_reset";
146
+ return `[${q.modelKey}: ${q.timerType} ${q.percentRemaining}% in ${remain}]`;
147
+ })
148
+ .join(" | ");
149
+ } catch {
150
+ // Network error, skip
151
+ }
152
+ }