@cortexkit/opencode-antigravity-auth 1.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 (271) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +714 -0
  3. package/dist/index.d.ts +4 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +15175 -0
  6. package/dist/index.js.map +7 -0
  7. package/dist/src/antigravity/oauth.d.ts +2 -0
  8. package/dist/src/antigravity/oauth.d.ts.map +1 -0
  9. package/dist/src/antigravity/oauth.js +3 -0
  10. package/dist/src/antigravity/oauth.js.map +1 -0
  11. package/dist/src/constants.d.ts +2 -0
  12. package/dist/src/constants.d.ts.map +1 -0
  13. package/dist/src/constants.js +3 -0
  14. package/dist/src/constants.js.map +1 -0
  15. package/dist/src/hooks/auto-update-checker/cache.d.ts +3 -0
  16. package/dist/src/hooks/auto-update-checker/cache.d.ts.map +1 -0
  17. package/dist/src/hooks/auto-update-checker/cache.js +71 -0
  18. package/dist/src/hooks/auto-update-checker/cache.js.map +1 -0
  19. package/dist/src/hooks/auto-update-checker/checker.d.ts +16 -0
  20. package/dist/src/hooks/auto-update-checker/checker.d.ts.map +1 -0
  21. package/dist/src/hooks/auto-update-checker/checker.js +234 -0
  22. package/dist/src/hooks/auto-update-checker/checker.js.map +1 -0
  23. package/dist/src/hooks/auto-update-checker/constants.d.ts +9 -0
  24. package/dist/src/hooks/auto-update-checker/constants.d.ts.map +1 -0
  25. package/dist/src/hooks/auto-update-checker/constants.js +23 -0
  26. package/dist/src/hooks/auto-update-checker/constants.js.map +1 -0
  27. package/dist/src/hooks/auto-update-checker/index.d.ts +34 -0
  28. package/dist/src/hooks/auto-update-checker/index.d.ts.map +1 -0
  29. package/dist/src/hooks/auto-update-checker/index.js +122 -0
  30. package/dist/src/hooks/auto-update-checker/index.js.map +1 -0
  31. package/dist/src/hooks/auto-update-checker/logging.d.ts +3 -0
  32. package/dist/src/hooks/auto-update-checker/logging.d.ts.map +1 -0
  33. package/dist/src/hooks/auto-update-checker/logging.js +9 -0
  34. package/dist/src/hooks/auto-update-checker/logging.js.map +1 -0
  35. package/dist/src/hooks/auto-update-checker/types.d.ts +25 -0
  36. package/dist/src/hooks/auto-update-checker/types.d.ts.map +1 -0
  37. package/dist/src/hooks/auto-update-checker/types.js +1 -0
  38. package/dist/src/hooks/auto-update-checker/types.js.map +1 -0
  39. package/dist/src/plugin/accounts.d.ts +232 -0
  40. package/dist/src/plugin/accounts.d.ts.map +1 -0
  41. package/dist/src/plugin/accounts.js +1163 -0
  42. package/dist/src/plugin/accounts.js.map +1 -0
  43. package/dist/src/plugin/agy-transport.d.ts +2 -0
  44. package/dist/src/plugin/agy-transport.d.ts.map +1 -0
  45. package/dist/src/plugin/agy-transport.js +3 -0
  46. package/dist/src/plugin/agy-transport.js.map +1 -0
  47. package/dist/src/plugin/auth-doctor.d.ts +30 -0
  48. package/dist/src/plugin/auth-doctor.d.ts.map +1 -0
  49. package/dist/src/plugin/auth-doctor.js +144 -0
  50. package/dist/src/plugin/auth-doctor.js.map +1 -0
  51. package/dist/src/plugin/auth-drift.d.ts +13 -0
  52. package/dist/src/plugin/auth-drift.d.ts.map +1 -0
  53. package/dist/src/plugin/auth-drift.js +70 -0
  54. package/dist/src/plugin/auth-drift.js.map +1 -0
  55. package/dist/src/plugin/auth.d.ts +2 -0
  56. package/dist/src/plugin/auth.d.ts.map +1 -0
  57. package/dist/src/plugin/auth.js +3 -0
  58. package/dist/src/plugin/auth.js.map +1 -0
  59. package/dist/src/plugin/cache/index.d.ts +5 -0
  60. package/dist/src/plugin/cache/index.d.ts.map +1 -0
  61. package/dist/src/plugin/cache/index.js +5 -0
  62. package/dist/src/plugin/cache/index.js.map +1 -0
  63. package/dist/src/plugin/cache/signature-cache.d.ts +111 -0
  64. package/dist/src/plugin/cache/signature-cache.d.ts.map +1 -0
  65. package/dist/src/plugin/cache/signature-cache.js +375 -0
  66. package/dist/src/plugin/cache/signature-cache.js.map +1 -0
  67. package/dist/src/plugin/cache.d.ts +44 -0
  68. package/dist/src/plugin/cache.d.ts.map +1 -0
  69. package/dist/src/plugin/cache.js +247 -0
  70. package/dist/src/plugin/cache.js.map +1 -0
  71. package/dist/src/plugin/cli.d.ts +44 -0
  72. package/dist/src/plugin/cli.d.ts.map +1 -0
  73. package/dist/src/plugin/cli.js +153 -0
  74. package/dist/src/plugin/cli.js.map +1 -0
  75. package/dist/src/plugin/config/index.d.ts +16 -0
  76. package/dist/src/plugin/config/index.d.ts.map +1 -0
  77. package/dist/src/plugin/config/index.js +16 -0
  78. package/dist/src/plugin/config/index.js.map +1 -0
  79. package/dist/src/plugin/config/loader.d.ts +36 -0
  80. package/dist/src/plugin/config/loader.d.ts.map +1 -0
  81. package/dist/src/plugin/config/loader.js +140 -0
  82. package/dist/src/plugin/config/loader.js.map +1 -0
  83. package/dist/src/plugin/config/models.d.ts +3 -0
  84. package/dist/src/plugin/config/models.d.ts.map +1 -0
  85. package/dist/src/plugin/config/models.js +2 -0
  86. package/dist/src/plugin/config/models.js.map +1 -0
  87. package/dist/src/plugin/config/schema.d.ts +141 -0
  88. package/dist/src/plugin/config/schema.d.ts.map +1 -0
  89. package/dist/src/plugin/config/schema.js +504 -0
  90. package/dist/src/plugin/config/schema.js.map +1 -0
  91. package/dist/src/plugin/config/updater.d.ts +55 -0
  92. package/dist/src/plugin/config/updater.d.ts.map +1 -0
  93. package/dist/src/plugin/config/updater.js +127 -0
  94. package/dist/src/plugin/config/updater.js.map +1 -0
  95. package/dist/src/plugin/core/streaming/index.d.ts +3 -0
  96. package/dist/src/plugin/core/streaming/index.d.ts.map +1 -0
  97. package/dist/src/plugin/core/streaming/index.js +3 -0
  98. package/dist/src/plugin/core/streaming/index.js.map +1 -0
  99. package/dist/src/plugin/core/streaming/transformer.d.ts +12 -0
  100. package/dist/src/plugin/core/streaming/transformer.d.ts.map +1 -0
  101. package/dist/src/plugin/core/streaming/transformer.js +447 -0
  102. package/dist/src/plugin/core/streaming/transformer.js.map +1 -0
  103. package/dist/src/plugin/core/streaming/types.d.ts +34 -0
  104. package/dist/src/plugin/core/streaming/types.d.ts.map +1 -0
  105. package/dist/src/plugin/core/streaming/types.js +1 -0
  106. package/dist/src/plugin/core/streaming/types.js.map +1 -0
  107. package/dist/src/plugin/debug.d.ts +94 -0
  108. package/dist/src/plugin/debug.d.ts.map +1 -0
  109. package/dist/src/plugin/debug.js +383 -0
  110. package/dist/src/plugin/debug.js.map +1 -0
  111. package/dist/src/plugin/errors.d.ts +28 -0
  112. package/dist/src/plugin/errors.d.ts.map +1 -0
  113. package/dist/src/plugin/errors.js +42 -0
  114. package/dist/src/plugin/errors.js.map +1 -0
  115. package/dist/src/plugin/fingerprint.d.ts +2 -0
  116. package/dist/src/plugin/fingerprint.d.ts.map +1 -0
  117. package/dist/src/plugin/fingerprint.js +3 -0
  118. package/dist/src/plugin/fingerprint.js.map +1 -0
  119. package/dist/src/plugin/gemini-dump.d.ts +47 -0
  120. package/dist/src/plugin/gemini-dump.d.ts.map +1 -0
  121. package/dist/src/plugin/gemini-dump.js +238 -0
  122. package/dist/src/plugin/gemini-dump.js.map +1 -0
  123. package/dist/src/plugin/image-saver.d.ts +25 -0
  124. package/dist/src/plugin/image-saver.d.ts.map +1 -0
  125. package/dist/src/plugin/image-saver.js +86 -0
  126. package/dist/src/plugin/image-saver.js.map +1 -0
  127. package/dist/src/plugin/logger.d.ts +23 -0
  128. package/dist/src/plugin/logger.d.ts.map +1 -0
  129. package/dist/src/plugin/logger.js +80 -0
  130. package/dist/src/plugin/logger.js.map +1 -0
  131. package/dist/src/plugin/logging-utils.d.ts +23 -0
  132. package/dist/src/plugin/logging-utils.d.ts.map +1 -0
  133. package/dist/src/plugin/logging-utils.js +92 -0
  134. package/dist/src/plugin/logging-utils.js.map +1 -0
  135. package/dist/src/plugin/model-registry.d.ts +2 -0
  136. package/dist/src/plugin/model-registry.d.ts.map +1 -0
  137. package/dist/src/plugin/model-registry.js +3 -0
  138. package/dist/src/plugin/model-registry.js.map +1 -0
  139. package/dist/src/plugin/project.d.ts +2 -0
  140. package/dist/src/plugin/project.d.ts.map +1 -0
  141. package/dist/src/plugin/project.js +3 -0
  142. package/dist/src/plugin/project.js.map +1 -0
  143. package/dist/src/plugin/prompt-context.d.ts +18 -0
  144. package/dist/src/plugin/prompt-context.d.ts.map +1 -0
  145. package/dist/src/plugin/prompt-context.js +99 -0
  146. package/dist/src/plugin/prompt-context.js.map +1 -0
  147. package/dist/src/plugin/quota.d.ts +44 -0
  148. package/dist/src/plugin/quota.d.ts.map +1 -0
  149. package/dist/src/plugin/quota.js +336 -0
  150. package/dist/src/plugin/quota.js.map +1 -0
  151. package/dist/src/plugin/recovery/constants.d.ts +22 -0
  152. package/dist/src/plugin/recovery/constants.d.ts.map +1 -0
  153. package/dist/src/plugin/recovery/constants.js +43 -0
  154. package/dist/src/plugin/recovery/constants.js.map +1 -0
  155. package/dist/src/plugin/recovery/index.d.ts +12 -0
  156. package/dist/src/plugin/recovery/index.d.ts.map +1 -0
  157. package/dist/src/plugin/recovery/index.js +12 -0
  158. package/dist/src/plugin/recovery/index.js.map +1 -0
  159. package/dist/src/plugin/recovery/storage.d.ts +24 -0
  160. package/dist/src/plugin/recovery/storage.d.ts.map +1 -0
  161. package/dist/src/plugin/recovery/storage.js +354 -0
  162. package/dist/src/plugin/recovery/storage.js.map +1 -0
  163. package/dist/src/plugin/recovery/types.d.ts +116 -0
  164. package/dist/src/plugin/recovery/types.d.ts.map +1 -0
  165. package/dist/src/plugin/recovery/types.js +6 -0
  166. package/dist/src/plugin/recovery/types.js.map +1 -0
  167. package/dist/src/plugin/recovery.d.ts +61 -0
  168. package/dist/src/plugin/recovery.d.ts.map +1 -0
  169. package/dist/src/plugin/recovery.js +381 -0
  170. package/dist/src/plugin/recovery.js.map +1 -0
  171. package/dist/src/plugin/refresh-queue.d.ts +100 -0
  172. package/dist/src/plugin/refresh-queue.d.ts.map +1 -0
  173. package/dist/src/plugin/refresh-queue.js +247 -0
  174. package/dist/src/plugin/refresh-queue.js.map +1 -0
  175. package/dist/src/plugin/request-helpers.d.ts +278 -0
  176. package/dist/src/plugin/request-helpers.d.ts.map +1 -0
  177. package/dist/src/plugin/request-helpers.js +2323 -0
  178. package/dist/src/plugin/request-helpers.js.map +1 -0
  179. package/dist/src/plugin/request.d.ts +102 -0
  180. package/dist/src/plugin/request.d.ts.map +1 -0
  181. package/dist/src/plugin/request.js +1658 -0
  182. package/dist/src/plugin/request.js.map +1 -0
  183. package/dist/src/plugin/rotation.d.ts +169 -0
  184. package/dist/src/plugin/rotation.d.ts.map +1 -0
  185. package/dist/src/plugin/rotation.js +328 -0
  186. package/dist/src/plugin/rotation.js.map +1 -0
  187. package/dist/src/plugin/search.d.ts +25 -0
  188. package/dist/src/plugin/search.d.ts.map +1 -0
  189. package/dist/src/plugin/search.js +200 -0
  190. package/dist/src/plugin/search.js.map +1 -0
  191. package/dist/src/plugin/server.d.ts +23 -0
  192. package/dist/src/plugin/server.d.ts.map +1 -0
  193. package/dist/src/plugin/server.js +324 -0
  194. package/dist/src/plugin/server.js.map +1 -0
  195. package/dist/src/plugin/storage.d.ts +150 -0
  196. package/dist/src/plugin/storage.d.ts.map +1 -0
  197. package/dist/src/plugin/storage.js +628 -0
  198. package/dist/src/plugin/storage.js.map +1 -0
  199. package/dist/src/plugin/stores/signature-store.d.ts +5 -0
  200. package/dist/src/plugin/stores/signature-store.d.ts.map +1 -0
  201. package/dist/src/plugin/stores/signature-store.js +25 -0
  202. package/dist/src/plugin/stores/signature-store.js.map +1 -0
  203. package/dist/src/plugin/thinking-recovery.d.ts +90 -0
  204. package/dist/src/plugin/thinking-recovery.d.ts.map +1 -0
  205. package/dist/src/plugin/thinking-recovery.js +330 -0
  206. package/dist/src/plugin/thinking-recovery.js.map +1 -0
  207. package/dist/src/plugin/token.d.ts +19 -0
  208. package/dist/src/plugin/token.d.ts.map +1 -0
  209. package/dist/src/plugin/token.js +130 -0
  210. package/dist/src/plugin/token.js.map +1 -0
  211. package/dist/src/plugin/transform/claude.d.ts +92 -0
  212. package/dist/src/plugin/transform/claude.d.ts.map +1 -0
  213. package/dist/src/plugin/transform/claude.js +280 -0
  214. package/dist/src/plugin/transform/claude.js.map +1 -0
  215. package/dist/src/plugin/transform/cross-model-sanitizer.d.ts +2 -0
  216. package/dist/src/plugin/transform/cross-model-sanitizer.d.ts.map +1 -0
  217. package/dist/src/plugin/transform/cross-model-sanitizer.js +3 -0
  218. package/dist/src/plugin/transform/cross-model-sanitizer.js.map +1 -0
  219. package/dist/src/plugin/transform/gemini.d.ts +100 -0
  220. package/dist/src/plugin/transform/gemini.d.ts.map +1 -0
  221. package/dist/src/plugin/transform/gemini.js +446 -0
  222. package/dist/src/plugin/transform/gemini.js.map +1 -0
  223. package/dist/src/plugin/transform/index.d.ts +2 -0
  224. package/dist/src/plugin/transform/index.d.ts.map +1 -0
  225. package/dist/src/plugin/transform/index.js +3 -0
  226. package/dist/src/plugin/transform/index.js.map +1 -0
  227. package/dist/src/plugin/transform/model-resolver.d.ts +2 -0
  228. package/dist/src/plugin/transform/model-resolver.d.ts.map +1 -0
  229. package/dist/src/plugin/transform/model-resolver.js +3 -0
  230. package/dist/src/plugin/transform/model-resolver.js.map +1 -0
  231. package/dist/src/plugin/transform/types.d.ts +2 -0
  232. package/dist/src/plugin/transform/types.d.ts.map +1 -0
  233. package/dist/src/plugin/transform/types.js +3 -0
  234. package/dist/src/plugin/transform/types.js.map +1 -0
  235. package/dist/src/plugin/types.d.ts +80 -0
  236. package/dist/src/plugin/types.d.ts.map +1 -0
  237. package/dist/src/plugin/types.js +1 -0
  238. package/dist/src/plugin/types.js.map +1 -0
  239. package/dist/src/plugin/ui/ansi.d.ts +32 -0
  240. package/dist/src/plugin/ui/ansi.d.ts.map +1 -0
  241. package/dist/src/plugin/ui/ansi.js +52 -0
  242. package/dist/src/plugin/ui/ansi.js.map +1 -0
  243. package/dist/src/plugin/ui/auth-menu.d.ts +59 -0
  244. package/dist/src/plugin/ui/auth-menu.d.ts.map +1 -0
  245. package/dist/src/plugin/ui/auth-menu.js +362 -0
  246. package/dist/src/plugin/ui/auth-menu.js.map +1 -0
  247. package/dist/src/plugin/ui/confirm.d.ts +2 -0
  248. package/dist/src/plugin/ui/confirm.d.ts.map +1 -0
  249. package/dist/src/plugin/ui/confirm.js +15 -0
  250. package/dist/src/plugin/ui/confirm.js.map +1 -0
  251. package/dist/src/plugin/ui/model-status.d.ts +28 -0
  252. package/dist/src/plugin/ui/model-status.d.ts.map +1 -0
  253. package/dist/src/plugin/ui/model-status.js +80 -0
  254. package/dist/src/plugin/ui/model-status.js.map +1 -0
  255. package/dist/src/plugin/ui/quota-status.d.ts +82 -0
  256. package/dist/src/plugin/ui/quota-status.d.ts.map +1 -0
  257. package/dist/src/plugin/ui/quota-status.js +251 -0
  258. package/dist/src/plugin/ui/quota-status.js.map +1 -0
  259. package/dist/src/plugin/ui/select.d.ts +23 -0
  260. package/dist/src/plugin/ui/select.d.ts.map +1 -0
  261. package/dist/src/plugin/ui/select.js +254 -0
  262. package/dist/src/plugin/ui/select.js.map +1 -0
  263. package/dist/src/plugin/version.d.ts +2 -0
  264. package/dist/src/plugin/version.d.ts.map +1 -0
  265. package/dist/src/plugin/version.js +3 -0
  266. package/dist/src/plugin/version.js.map +1 -0
  267. package/dist/src/plugin.d.ts +32 -0
  268. package/dist/src/plugin.d.ts.map +1 -0
  269. package/dist/src/plugin.js +3125 -0
  270. package/dist/src/plugin.js.map +1 -0
  271. package/package.json +70 -0
