antigravity-auth 1.6.0 → 1.7.1

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 (258) hide show
  1. package/README.md +79 -41
  2. package/dist/cli.js +17854 -0
  3. package/dist/handler.js +25147 -0
  4. package/dist/index.js +25138 -5
  5. package/package.json +66 -54
  6. package/dist/antigravity/oauth.d.ts +0 -30
  7. package/dist/antigravity/oauth.js +0 -170
  8. package/dist/claude/login.d.ts +0 -7
  9. package/dist/claude/login.js +0 -480
  10. package/dist/claude/menu-helpers.d.ts +0 -22
  11. package/dist/claude/menu-helpers.js +0 -281
  12. package/dist/claude/proxy-manager.d.ts +0 -11
  13. package/dist/claude/proxy-manager.js +0 -129
  14. package/dist/claude/proxy.d.ts +0 -1
  15. package/dist/claude/proxy.js +0 -733
  16. package/dist/constants.d.ts +0 -138
  17. package/dist/constants.js +0 -216
  18. package/dist/hooks/auto-update-checker/cache.d.ts +0 -2
  19. package/dist/hooks/auto-update-checker/cache.js +0 -70
  20. package/dist/hooks/auto-update-checker/checker.d.ts +0 -15
  21. package/dist/hooks/auto-update-checker/checker.js +0 -233
  22. package/dist/hooks/auto-update-checker/constants.d.ts +0 -8
  23. package/dist/hooks/auto-update-checker/constants.js +0 -22
  24. package/dist/hooks/auto-update-checker/index.d.ts +0 -33
  25. package/dist/hooks/auto-update-checker/index.js +0 -121
  26. package/dist/hooks/auto-update-checker/logging.d.ts +0 -2
  27. package/dist/hooks/auto-update-checker/logging.js +0 -8
  28. package/dist/hooks/auto-update-checker/types.d.ts +0 -24
  29. package/dist/hooks/auto-update-checker/types.js +0 -1
  30. package/dist/index.d.ts +0 -6
  31. package/dist/opencode/hooks/auto-update-checker/cache.d.ts +0 -2
  32. package/dist/opencode/hooks/auto-update-checker/cache.js +0 -70
  33. package/dist/opencode/hooks/auto-update-checker/checker.d.ts +0 -15
  34. package/dist/opencode/hooks/auto-update-checker/checker.js +0 -233
  35. package/dist/opencode/hooks/auto-update-checker/constants.d.ts +0 -8
  36. package/dist/opencode/hooks/auto-update-checker/constants.js +0 -22
  37. package/dist/opencode/hooks/auto-update-checker/index.d.ts +0 -33
  38. package/dist/opencode/hooks/auto-update-checker/index.js +0 -121
  39. package/dist/opencode/hooks/auto-update-checker/logging.d.ts +0 -2
  40. package/dist/opencode/hooks/auto-update-checker/logging.js +0 -8
  41. package/dist/opencode/hooks/auto-update-checker/types.d.ts +0 -24
  42. package/dist/opencode/hooks/auto-update-checker/types.js +0 -1
  43. package/dist/opencode/plugin.d.ts +0 -29
  44. package/dist/opencode/plugin.js +0 -2954
  45. package/dist/plugin/accounts.d.ts +0 -173
  46. package/dist/plugin/accounts.js +0 -966
  47. package/dist/plugin/auth.d.ts +0 -20
  48. package/dist/plugin/auth.js +0 -44
  49. package/dist/plugin/cache/index.d.ts +0 -4
  50. package/dist/plugin/cache/index.js +0 -4
  51. package/dist/plugin/cache/signature-cache.d.ts +0 -110
  52. package/dist/plugin/cache/signature-cache.js +0 -347
  53. package/dist/plugin/cache.d.ts +0 -43
  54. package/dist/plugin/cache.js +0 -180
  55. package/dist/plugin/cli.d.ts +0 -26
  56. package/dist/plugin/cli.js +0 -126
  57. package/dist/plugin/config/index.d.ts +0 -15
  58. package/dist/plugin/config/index.js +0 -15
  59. package/dist/plugin/config/loader.d.ts +0 -38
  60. package/dist/plugin/config/loader.js +0 -150
  61. package/dist/plugin/config/models.d.ts +0 -26
  62. package/dist/plugin/config/models.js +0 -95
  63. package/dist/plugin/config/schema.d.ts +0 -144
  64. package/dist/plugin/config/schema.js +0 -458
  65. package/dist/plugin/config/updater.d.ts +0 -76
  66. package/dist/plugin/config/updater.js +0 -205
  67. package/dist/plugin/core/streaming/index.d.ts +0 -2
  68. package/dist/plugin/core/streaming/index.js +0 -2
  69. package/dist/plugin/core/streaming/transformer.d.ts +0 -9
  70. package/dist/plugin/core/streaming/transformer.js +0 -301
  71. package/dist/plugin/core/streaming/types.d.ts +0 -28
  72. package/dist/plugin/core/streaming/types.js +0 -1
  73. package/dist/plugin/debug.d.ts +0 -93
  74. package/dist/plugin/debug.js +0 -375
  75. package/dist/plugin/errors.d.ts +0 -27
  76. package/dist/plugin/errors.js +0 -41
  77. package/dist/plugin/fingerprint.d.ts +0 -69
  78. package/dist/plugin/fingerprint.js +0 -137
  79. package/dist/plugin/image-saver.d.ts +0 -24
  80. package/dist/plugin/image-saver.js +0 -78
  81. package/dist/plugin/logger.d.ts +0 -35
  82. package/dist/plugin/logger.js +0 -67
  83. package/dist/plugin/logging-utils.d.ts +0 -22
  84. package/dist/plugin/logging-utils.js +0 -91
  85. package/dist/plugin/project.d.ts +0 -32
  86. package/dist/plugin/project.js +0 -229
  87. package/dist/plugin/quota.d.ts +0 -34
  88. package/dist/plugin/quota.js +0 -261
  89. package/dist/plugin/recovery/constants.d.ts +0 -21
  90. package/dist/plugin/recovery/constants.js +0 -42
  91. package/dist/plugin/recovery/index.d.ts +0 -11
  92. package/dist/plugin/recovery/index.js +0 -11
  93. package/dist/plugin/recovery/storage.d.ts +0 -23
  94. package/dist/plugin/recovery/storage.js +0 -340
  95. package/dist/plugin/recovery/types.d.ts +0 -115
  96. package/dist/plugin/recovery/types.js +0 -6
  97. package/dist/plugin/recovery.d.ts +0 -60
  98. package/dist/plugin/recovery.js +0 -360
  99. package/dist/plugin/refresh-queue.d.ts +0 -99
  100. package/dist/plugin/refresh-queue.js +0 -235
  101. package/dist/plugin/request-helpers.d.ts +0 -281
  102. package/dist/plugin/request-helpers.js +0 -2200
  103. package/dist/plugin/request.d.ts +0 -110
  104. package/dist/plugin/request.js +0 -1489
  105. package/dist/plugin/rotation.d.ts +0 -182
  106. package/dist/plugin/rotation.js +0 -364
  107. package/dist/plugin/search.d.ts +0 -31
  108. package/dist/plugin/search.js +0 -185
  109. package/dist/plugin/server.d.ts +0 -22
  110. package/dist/plugin/server.js +0 -306
  111. package/dist/plugin/storage.d.ts +0 -136
  112. package/dist/plugin/storage.js +0 -599
  113. package/dist/plugin/stores/signature-store.d.ts +0 -4
  114. package/dist/plugin/stores/signature-store.js +0 -24
  115. package/dist/plugin/thinking-recovery.d.ts +0 -89
  116. package/dist/plugin/thinking-recovery.js +0 -289
  117. package/dist/plugin/token.d.ts +0 -18
  118. package/dist/plugin/token.js +0 -127
  119. package/dist/plugin/transform/claude.d.ts +0 -79
  120. package/dist/plugin/transform/claude.js +0 -256
  121. package/dist/plugin/transform/cross-model-sanitizer.d.ts +0 -34
  122. package/dist/plugin/transform/cross-model-sanitizer.js +0 -224
  123. package/dist/plugin/transform/gemini.d.ts +0 -132
  124. package/dist/plugin/transform/gemini.js +0 -659
  125. package/dist/plugin/transform/index.d.ts +0 -14
  126. package/dist/plugin/transform/index.js +0 -9
  127. package/dist/plugin/transform/model-resolver.d.ts +0 -98
  128. package/dist/plugin/transform/model-resolver.js +0 -320
  129. package/dist/plugin/transform/types.d.ts +0 -110
  130. package/dist/plugin/transform/types.js +0 -1
  131. package/dist/plugin/types.d.ts +0 -95
  132. package/dist/plugin/types.js +0 -1
  133. package/dist/plugin/ui/ansi.d.ts +0 -31
  134. package/dist/plugin/ui/ansi.js +0 -45
  135. package/dist/plugin/ui/auth-menu.d.ts +0 -47
  136. package/dist/plugin/ui/auth-menu.js +0 -199
  137. package/dist/plugin/ui/confirm.d.ts +0 -1
  138. package/dist/plugin/ui/confirm.js +0 -14
  139. package/dist/plugin/ui/select.d.ts +0 -22
  140. package/dist/plugin/ui/select.js +0 -243
  141. package/dist/plugin/version.d.ts +0 -18
  142. package/dist/plugin/version.js +0 -79
  143. package/dist/src/antigravity/oauth.d.ts +0 -30
  144. package/dist/src/antigravity/oauth.js +0 -170
  145. package/dist/src/constants.d.ts +0 -138
  146. package/dist/src/constants.js +0 -216
  147. package/dist/src/hooks/auto-update-checker/cache.d.ts +0 -2
  148. package/dist/src/hooks/auto-update-checker/cache.js +0 -70
  149. package/dist/src/hooks/auto-update-checker/checker.d.ts +0 -15
  150. package/dist/src/hooks/auto-update-checker/checker.js +0 -233
  151. package/dist/src/hooks/auto-update-checker/constants.d.ts +0 -8
  152. package/dist/src/hooks/auto-update-checker/constants.js +0 -22
  153. package/dist/src/hooks/auto-update-checker/index.d.ts +0 -33
  154. package/dist/src/hooks/auto-update-checker/index.js +0 -121
  155. package/dist/src/hooks/auto-update-checker/logging.d.ts +0 -2
  156. package/dist/src/hooks/auto-update-checker/logging.js +0 -8
  157. package/dist/src/hooks/auto-update-checker/types.d.ts +0 -24
  158. package/dist/src/hooks/auto-update-checker/types.js +0 -1
  159. package/dist/src/index.d.ts +0 -6
  160. package/dist/src/index.js +0 -5
  161. package/dist/src/plugin/accounts.d.ts +0 -173
  162. package/dist/src/plugin/accounts.js +0 -966
  163. package/dist/src/plugin/auth.d.ts +0 -20
  164. package/dist/src/plugin/auth.js +0 -44
  165. package/dist/src/plugin/cache/index.d.ts +0 -4
  166. package/dist/src/plugin/cache/index.js +0 -4
  167. package/dist/src/plugin/cache/signature-cache.d.ts +0 -110
  168. package/dist/src/plugin/cache/signature-cache.js +0 -347
  169. package/dist/src/plugin/cache.d.ts +0 -43
  170. package/dist/src/plugin/cache.js +0 -180
  171. package/dist/src/plugin/cli.d.ts +0 -26
  172. package/dist/src/plugin/cli.js +0 -126
  173. package/dist/src/plugin/config/index.d.ts +0 -15
  174. package/dist/src/plugin/config/index.js +0 -15
  175. package/dist/src/plugin/config/loader.d.ts +0 -38
  176. package/dist/src/plugin/config/loader.js +0 -150
  177. package/dist/src/plugin/config/models.d.ts +0 -26
  178. package/dist/src/plugin/config/models.js +0 -95
  179. package/dist/src/plugin/config/schema.d.ts +0 -144
  180. package/dist/src/plugin/config/schema.js +0 -458
  181. package/dist/src/plugin/config/updater.d.ts +0 -76
  182. package/dist/src/plugin/config/updater.js +0 -205
  183. package/dist/src/plugin/core/streaming/index.d.ts +0 -2
  184. package/dist/src/plugin/core/streaming/index.js +0 -2
  185. package/dist/src/plugin/core/streaming/transformer.d.ts +0 -9
  186. package/dist/src/plugin/core/streaming/transformer.js +0 -301
  187. package/dist/src/plugin/core/streaming/types.d.ts +0 -28
  188. package/dist/src/plugin/core/streaming/types.js +0 -1
  189. package/dist/src/plugin/debug.d.ts +0 -93
  190. package/dist/src/plugin/debug.js +0 -375
  191. package/dist/src/plugin/errors.d.ts +0 -27
  192. package/dist/src/plugin/errors.js +0 -41
  193. package/dist/src/plugin/fingerprint.d.ts +0 -69
  194. package/dist/src/plugin/fingerprint.js +0 -137
  195. package/dist/src/plugin/image-saver.d.ts +0 -24
  196. package/dist/src/plugin/image-saver.js +0 -78
  197. package/dist/src/plugin/logger.d.ts +0 -35
  198. package/dist/src/plugin/logger.js +0 -67
  199. package/dist/src/plugin/logging-utils.d.ts +0 -22
  200. package/dist/src/plugin/logging-utils.js +0 -91
  201. package/dist/src/plugin/project.d.ts +0 -32
  202. package/dist/src/plugin/project.js +0 -229
  203. package/dist/src/plugin/quota.d.ts +0 -34
  204. package/dist/src/plugin/quota.js +0 -261
  205. package/dist/src/plugin/recovery/constants.d.ts +0 -21
  206. package/dist/src/plugin/recovery/constants.js +0 -42
  207. package/dist/src/plugin/recovery/index.d.ts +0 -11
  208. package/dist/src/plugin/recovery/index.js +0 -11
  209. package/dist/src/plugin/recovery/storage.d.ts +0 -23
  210. package/dist/src/plugin/recovery/storage.js +0 -340
  211. package/dist/src/plugin/recovery/types.d.ts +0 -115
  212. package/dist/src/plugin/recovery/types.js +0 -6
  213. package/dist/src/plugin/recovery.d.ts +0 -60
  214. package/dist/src/plugin/recovery.js +0 -360
  215. package/dist/src/plugin/refresh-queue.d.ts +0 -99
  216. package/dist/src/plugin/refresh-queue.js +0 -235
  217. package/dist/src/plugin/request-helpers.d.ts +0 -281
  218. package/dist/src/plugin/request-helpers.js +0 -2200
  219. package/dist/src/plugin/request.d.ts +0 -110
  220. package/dist/src/plugin/request.js +0 -1489
  221. package/dist/src/plugin/rotation.d.ts +0 -182
  222. package/dist/src/plugin/rotation.js +0 -364
  223. package/dist/src/plugin/search.d.ts +0 -31
  224. package/dist/src/plugin/search.js +0 -185
  225. package/dist/src/plugin/server.d.ts +0 -22
  226. package/dist/src/plugin/server.js +0 -306
  227. package/dist/src/plugin/storage.d.ts +0 -136
  228. package/dist/src/plugin/storage.js +0 -599
  229. package/dist/src/plugin/stores/signature-store.d.ts +0 -4
  230. package/dist/src/plugin/stores/signature-store.js +0 -24
  231. package/dist/src/plugin/thinking-recovery.d.ts +0 -89
  232. package/dist/src/plugin/thinking-recovery.js +0 -289
  233. package/dist/src/plugin/token.d.ts +0 -18
  234. package/dist/src/plugin/token.js +0 -127
  235. package/dist/src/plugin/transform/claude.d.ts +0 -79
  236. package/dist/src/plugin/transform/claude.js +0 -256
  237. package/dist/src/plugin/transform/cross-model-sanitizer.d.ts +0 -34
  238. package/dist/src/plugin/transform/cross-model-sanitizer.js +0 -224
  239. package/dist/src/plugin/transform/gemini.d.ts +0 -132
  240. package/dist/src/plugin/transform/gemini.js +0 -659
  241. package/dist/src/plugin/transform/index.d.ts +0 -14
  242. package/dist/src/plugin/transform/index.js +0 -9
  243. package/dist/src/plugin/transform/model-resolver.d.ts +0 -98
  244. package/dist/src/plugin/transform/model-resolver.js +0 -320
  245. package/dist/src/plugin/transform/types.d.ts +0 -110
  246. package/dist/src/plugin/transform/types.js +0 -1
  247. package/dist/src/plugin/types.d.ts +0 -95
  248. package/dist/src/plugin/types.js +0 -1
  249. package/dist/src/plugin/ui/ansi.d.ts +0 -31
  250. package/dist/src/plugin/ui/ansi.js +0 -45
  251. package/dist/src/plugin/ui/auth-menu.d.ts +0 -47
  252. package/dist/src/plugin/ui/auth-menu.js +0 -199
  253. package/dist/src/plugin/ui/confirm.d.ts +0 -1
  254. package/dist/src/plugin/ui/confirm.js +0 -14
  255. package/dist/src/plugin/ui/select.d.ts +0 -22
  256. package/dist/src/plugin/ui/select.js +0 -243
  257. package/dist/src/plugin/version.d.ts +0 -18
  258. package/dist/src/plugin/version.js +0 -79