@@ -0,0 +1,375 @@
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
+ // Path Utilities
19
+ // =============================================================================
20
+ function getConfigDir() {
21
+ const platform = process.platform;
22
+ if (platform === "win32") {
23
+ return join(process.env.APPDATA || join(homedir(), "AppData", "Roaming"), "opencode");
24
+ }
25
+ const xdgConfig = process.env.XDG_CONFIG_HOME || join(homedir(), ".config");
26
+ return join(xdgConfig, "opencode");
27
+ }
28
+ function getCacheFilePath() {
29
+ return join(getConfigDir(), "antigravity-signature-cache.json");
30
+ }
31
+ // =============================================================================
32
+ // Signature Cache Class
33
+ // =============================================================================
34
+ export class SignatureCache {
35
+ // In-memory cache: key -> entry with signature and optional thinking text
36
+ cache = new Map();
37
+ // Configuration
38
+ memoryTtlMs;
39
+ diskTtlMs;
40
+ writeIntervalMs;
41
+ cacheFilePath;
42
+ enabled;
43
+ // State
44
+ dirty = false;
45
+ writeTimer = null;
46
+ cleanupTimer = null;
47
+ // Statistics
48
+ stats = {
49
+ memoryHits: 0,
50
+ diskHits: 0,
51
+ misses: 0,
52
+ writes: 0,
53
+ };
54
+ constructor(config) {
55
+ this.enabled = config.enabled;
56
+ this.memoryTtlMs = config.memory_ttl_seconds * 1000;
57
+ this.diskTtlMs = config.disk_ttl_seconds * 1000;
58
+ this.writeIntervalMs = config.write_interval_seconds * 1000;
59
+ this.cacheFilePath = getCacheFilePath();
60
+ if (this.enabled) {
61
+ this.loadFromDisk();
62
+ this.startBackgroundTasks();
63
+ }
64
+ }
65
+ // ===========================================================================
66
+ // Public API
67
+ // ===========================================================================
68
+ /**
69
+ * Generate a cache key from sessionId and modelId.
70
+ */
71
+ static makeKey(sessionId, modelId) {
72
+ return `${sessionId}:${modelId}`;
73
+ }
74
+ /**
75
+ * Store a signature in the cache.
76
+ */
77
+ store(key, signature) {
78
+ if (!this.enabled)
79
+ return;
80
+ this.cache.set(key, {
81
+ value: signature,
82
+ timestamp: Date.now(),
83
+ });
84
+ this.dirty = true;
85
+ }
86
+ /**
87
+ * Retrieve a signature from the cache.
88
+ * Returns null if not found or expired.
89
+ */
90
+ retrieve(key) {
91
+ if (!this.enabled)
92
+ return null;
93
+ const entry = this.cache.get(key);
94
+ if (entry) {
95
+ const age = Date.now() - entry.timestamp;
96
+ if (age <= this.memoryTtlMs) {
97
+ this.stats.memoryHits++;
98
+ return entry.value;
99
+ }
100
+ // Expired from memory, remove it
101
+ this.cache.delete(key);
102
+ }
103
+ this.stats.misses++;
104
+ return null;
105
+ }
106
+ /**
107
+ * Check if a key exists in the cache (without updating stats).
108
+ */
109
+ has(key) {
110
+ if (!this.enabled)
111
+ return false;
112
+ const entry = this.cache.get(key);
113
+ if (!entry)
114
+ return false;
115
+ const age = Date.now() - entry.timestamp;
116
+ return age <= this.memoryTtlMs;
117
+ }
118
+ // ===========================================================================
119
+ // Full Thinking Cache (ported from LLM-API-Key-Proxy)
120
+ // ===========================================================================
121
+ /**
122
+ * Store full thinking content with signature.
123
+ * This enables recovery even after thinking text is stripped by compaction.
124
+ *
125
+ * Port of LLM-API-Key-Proxy's _cache_thinking()
126
+ */
127
+ storeThinking(key, thinkingText, signature, toolIds) {
128
+ if (!this.enabled || !thinkingText || !signature)
129
+ return;
130
+ this.cache.set(key, {
131
+ value: signature,
132
+ timestamp: Date.now(),
133
+ thinkingText,
134
+ textPreview: thinkingText.slice(0, 100),
135
+ toolIds,
136
+ });
137
+ this.dirty = true;
138
+ }
139
+ /**
140
+ * Retrieve full thinking content by key.
141
+ * Returns null if not found or expired.
142
+ */
143
+ retrieveThinking(key) {
144
+ if (!this.enabled)
145
+ return null;
146
+ const entry = this.cache.get(key);
147
+ if (!entry || !entry.thinkingText)
148
+ return null;
149
+ const age = Date.now() - entry.timestamp;
150
+ if (age > this.memoryTtlMs) {
151
+ this.cache.delete(key);
152
+ return null;
153
+ }
154
+ this.stats.memoryHits++;
155
+ return {
156
+ text: entry.thinkingText,
157
+ signature: entry.value,
158
+ toolIds: entry.toolIds,
159
+ };
160
+ }
161
+ /**
162
+ * Check if full thinking content exists for a key.
163
+ */
164
+ hasThinking(key) {
165
+ if (!this.enabled)
166
+ return false;
167
+ const entry = this.cache.get(key);
168
+ if (!entry || !entry.thinkingText)
169
+ return false;
170
+ const age = Date.now() - entry.timestamp;
171
+ return age <= this.memoryTtlMs;
172
+ }
173
+ /**
174
+ * Get cache statistics.
175
+ */
176
+ getStats() {
177
+ return {
178
+ ...this.stats,
179
+ memoryEntries: this.cache.size,
180
+ dirty: this.dirty,
181
+ diskEnabled: this.enabled,
182
+ };
183
+ }
184
+ /**
185
+ * Manually trigger a disk save.
186
+ */
187
+ async flush() {
188
+ if (!this.enabled)
189
+ return true;
190
+ return this.saveToDisk();
191
+ }
192
+ /**
193
+ * Graceful shutdown: stop timers and flush to disk.
194
+ */
195
+ shutdown() {
196
+ if (this.writeTimer) {
197
+ clearInterval(this.writeTimer);
198
+ this.writeTimer = null;
199
+ }
200
+ if (this.cleanupTimer) {
201
+ clearInterval(this.cleanupTimer);
202
+ this.cleanupTimer = null;
203
+ }
204
+ if (this.dirty && this.enabled) {
205
+ this.saveToDisk();
206
+ }
207
+ }
208
+ // ===========================================================================
209
+ // Disk Operations
210
+ // ===========================================================================
211
+ /**
212
+ * Load cache from disk file with TTL validation.
213
+ */
214
+ loadFromDisk() {
215
+ try {
216
+ if (!existsSync(this.cacheFilePath)) {
217
+ return;
218
+ }
219
+ const content = readFileSync(this.cacheFilePath, "utf-8");
220
+ const data = JSON.parse(content);
221
+ if (data.version !== "1.0") {
222
+ // Version mismatch - silently start fresh
223
+ return;
224
+ }
225
+ const now = Date.now();
226
+ let loaded = 0;
227
+ let expired = 0;
228
+ for (const [key, entry] of Object.entries(data.entries)) {
229
+ const age = now - entry.timestamp;
230
+ if (age <= this.diskTtlMs) {
231
+ this.cache.set(key, {
232
+ value: entry.value,
233
+ timestamp: entry.timestamp,
234
+ });
235
+ loaded++;
236
+ }
237
+ else {
238
+ expired++;
239
+ }
240
+ }
241
+ // Silently load - no console output
242
+ }
243
+ catch {
244
+ // Silently start fresh on any error (corruption, file not found, etc.)
245
+ }
246
+ }
247
+ /**
248
+ * Save cache to disk with atomic write pattern.
249
+ * Merges with existing disk entries that haven't expired.
250
+ */
251
+ saveToDisk() {
252
+ try {
253
+ // Ensure directory exists
254
+ const dir = dirname(this.cacheFilePath);
255
+ if (!existsSync(dir)) {
256
+ mkdirSync(dir, { recursive: true });
257
+ }
258
+ ensureGitignoreSync(dir);
259
+ const now = Date.now();
260
+ // Step 1: Load existing disk entries (if any)
261
+ let existingEntries = {};
262
+ if (existsSync(this.cacheFilePath)) {
263
+ try {
264
+ const content = readFileSync(this.cacheFilePath, "utf-8");
265
+ const data = JSON.parse(content);
266
+ existingEntries = data.entries || {};
267
+ }
268
+ catch {
269
+ // Start fresh if corrupted
270
+ }
271
+ }
272
+ // Step 2: Filter existing disk entries by disk_ttl
273
+ const validDiskEntries = {};
274
+ for (const [key, entry] of Object.entries(existingEntries)) {
275
+ const age = now - entry.timestamp;
276
+ if (age <= this.diskTtlMs) {
277
+ validDiskEntries[key] = entry;
278
+ }
279
+ }
280
+ // Step 3: Merge - memory entries take precedence
281
+ const mergedEntries = { ...validDiskEntries };
282
+ for (const [key, entry] of this.cache.entries()) {
283
+ mergedEntries[key] = {
284
+ value: entry.value,
285
+ timestamp: entry.timestamp,
286
+ };
287
+ }
288
+ // Step 4: Build cache data
289
+ const cacheData = {
290
+ version: "1.0",
291
+ memory_ttl_seconds: this.memoryTtlMs / 1000,
292
+ disk_ttl_seconds: this.diskTtlMs / 1000,
293
+ entries: mergedEntries,
294
+ statistics: {
295
+ memory_hits: this.stats.memoryHits,
296
+ disk_hits: this.stats.diskHits,
297
+ misses: this.stats.misses,
298
+ writes: this.stats.writes + 1,
299
+ last_write: now,
300
+ },
301
+ };
302
+ // Step 5: Atomic write (temp file + rename)
303
+ const tmpPath = join(tmpdir(), `antigravity-cache-${Date.now()}-${Math.random().toString(36).slice(2)}.tmp`);
304
+ writeFileSync(tmpPath, JSON.stringify(cacheData, null, 2), "utf-8");
305
+ try {
306
+ renameSync(tmpPath, this.cacheFilePath);
307
+ }
308
+ catch {
309
+ // On Windows, rename across volumes may fail
310
+ // Fall back to copy + delete
311
+ writeFileSync(this.cacheFilePath, readFileSync(tmpPath));
312
+ try {
313
+ unlinkSync(tmpPath);
314
+ }
315
+ catch {
316
+ // Ignore cleanup errors
317
+ }
318
+ }
319
+ this.stats.writes++;
320
+ this.dirty = false;
321
+ return true;
322
+ }
323
+ catch {
324
+ // Silently fail - disk cache is optional
325
+ return false;
326
+ }
327
+ }
328
+ // ===========================================================================
329
+ // Background Tasks
330
+ // ===========================================================================
331
+ /**
332
+ * Start background write and cleanup timers.
333
+ */
334
+ startBackgroundTasks() {
335
+ // Periodic disk writes
336
+ this.writeTimer = setInterval(() => {
337
+ if (this.dirty) {
338
+ this.saveToDisk();
339
+ }
340
+ }, this.writeIntervalMs);
341
+ // Periodic memory cleanup (every 30 minutes)
342
+ this.cleanupTimer = setInterval(() => {
343
+ this.cleanupExpired();
344
+ }, 30 * 60 * 1000);
345
+ }
346
+ /**
347
+ * Remove expired entries from memory.
348
+ */
349
+ cleanupExpired() {
350
+ const now = Date.now();
351
+ let cleaned = 0;
352
+ for (const [key, entry] of this.cache.entries()) {
353
+ const age = now - entry.timestamp;
354
+ if (age > this.memoryTtlMs) {
355
+ this.cache.delete(key);
356
+ cleaned++;
357
+ }
358
+ }
359
+ // Silently clean - no console output
360
+ }
361
+ }
362
+ // =============================================================================
363
+ // Factory Function
364
+ // =============================================================================
365
+ /**
366
+ * Create a signature cache with the given configuration.
367
+ * Returns null if caching is disabled.
368
+ */
369
+ export function createSignatureCache(config) {
370
+ if (!config || !config.enabled) {
371
+ return null;
372
+ }
373
+ return new SignatureCache(config);
374
+ }
375
+ //# sourceMappingURL=signature-cache.js.map
@@ -0,0 +1 @@
1
+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AC,CAAC;QAEzB,6CAA6C;QAC7C,IAAI,CAAC,YAAY,GAAG,WAAW,CAAC,GAAG,EAAE;YACnC,IAAI,CAAC,cAAc,EAAE,CAAC;QACxB,CAAC,EAAE,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC;IACrB,CAAC;IAED;;OAEG;IACK,cAAc;QACpB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,IAAI,OAAO,GAAG,CAAC,CAAC;QAEhB,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;YAChD,MAAM,GAAG,GAAG,GAAG,GAAG,KAAK,CAAC,SAAS,CAAC;YAClC,IAAI,GAAG,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;gBAC3B,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBACvB,OAAO,EAAE,CAAC;YACZ,CAAC;QACH,CAAC;QAED,qCAAqC;IACvC,CAAC;CACF;AAED,gFAAgF;AAChF,mBAAmB;AACnB,gFAAgF;AAEhF;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAAC,MAAwC;IAC3E,IAAI,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QAC/B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC;AACpC,CAAC"}
@@ -0,0 +1,44 @@
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";
44
+ //# sourceMappingURL=cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../../src/plugin/cache.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAahD;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,gBAAgB,GAAG,gBAAgB,CAuB1E;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,gBAAgB,GAAG,IAAI,CAM5D;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAStD;AAMD,OAAO,EAAE,cAAc,EAAwB,MAAM,yBAAyB,CAAC;AAC/E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAwBrD;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,oBAAoB,GAAG,SAAS,GAAG,cAAc,GAAG,IAAI,CAGtG;AAED;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,cAAc,GAAG,IAAI,CAE7D;AA8DD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,IAAI,CAuCvF;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAoCtF;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAU5D;AAOD,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAC/E,YAAY,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC"}
@@ -0,0 +1,247 @@
1
+ import { accessTokenExpired } from "./auth";
2
+ import { createHash } from "node:crypto";
3
+ const authCache = new Map();
4
+ /**
5
+ * Produces a stable cache key from a refresh token string.
6
+ */
7
+ function normalizeRefreshKey(refresh) {
8
+ const key = refresh?.trim();
9
+ return key ? key : undefined;
10
+ }
11
+ /**
12
+ * Returns a cached auth snapshot when available, favoring unexpired tokens.
13
+ */
14
+ export function resolveCachedAuth(auth) {
15
+ const key = normalizeRefreshKey(auth.refresh);
16
+ if (!key) {
17
+ return auth;
18
+ }
19
+ const cached = authCache.get(key);
20
+ if (!cached) {
21
+ authCache.set(key, auth);
22
+ return auth;
23
+ }
24
+ if (!accessTokenExpired(auth)) {
25
+ authCache.set(key, auth);
26
+ return auth;
27
+ }
28
+ if (!accessTokenExpired(cached)) {
29
+ return cached;
30
+ }
31
+ authCache.set(key, auth);
32
+ return auth;
33
+ }
34
+ /**
35
+ * Stores the latest auth snapshot keyed by refresh token.
36
+ */
37
+ export function storeCachedAuth(auth) {
38
+ const key = normalizeRefreshKey(auth.refresh);
39
+ if (!key) {
40
+ return;
41
+ }
42
+ authCache.set(key, auth);
43
+ }
44
+ /**
45
+ * Clears cached auth globally or for a specific refresh token.
46
+ */
47
+ export function clearCachedAuth(refresh) {
48
+ if (!refresh) {
49
+ authCache.clear();
50
+ return;
51
+ }
52
+ const key = normalizeRefreshKey(refresh);
53
+ if (key) {
54
+ authCache.delete(key);
55
+ }
56
+ }
57
+ // ============================================================================
58
+ // Thinking Signature Cache (for Claude multi-turn conversations)
59
+ // ============================================================================
60
+ import { SignatureCache, createSignatureCache } from "./cache/signature-cache";
61
+ // Map: sessionId -> Map<textHash, SignatureEntry>
62
+ const signatureCache = new Map();
63
+ // Cache entries expire after 1 hour
64
+ const SIGNATURE_CACHE_TTL_MS = 60 * 60 * 1000;
65
+ // Maximum entries per session to prevent memory bloat
66
+ const MAX_ENTRIES_PER_SESSION = 100;
67
+ // Maximum sessions tracked in the outer Map to prevent unbounded growth
68
+ const MAX_CACHED_SESSIONS = 10;
69
+ // 16 hex chars = 64-bit key space; keeps memory bounded while making collisions extremely unlikely.
70
+ const SIGNATURE_TEXT_HASH_HEX_LEN = 16;
71
+ // Disk cache instance (initialized via initDiskSignatureCache)
72
+ let diskCache = null;
73
+ /**
74
+ * Initialize the disk-based signature cache.
75
+ * Call this from plugin initialization when keep_thinking is enabled.
76
+ */
77
+ export function initDiskSignatureCache(config) {
78
+ diskCache = createSignatureCache(config);
79
+ return diskCache;
80
+ }
81
+ /**
82
+ * Get the disk cache instance (for testing/debugging).
83
+ */
84
+ export function getDiskSignatureCache() {
85
+ return diskCache;
86
+ }
87
+ /**
88
+ * Hashes text content into a stable, Unicode-safe key.
89
+ *
90
+ * Uses SHA-256 over UTF-8 bytes and truncates to keep memory usage bounded.
91
+ */
92
+ function hashText(text) {
93
+ return createHash("sha256").update(text, "utf8").digest("hex").slice(0, SIGNATURE_TEXT_HASH_HEX_LEN);
94
+ }
95
+ /**
96
+ * Create a disk cache key from sessionId and textHash.
97
+ */
98
+ function makeDiskKey(sessionId, textHash) {
99
+ return `${sessionId}:${textHash}`;
100
+ }
101
+ /**
102
+ * Prune stale sessions from the outer signatureCache Map.
103
+ * Removes sessions where all entries have expired, then evicts the
104
+ * oldest sessions if the Map still exceeds MAX_CACHED_SESSIONS.
105
+ */
106
+ function pruneSignatureSessions() {
107
+ if (signatureCache.size <= MAX_CACHED_SESSIONS)
108
+ return;
109
+ const now = Date.now();
110
+ // First pass: remove sessions where ALL entries are expired
111
+ for (const [sid, innerMap] of signatureCache) {
112
+ let allExpired = true;
113
+ for (const entry of innerMap.values()) {
114
+ if (now - entry.timestamp <= SIGNATURE_CACHE_TTL_MS) {
115
+ allExpired = false;
116
+ break;
117
+ }
118
+ }
119
+ if (allExpired) {
120
+ signatureCache.delete(sid);
121
+ }
122
+ }
123
+ // Second pass: if still over cap, evict oldest sessions by newest entry timestamp
124
+ if (signatureCache.size > MAX_CACHED_SESSIONS) {
125
+ const sessionsByAge = [];
126
+ for (const [sid, innerMap] of signatureCache) {
127
+ let newestTs = 0;
128
+ for (const entry of innerMap.values()) {
129
+ if (entry.timestamp > newestTs)
130
+ newestTs = entry.timestamp;
131
+ }
132
+ sessionsByAge.push({ sid, newestTs });
133
+ }
134
+ // Sort oldest-first, evict until at cap
135
+ sessionsByAge.sort((a, b) => a.newestTs - b.newestTs);
136
+ const toEvict = signatureCache.size - MAX_CACHED_SESSIONS;
137
+ for (let i = 0; i < toEvict; i++) {
138
+ const entry = sessionsByAge[i];
139
+ if (entry)
140
+ signatureCache.delete(entry.sid);
141
+ }
142
+ }
143
+ }
144
+ /**
145
+ * Caches a thinking signature for a given session and text.
146
+ * Used for Claude models that require signed thinking blocks in multi-turn conversations.
147
+ * Also writes to disk cache if enabled.
148
+ */
149
+ export function cacheSignature(sessionId, text, signature) {
150
+ if (!sessionId || !text || !signature)
151
+ return;
152
+ const textHash = hashText(text);
153
+ // Write to memory cache
154
+ let sessionMemCache = signatureCache.get(sessionId);
155
+ if (!sessionMemCache) {
156
+ // About to add a new session — prune stale ones first
157
+ pruneSignatureSessions();
158
+ sessionMemCache = new Map();
159
+ signatureCache.set(sessionId, sessionMemCache);
160
+ }
161
+ // Evict old entries if we're at capacity
162
+ if (sessionMemCache.size >= MAX_ENTRIES_PER_SESSION) {
163
+ const now = Date.now();
164
+ for (const [key, entry] of sessionMemCache.entries()) {
165
+ if (now - entry.timestamp > SIGNATURE_CACHE_TTL_MS) {
166
+ sessionMemCache.delete(key);
167
+ }
168
+ }
169
+ // If still at capacity, remove oldest entries
170
+ if (sessionMemCache.size >= MAX_ENTRIES_PER_SESSION) {
171
+ const entries = Array.from(sessionMemCache.entries())
172
+ .sort((a, b) => a[1].timestamp - b[1].timestamp);
173
+ const toRemove = entries.slice(0, Math.floor(MAX_ENTRIES_PER_SESSION / 4));
174
+ for (const [key] of toRemove) {
175
+ sessionMemCache.delete(key);
176
+ }
177
+ }
178
+ }
179
+ sessionMemCache.set(textHash, { signature, timestamp: Date.now() });
180
+ // Write to disk cache if enabled
181
+ if (diskCache) {
182
+ const diskKey = makeDiskKey(sessionId, textHash);
183
+ diskCache.store(diskKey, signature);
184
+ }
185
+ }
186
+ /**
187
+ * Retrieves a cached signature for a given session and text.
188
+ * Checks memory first, then falls back to disk cache.
189
+ * Returns undefined if not found or expired.
190
+ */
191
+ export function getCachedSignature(sessionId, text) {
192
+ if (!sessionId || !text)
193
+ return undefined;
194
+ const textHash = hashText(text);
195
+ // Check memory cache first
196
+ const sessionMemCache = signatureCache.get(sessionId);
197
+ if (sessionMemCache) {
198
+ const entry = sessionMemCache.get(textHash);
199
+ if (entry) {
200
+ // Check if expired
201
+ if (Date.now() - entry.timestamp > SIGNATURE_CACHE_TTL_MS) {
202
+ sessionMemCache.delete(textHash);
203
+ }
204
+ else {
205
+ return entry.signature;
206
+ }
207
+ }
208
+ }
209
+ // Fall back to disk cache
210
+ if (diskCache) {
211
+ const diskKey = makeDiskKey(sessionId, textHash);
212
+ const diskValue = diskCache.retrieve(diskKey);
213
+ if (diskValue) {
214
+ // Promote to memory cache for faster subsequent access
215
+ let memCache = signatureCache.get(sessionId);
216
+ if (!memCache) {
217
+ memCache = new Map();
218
+ signatureCache.set(sessionId, memCache);
219
+ }
220
+ memCache.set(textHash, { signature: diskValue, timestamp: Date.now() });
221
+ return diskValue;
222
+ }
223
+ }
224
+ return undefined;
225
+ }
226
+ /**
227
+ * Clears signature cache for a specific session or all sessions.
228
+ * Also clears from disk cache if enabled.
229
+ */
230
+ export function clearSignatureCache(sessionId) {
231
+ if (sessionId) {
232
+ signatureCache.delete(sessionId);
233
+ // Note: We don't clear individual sessions from disk cache to avoid
234
+ // expensive iteration. Disk cache entries will expire naturally.
235
+ }
236
+ else {
237
+ signatureCache.clear();
238
+ // For full clear, we could clear disk cache, but leaving it for now
239
+ // since entries have TTL and will expire naturally.
240
+ }
241
+ }
242
+ // ============================================================================
243
+ // Disk-Persistent Signature Cache (re-export from cache/ folder)
244
+ // ============================================================================
245
+ // Re-export SignatureCache class and factory for direct use
246
+ export { SignatureCache, createSignatureCache } from "./cache/signature-cache";
247
+ //# sourceMappingURL=cache.js.map