@@ -1,20 +0,0 @@
1
- import type { AuthDetails, OAuthAuthDetails, RefreshParts } from "./types";
2
- export declare function isOAuthAuth(auth: AuthDetails): auth is OAuthAuthDetails;
3
- /**
4
- * Splits a packed refresh string into its constituent refresh token and project IDs.
5
- */
6
- export declare function parseRefreshParts(refresh: string): RefreshParts;
7
- /**
8
- * Serializes refresh token parts into the stored string format.
9
- */
10
- export declare function formatRefreshParts(parts: RefreshParts): string;
11
- /**
12
- * Determines whether an access token is expired or missing, with buffer for clock skew.
13
- */
14
- export declare function accessTokenExpired(auth: OAuthAuthDetails): boolean;
15
- /**
16
- * Calculates absolute expiry timestamp based on a duration.
17
- * @param requestTimeMs The local time when the request was initiated
18
- * @param expiresInSeconds The duration returned by the server
19
- */
20
- export declare function calculateTokenExpiry(requestTimeMs: number, expiresInSeconds: unknown): number;
@@ -1,44 +0,0 @@
1
- const ACCESS_TOKEN_EXPIRY_BUFFER_MS = 60 * 1000;
2
- export function isOAuthAuth(auth) {
3
- return auth.type === "oauth";
4
- }
5
- /**
6
- * Splits a packed refresh string into its constituent refresh token and project IDs.
7
- */
8
- export function parseRefreshParts(refresh) {
9
- const [refreshToken = "", projectId = "", managedProjectId = ""] = (refresh ?? "").split("|");
10
- return {
11
- refreshToken,
12
- projectId: projectId || undefined,
13
- managedProjectId: managedProjectId || undefined,
14
- };
15
- }
16
- /**
17
- * Serializes refresh token parts into the stored string format.
18
- */
19
- export function formatRefreshParts(parts) {
20
- const projectSegment = parts.projectId ?? "";
21
- const base = `${parts.refreshToken}|${projectSegment}`;
22
- return parts.managedProjectId ? `${base}|${parts.managedProjectId}` : base;
23
- }
24
- /**
25
- * Determines whether an access token is expired or missing, with buffer for clock skew.
26
- */
27
- export function accessTokenExpired(auth) {
28
- if (!auth.access || typeof auth.expires !== "number") {
29
- return true;
30
- }
31
- return auth.expires <= Date.now() + ACCESS_TOKEN_EXPIRY_BUFFER_MS;
32
- }
33
- /**
34
- * Calculates absolute expiry timestamp based on a duration.
35
- * @param requestTimeMs The local time when the request was initiated
36
- * @param expiresInSeconds The duration returned by the server
37
- */
38
- export function calculateTokenExpiry(requestTimeMs, expiresInSeconds) {
39
- const seconds = typeof expiresInSeconds === "number" ? expiresInSeconds : 3600;
40
- if (isNaN(seconds) || seconds <= 0) {
41
- return requestTimeMs;
42
- }
43
- return requestTimeMs + seconds * 1000;
44
- }
@@ -1,4 +0,0 @@
1
- /**
2
- * Cache module for opencode-antigravity-auth plugin.
3
- */
4
- export { SignatureCache, createSignatureCache, } from "./signature-cache";
@@ -1,4 +0,0 @@
1
- /**
2
- * Cache module for opencode-antigravity-auth plugin.
3
- */
4
- export { SignatureCache, createSignatureCache, } from "./signature-cache";
@@ -1,110 +0,0 @@
1
- /**
2
- * Signature cache for persisting thinking block signatures to disk.
3
- *
4
- * Features (based on LLM-API-Key-Proxy's ProviderCache):
5
- * - Dual-TTL system: short memory TTL, longer disk TTL
6
- * - Background disk persistence with batched writes
7
- * - Atomic writes with temp file + move pattern
8
- * - Automatic cleanup of expired entries
9
- *
10
- * Cache key format: `${sessionId}:${modelId}`
11
- */
12
- import type { SignatureCacheConfig } from "../config";
13
- interface CacheStats {
14
- memoryHits: number;
15
- diskHits: number;
16
- misses: number;
17
- writes: number;
18
- memoryEntries: number;
19
- dirty: boolean;
20
- diskEnabled: boolean;
21
- }
22
- /**
23
- * Full thinking content with signature (for recovery)
24
- */
25
- export interface ThinkingCacheData {
26
- text: string;
27
- signature: string;
28
- toolIds?: string[];
29
- }
30
- export declare class SignatureCache {
31
- private cache;
32
- private memoryTtlMs;
33
- private diskTtlMs;
34
- private writeIntervalMs;
35
- private cacheFilePath;
36
- private enabled;
37
- private dirty;
38
- private writeTimer;
39
- private cleanupTimer;
40
- private stats;
41
- constructor(config: SignatureCacheConfig);
42
- /**
43
- * Generate a cache key from sessionId and modelId.
44
- */
45
- static makeKey(sessionId: string, modelId: string): string;
46
- /**
47
- * Store a signature in the cache.
48
- */
49
- store(key: string, signature: string): void;
50
- /**
51
- * Retrieve a signature from the cache.
52
- * Returns null if not found or expired.
53
- */
54
- retrieve(key: string): string | null;
55
- /**
56
- * Check if a key exists in the cache (without updating stats).
57
- */
58
- has(key: string): boolean;
59
- /**
60
- * Store full thinking content with signature.
61
- * This enables recovery even after thinking text is stripped by compaction.
62
- *
63
- * Port of LLM-API-Key-Proxy's _cache_thinking()
64
- */
65
- storeThinking(key: string, thinkingText: string, signature: string, toolIds?: string[]): void;
66
- /**
67
- * Retrieve full thinking content by key.
68
- * Returns null if not found or expired.
69
- */
70
- retrieveThinking(key: string): ThinkingCacheData | null;
71
- /**
72
- * Check if full thinking content exists for a key.
73
- */
74
- hasThinking(key: string): boolean;
75
- /**
76
- * Get cache statistics.
77
- */
78
- getStats(): CacheStats;
79
- /**
80
- * Manually trigger a disk save.
81
- */
82
- flush(): Promise<boolean>;
83
- /**
84
- * Graceful shutdown: stop timers and flush to disk.
85
- */
86
- shutdown(): void;
87
- /**
88
- * Load cache from disk file with TTL validation.
89
- */
90
- private loadFromDisk;
91
- /**
92
- * Save cache to disk with atomic write pattern.
93
- * Merges with existing disk entries that haven't expired.
94
- */
95
- private saveToDisk;
96
- /**
97
- * Start background write and cleanup timers.
98
- */
99
- private startBackgroundTasks;
100
- /**
101
- * Remove expired entries from memory.
102
- */
103
- private cleanupExpired;
104
- }
105
- /**
106
- * Create a signature cache with the given configuration.
107
- * Returns null if caching is disabled.
108
- */
109
- export declare function createSignatureCache(config: SignatureCacheConfig | undefined): SignatureCache | null;
110
- export {};
@@ -1,347 +0,0 @@
1
- /**
2
- * Signature cache for persisting thinking block signatures to disk.
3
- *
4
- * Features (based on LLM-API-Key-Proxy's ProviderCache):
5
- * - Dual-TTL system: short memory TTL, longer disk TTL
6
- * - Background disk persistence with batched writes
7
- * - Atomic writes with temp file + move pattern
8
- * - Automatic cleanup of expired entries
9
- *
10
- * Cache key format: `${sessionId}:${modelId}`
11
- */
12
- import { existsSync, mkdirSync, readFileSync, writeFileSync, renameSync, unlinkSync } from "node:fs";
13
- import { join, dirname } from "node:path";
14
- import { homedir } from "node:os";
15
- import { tmpdir } from "node:os";
16
- import { ensureGitignoreSync } from "../storage";
17
- // =============================================================================
18
- // =============================================================================
19
- function getConfigDir() {
20
- const platform = process.platform;
21
- if (platform === "win32") {
22
- return join(process.env.APPDATA || join(homedir(), "AppData", "Roaming"), "opencode");
23
- }
24
- const xdgConfig = process.env.XDG_CONFIG_HOME || join(homedir(), ".config");
25
- return join(xdgConfig, "opencode");
26
- }
27
- function getCacheFilePath() {
28
- const configSubDir = join(getConfigDir(), "config");
29
- return join(configSubDir, "antigravity-signature-cache.json");
30
- }
31
- // =============================================================================
32
- // =============================================================================
33
- export class SignatureCache {
34
- cache = new Map();
35
- memoryTtlMs;
36
- diskTtlMs;
37
- writeIntervalMs;
38
- cacheFilePath;
39
- enabled;
40
- dirty = false;
41
- writeTimer = null;
42
- cleanupTimer = null;
43
- stats = {
44
- memoryHits: 0,
45
- diskHits: 0,
46
- misses: 0,
47
- writes: 0,
48
- };
49
- constructor(config) {
50
- this.enabled = config.enabled;
51
- this.memoryTtlMs = config.memory_ttl_seconds * 1000;
52
- this.diskTtlMs = config.disk_ttl_seconds * 1000;
53
- this.writeIntervalMs = config.write_interval_seconds * 1000;
54
- this.cacheFilePath = getCacheFilePath();
55
- if (this.enabled) {
56
- this.loadFromDisk();
57
- this.startBackgroundTasks();
58
- }
59
- }
60
- // ===========================================================================
61
- // ===========================================================================
62
- /**
63
- * Generate a cache key from sessionId and modelId.
64
- */
65
- static makeKey(sessionId, modelId) {
66
- return `${sessionId}:${modelId}`;
67
- }
68
- /**
69
- * Store a signature in the cache.
70
- */
71
- store(key, signature) {
72
- if (!this.enabled)
73
- return;
74
- this.cache.set(key, {
75
- value: signature,
76
- timestamp: Date.now(),
77
- });
78
- this.dirty = true;
79
- }
80
- /**
81
- * Retrieve a signature from the cache.
82
- * Returns null if not found or expired.
83
- */
84
- retrieve(key) {
85
- if (!this.enabled)
86
- return null;
87
- const entry = this.cache.get(key);
88
- if (entry) {
89
- const age = Date.now() - entry.timestamp;
90
- if (age <= this.memoryTtlMs) {
91
- this.stats.memoryHits++;
92
- return entry.value;
93
- }
94
- this.cache.delete(key);
95
- }
96
- this.stats.misses++;
97
- return null;
98
- }
99
- /**
100
- * Check if a key exists in the cache (without updating stats).
101
- */
102
- has(key) {
103
- if (!this.enabled)
104
- return false;
105
- const entry = this.cache.get(key);
106
- if (!entry)
107
- return false;
108
- const age = Date.now() - entry.timestamp;
109
- return age <= this.memoryTtlMs;
110
- }
111
- // ===========================================================================
112
- // ===========================================================================
113
- /**
114
- * Store full thinking content with signature.
115
- * This enables recovery even after thinking text is stripped by compaction.
116
- *
117
- * Port of LLM-API-Key-Proxy's _cache_thinking()
118
- */
119
- storeThinking(key, thinkingText, signature, toolIds) {
120
- if (!this.enabled || !thinkingText || !signature)
121
- return;
122
- this.cache.set(key, {
123
- value: signature,
124
- timestamp: Date.now(),
125
- thinkingText,
126
- textPreview: thinkingText.slice(0, 100),
127
- toolIds,
128
- });
129
- this.dirty = true;
130
- }
131
- /**
132
- * Retrieve full thinking content by key.
133
- * Returns null if not found or expired.
134
- */
135
- retrieveThinking(key) {
136
- if (!this.enabled)
137
- return null;
138
- const entry = this.cache.get(key);
139
- if (!entry || !entry.thinkingText)
140
- return null;
141
- const age = Date.now() - entry.timestamp;
142
- if (age > this.memoryTtlMs) {
143
- this.cache.delete(key);
144
- return null;
145
- }
146
- this.stats.memoryHits++;
147
- return {
148
- text: entry.thinkingText,
149
- signature: entry.value,
150
- toolIds: entry.toolIds,
151
- };
152
- }
153
- /**
154
- * Check if full thinking content exists for a key.
155
- */
156
- hasThinking(key) {
157
- if (!this.enabled)
158
- return false;
159
- const entry = this.cache.get(key);
160
- if (!entry || !entry.thinkingText)
161
- return false;
162
- const age = Date.now() - entry.timestamp;
163
- return age <= this.memoryTtlMs;
164
- }
165
- /**
166
- * Get cache statistics.
167
- */
168
- getStats() {
169
- return {
170
- ...this.stats,
171
- memoryEntries: this.cache.size,
172
- dirty: this.dirty,
173
- diskEnabled: this.enabled,
174
- };
175
- }
176
- /**
177
- * Manually trigger a disk save.
178
- */
179
- async flush() {
180
- if (!this.enabled)
181
- return true;
182
- return this.saveToDisk();
183
- }
184
- /**
185
- * Graceful shutdown: stop timers and flush to disk.
186
- */
187
- shutdown() {
188
- if (this.writeTimer) {
189
- clearInterval(this.writeTimer);
190
- this.writeTimer = null;
191
- }
192
- if (this.cleanupTimer) {
193
- clearInterval(this.cleanupTimer);
194
- this.cleanupTimer = null;
195
- }
196
- if (this.dirty && this.enabled) {
197
- this.saveToDisk();
198
- }
199
- }
200
- // ===========================================================================
201
- // ===========================================================================
202
- /**
203
- * Load cache from disk file with TTL validation.
204
- */
205
- loadFromDisk() {
206
- try {
207
- if (!existsSync(this.cacheFilePath)) {
208
- return;
209
- }
210
- const content = readFileSync(this.cacheFilePath, "utf-8");
211
- const data = JSON.parse(content);
212
- if (data.version !== "1.0") {
213
- return;
214
- }
215
- const now = Date.now();
216
- let loaded = 0;
217
- let expired = 0;
218
- for (const [key, entry] of Object.entries(data.entries)) {
219
- const age = now - entry.timestamp;
220
- if (age <= this.diskTtlMs) {
221
- this.cache.set(key, {
222
- value: entry.value,
223
- timestamp: entry.timestamp,
224
- });
225
- loaded++;
226
- }
227
- else {
228
- expired++;
229
- }
230
- }
231
- }
232
- catch {
233
- }
234
- }
235
- /**
236
- * Save cache to disk with atomic write pattern.
237
- * Merges with existing disk entries that haven't expired.
238
- */
239
- saveToDisk() {
240
- try {
241
- const dir = dirname(this.cacheFilePath);
242
- if (!existsSync(dir)) {
243
- mkdirSync(dir, { recursive: true });
244
- }
245
- ensureGitignoreSync(dir);
246
- const now = Date.now();
247
- let existingEntries = {};
248
- if (existsSync(this.cacheFilePath)) {
249
- try {
250
- const content = readFileSync(this.cacheFilePath, "utf-8");
251
- const data = JSON.parse(content);
252
- existingEntries = data.entries || {};
253
- }
254
- catch {
255
- }
256
- }
257
- // Step 2: Filter existing disk entries by disk_ttl
258
- const validDiskEntries = {};
259
- for (const [key, entry] of Object.entries(existingEntries)) {
260
- const age = now - entry.timestamp;
261
- if (age <= this.diskTtlMs) {
262
- validDiskEntries[key] = entry;
263
- }
264
- }
265
- const mergedEntries = { ...validDiskEntries };
266
- for (const [key, entry] of this.cache.entries()) {
267
- mergedEntries[key] = {
268
- value: entry.value,
269
- timestamp: entry.timestamp,
270
- };
271
- }
272
- const cacheData = {
273
- version: "1.0",
274
- memory_ttl_seconds: this.memoryTtlMs / 1000,
275
- disk_ttl_seconds: this.diskTtlMs / 1000,
276
- entries: mergedEntries,
277
- statistics: {
278
- memory_hits: this.stats.memoryHits,
279
- disk_hits: this.stats.diskHits,
280
- misses: this.stats.misses,
281
- writes: this.stats.writes + 1,
282
- last_write: now,
283
- },
284
- };
285
- const tmpPath = join(tmpdir(), `antigravity-cache-${Date.now()}-${Math.random().toString(36).slice(2)}.tmp`);
286
- writeFileSync(tmpPath, JSON.stringify(cacheData, null, 2), "utf-8");
287
- try {
288
- renameSync(tmpPath, this.cacheFilePath);
289
- }
290
- catch {
291
- writeFileSync(this.cacheFilePath, readFileSync(tmpPath));
292
- try {
293
- unlinkSync(tmpPath);
294
- }
295
- catch {
296
- }
297
- }
298
- this.stats.writes++;
299
- this.dirty = false;
300
- return true;
301
- }
302
- catch {
303
- return false;
304
- }
305
- }
306
- // ===========================================================================
307
- // ===========================================================================
308
- /**
309
- * Start background write and cleanup timers.
310
- */
311
- startBackgroundTasks() {
312
- this.writeTimer = setInterval(() => {
313
- if (this.dirty) {
314
- this.saveToDisk();
315
- }
316
- }, this.writeIntervalMs);
317
- this.cleanupTimer = setInterval(() => {
318
- this.cleanupExpired();
319
- }, 30 * 60 * 1000);
320
- }
321
- /**
322
- * Remove expired entries from memory.
323
- */
324
- cleanupExpired() {
325
- const now = Date.now();
326
- let cleaned = 0;
327
- for (const [key, entry] of this.cache.entries()) {
328
- const age = now - entry.timestamp;
329
- if (age > this.memoryTtlMs) {
330
- this.cache.delete(key);
331
- cleaned++;
332
- }
333
- }
334
- }
335
- }
336
- // =============================================================================
337
- // =============================================================================
338
- /**
339
- * Create a signature cache with the given configuration.
340
- * Returns null if caching is disabled.
341
- */
342
- export function createSignatureCache(config) {
343
- if (!config || !config.enabled) {
344
- return null;
345
- }
346
- return new SignatureCache(config);
347
- }
@@ -1,43 +0,0 @@
1
- import type { OAuthAuthDetails } from "./types";
2
- /**
3
- * Returns a cached auth snapshot when available, favoring unexpired tokens.
4
- */
5
- export declare function resolveCachedAuth(auth: OAuthAuthDetails): OAuthAuthDetails;
6
- /**
7
- * Stores the latest auth snapshot keyed by refresh token.
8
- */
9
- export declare function storeCachedAuth(auth: OAuthAuthDetails): void;
10
- /**
11
- * Clears cached auth globally or for a specific refresh token.
12
- */
13
- export declare function clearCachedAuth(refresh?: string): void;
14
- import { SignatureCache } from "./cache/signature-cache";
15
- import type { SignatureCacheConfig } from "./config";
16
- /**
17
- * Initialize the disk-based signature cache.
18
- * Call this from plugin initialization when keep_thinking is enabled.
19
- */
20
- export declare function initDiskSignatureCache(config: SignatureCacheConfig | undefined): SignatureCache | null;
21
- /**
22
- * Get the disk cache instance (for testing/debugging).
23
- */
24
- export declare function getDiskSignatureCache(): SignatureCache | null;
25
- /**
26
- * Caches a thinking signature for a given session and text.
27
- * Used for Claude models that require signed thinking blocks in multi-turn conversations.
28
- * Also writes to disk cache if enabled.
29
- */
30
- export declare function cacheSignature(sessionId: string, text: string, signature: string): void;
31
- /**
32
- * Retrieves a cached signature for a given session and text.
33
- * Checks memory first, then falls back to disk cache.
34
- * Returns undefined if not found or expired.
35
- */
36
- export declare function getCachedSignature(sessionId: string, text: string): string | undefined;
37
- /**
38
- * Clears signature cache for a specific session or all sessions.
39
- * Also clears from disk cache if enabled.
40
- */
41
- export declare function clearSignatureCache(sessionId?: string): void;
42
- export { SignatureCache, createSignatureCache } from "./cache/signature-cache";
43
- export type { SignatureCacheConfig } from "./config";