antigravity-claude-proxy 2.7.4 → 2.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "antigravity-claude-proxy",
3
- "version": "2.7.4",
3
+ "version": "2.7.6",
4
4
  "description": "Proxy server to use Antigravity's Claude models with Claude Code CLI",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -63,7 +63,8 @@
63
63
  "better-sqlite3": "^12.5.0",
64
64
  "cors": "^2.8.5",
65
65
  "express": "^4.18.2",
66
- "undici": "^7.20.0"
66
+ "undici": "^7.20.0",
67
+ "wreq-js": "^2.0.1"
67
68
  },
68
69
  "devDependencies": {
69
70
  "@tailwindcss/forms": "^0.5.7",
@@ -157,7 +157,7 @@ export async function sendMessage(anthropicRequest, accountManager, fallbackEnab
157
157
 
158
158
  const response = await throttledFetch(url, {
159
159
  method: 'POST',
160
- headers: buildHeaders(token, model, isThinking ? 'text/event-stream' : 'application/json'),
160
+ headers: buildHeaders(token, model, isThinking ? 'text/event-stream' : 'application/json', payload.request.sessionId),
161
161
  body: JSON.stringify(payload)
162
162
  });
163
163
 
@@ -70,15 +70,21 @@ export function buildCloudCodeRequest(anthropicRequest, projectId, accountEmail)
70
70
  * @param {string} token - OAuth access token
71
71
  * @param {string} model - Model name
72
72
  * @param {string} accept - Accept header value (default: 'application/json')
73
+ * @param {string} [sessionId] - Optional session ID for X-Machine-Session-Id header
73
74
  * @returns {Object} Headers object
74
75
  */
75
- export function buildHeaders(token, model, accept = 'application/json') {
76
+ export function buildHeaders(token, model, accept = 'application/json', sessionId) {
76
77
  const headers = {
77
78
  'Authorization': `Bearer ${token}`,
78
79
  'Content-Type': 'application/json',
79
80
  ...ANTIGRAVITY_HEADERS
80
81
  };
81
82
 
83
+ // Add session ID header if provided (matches Antigravity binary behavior)
84
+ if (sessionId) {
85
+ headers['X-Machine-Session-Id'] = sessionId;
86
+ }
87
+
82
88
  const modelFamily = getModelFamily(model);
83
89
 
84
90
  // Add interleaved thinking header only for Claude thinking models
@@ -8,46 +8,59 @@
8
8
 
9
9
  import crypto from 'crypto';
10
10
 
11
+
12
+ // Runtime storage for session IDs (per account)
13
+ // This mimics the behavior of the binary which generates a session ID at startup
14
+ // and keeps it for the process lifetime.
15
+ // Key: accountEmail, Value: sessionId
16
+ const runtimeSessionStore = new Map();
17
+
11
18
  /**
12
- * Derive a stable session ID from the first user message in the conversation.
13
- * This ensures the same conversation uses the same session ID across turns,
14
- * enabling prompt caching (cache is scoped to session + organization).
19
+ * Get or create a session ID for the given account.
20
+ *
21
+ * The binary generates a session ID once at startup: `p.sessionID = rs() + Date.now()`.
22
+ * Since our proxy is long-running, we simulate this "per-launch" behavior by storing
23
+ * a generated ID in memory for each account.
15
24
  *
16
- * @param {Object} anthropicRequest - The Anthropic-format request
17
- * @param {string} accountEmail - The account email to make session IDs unique per account
18
- * @returns {string} A stable session ID (32 hex characters) or random UUID if no user message
25
+ * - If the proxy restarts, the ID changes (matching binary/VS Code restart behavior).
26
+ * - Within a running proxy instance, the ID is stable for that account.
27
+ * - This enables prompt caching while using the EXACT random logic of the binary.
28
+ *
29
+ * @param {Object} anthropicRequest - The Anthropic-format request (unused for ID generation now)
30
+ * @param {string} accountEmail - The account email to scope the session ID
31
+ * @returns {string} A stable session ID string matching binary format
19
32
  */
20
33
  export function deriveSessionId(anthropicRequest, accountEmail) {
21
- const messages = anthropicRequest.messages || [];
22
-
23
- // Find the first user message
24
- for (const msg of messages) {
25
- if (msg.role === 'user') {
26
- let content = '';
27
-
28
- if (typeof msg.content === 'string') {
29
- content = msg.content;
30
- } else if (Array.isArray(msg.content)) {
31
- // Extract text from content blocks
32
- content = msg.content
33
- .filter(block => block.type === 'text' && block.text)
34
- .map(block => block.text)
35
- .join('\n');
36
- }
37
-
38
- if (content) {
39
- // Include account email in the content to be hashed to ensure
40
- // unique session IDs per account for the same conversation.
41
- // This prevents Google from correlating sessions across accounts.
42
- const saltedContent = accountEmail ? `${accountEmail}:${content}` : content;
43
-
44
- // Hash the content with SHA256, return first 32 hex chars
45
- const hash = crypto.createHash('sha256').update(saltedContent).digest('hex');
46
- return hash.substring(0, 32);
47
- }
48
- }
34
+ if (!accountEmail) {
35
+ // Fallback for requests without an account (should differ every time)
36
+ return generateBinaryStyleId();
37
+ }
38
+
39
+ // Check if we already have a session ID for this account in this process run
40
+ if (runtimeSessionStore.has(accountEmail)) {
41
+ return runtimeSessionStore.get(accountEmail);
49
42
  }
50
43
 
51
- // Fallback to random UUID if no user message found
52
- return crypto.randomUUID();
44
+ // Generate a new ID using the binary's exact logic
45
+ const newSessionId = generateBinaryStyleId();
46
+
47
+ // Store it for future requests from this account
48
+ runtimeSessionStore.set(accountEmail, newSessionId);
49
+
50
+ return newSessionId;
51
+ }
52
+
53
+ /**
54
+ * Generate a Session ID using the binary's exact logic.
55
+ * logic: `rs() + Date.now()` where `rs()` is randomUUID
56
+ */
57
+ function generateBinaryStyleId() {
58
+ return crypto.randomUUID() + Date.now().toString();
59
+ }
60
+
61
+ /**
62
+ * Clears all session IDs (e.g. useful for testing or explicit reset)
63
+ */
64
+ export function clearSessionStore() {
65
+ runtimeSessionStore.clear();
53
66
  }
@@ -155,7 +155,7 @@ export async function* sendMessageStream(anthropicRequest, accountManager, fallb
155
155
 
156
156
  const response = await throttledFetch(url, {
157
157
  method: 'POST',
158
- headers: buildHeaders(token, model, 'text/event-stream'),
158
+ headers: buildHeaders(token, model, 'text/event-stream', payload.request.sessionId),
159
159
  body: JSON.stringify(payload)
160
160
  });
161
161
 
@@ -336,7 +336,7 @@ export async function* sendMessageStream(anthropicRequest, accountManager, fallb
336
336
  // Refetch the response
337
337
  currentResponse = await throttledFetch(url, {
338
338
  method: 'POST',
339
- headers: buildHeaders(token, model, 'text/event-stream'),
339
+ headers: buildHeaders(token, model, 'text/event-stream', payload.request.sessionId),
340
340
  body: JSON.stringify(payload)
341
341
  });
342
342
 
package/src/constants.js CHANGED
@@ -6,6 +6,7 @@
6
6
  import { homedir, platform, arch } from 'os';
7
7
  import { join } from 'path';
8
8
  import { config } from './config.js';
9
+ import { generateSmartUserAgent } from './utils/version-detector.js';
9
10
 
10
11
  /**
11
12
  * Get the Antigravity database path based on the current platform.
@@ -30,10 +31,8 @@ function getAntigravityDbPath() {
30
31
  * Generate platform-specific User-Agent string.
31
32
  * @returns {string} User-Agent in format "antigravity/version os/arch"
32
33
  */
33
- function getPlatformUserAgent() {
34
- const os = platform();
35
- const architecture = arch();
36
- return `antigravity/1.16.5 ${os}/${architecture}`;
34
+ export function getPlatformUserAgent() {
35
+ return generateSmartUserAgent();
37
36
  }
38
37
 
39
38
  // IDE Type enum (numeric values as expected by Cloud Code API)
@@ -103,7 +102,10 @@ export const ANTIGRAVITY_ENDPOINT_FALLBACKS = [
103
102
  // Strictly matches the generic 'u' method in main.js
104
103
  export const ANTIGRAVITY_HEADERS = {
105
104
  'User-Agent': getPlatformUserAgent(),
106
- 'Content-Type': 'application/json'
105
+ 'Content-Type': 'application/json',
106
+ 'X-Client-Name': 'antigravity',
107
+ 'X-Client-Version': '1.107.0', // Match product.json version
108
+ 'x-goog-api-client': 'gl-node/18.18.2 fire/0.8.6 grpc/1.10.x' // Simulate Google Node.js client environment
107
109
  };
108
110
 
109
111
  // Endpoint order for loadCodeAssist (prod first)
package/src/index.js CHANGED
@@ -12,6 +12,7 @@ import { logger } from './utils/logger.js';
12
12
  import { config } from './config.js';
13
13
  import { getStrategyLabel, STRATEGY_NAMES, DEFAULT_STRATEGY } from './account-manager/strategies/index.js';
14
14
  import { getPackageVersion } from './utils/helpers.js';
15
+ import tlsClient from './utils/tls-client.js';
15
16
  import path from 'path';
16
17
  import os from 'os';
17
18
 
@@ -159,8 +160,15 @@ ${environmentSection}
159
160
  });
160
161
 
161
162
  // Graceful shutdown
162
- const shutdown = () => {
163
+ const shutdown = async () => {
163
164
  logger.info('Shutting down server...');
165
+
166
+ try {
167
+ await tlsClient.exit();
168
+ } catch (err) {
169
+ logger.error('Error shutting down TLS client:', err);
170
+ }
171
+
164
172
  server.close(() => {
165
173
  logger.success('Server stopped');
166
174
  process.exit(0);
@@ -2,6 +2,7 @@ import { readFileSync } from 'fs';
2
2
  import { fileURLToPath } from 'url';
3
3
  import path from 'path';
4
4
  import { config } from '../config.js';
5
+ import tlsClient from './tls-client.js';
5
6
 
6
7
  /**
7
8
  * Shared Utility Functions
@@ -86,7 +87,7 @@ export async function throttledFetch(url, options) {
86
87
  await sleep(delayMs);
87
88
  }
88
89
  }
89
- return fetch(url, options);
90
+ return tlsClient.fetch(url, options);
90
91
  }
91
92
 
92
93
  /**
@@ -0,0 +1,102 @@
1
+ import { createRequire } from 'module';
2
+ import { Readable } from 'stream';
3
+ import { getPlatformUserAgent } from '../constants.js';
4
+
5
+ const require = createRequire(import.meta.url);
6
+ const { createSession } = require('wreq-js');
7
+
8
+ class TlsClient {
9
+ constructor() {
10
+ this.userAgent = getPlatformUserAgent();
11
+ this.session = null;
12
+ }
13
+
14
+ async getSession() {
15
+ if (this.session) return this.session;
16
+ this.session = await createSession({
17
+ browser: 'chrome_124',
18
+ os: 'macos',
19
+ userAgent: this.userAgent
20
+ });
21
+ return this.session;
22
+ }
23
+
24
+ async fetch(url, options = {}) {
25
+ const session = await this.getSession();
26
+ const method = (options.method || 'GET').toUpperCase();
27
+
28
+ const wreqOptions = {
29
+ method,
30
+ headers: options.headers,
31
+ body: options.body,
32
+ redirect: options.redirect === 'manual' ? 'manual' : 'follow',
33
+ };
34
+
35
+ try {
36
+ const response = await session.fetch(url, wreqOptions);
37
+ return new ResponseWrapper(response);
38
+ } catch (error) {
39
+ console.error('wreq-js fetch failed:', error);
40
+ throw error;
41
+ }
42
+ }
43
+
44
+ async exit() {
45
+ if (this.session) {
46
+ await this.session.close();
47
+ this.session = null;
48
+ }
49
+ }
50
+ }
51
+
52
+ class ResponseWrapper {
53
+ constructor(wreqResponse) {
54
+ this.status = wreqResponse.status;
55
+ this.statusText = wreqResponse.statusText || (this.status === 200 ? 'OK' : `Status ${this.status}`);
56
+ this.headers = new Headers(wreqResponse.headers);
57
+ this.url = wreqResponse.url;
58
+ this.ok = this.status >= 200 && this.status < 300;
59
+
60
+ if (wreqResponse.body) {
61
+ if (typeof wreqResponse.body.getReader === 'function') {
62
+ this.body = wreqResponse.body;
63
+ } else {
64
+ this.body = Readable.toWeb(wreqResponse.body);
65
+ }
66
+ } else {
67
+ this.body = null;
68
+ }
69
+ }
70
+
71
+ async text() {
72
+ if (!this.body) return '';
73
+ const reader = this.body.getReader();
74
+ const chunks = [];
75
+ while (true) {
76
+ const { done, value } = await reader.read();
77
+ if (done) break;
78
+ chunks.push(typeof value === 'string' ? Buffer.from(value) : value);
79
+ }
80
+ return Buffer.concat(chunks).toString('utf8');
81
+ }
82
+
83
+ async json() {
84
+ const text = await this.text();
85
+ return JSON.parse(text);
86
+ }
87
+ }
88
+
89
+ class Headers {
90
+ constructor(headersObj = {}) {
91
+ this.map = new Map();
92
+ for (const [key, value] of Object.entries(headersObj)) {
93
+ this.map.set(key.toLowerCase(), Array.isArray(value) ? value.join(', ') : value);
94
+ }
95
+ }
96
+
97
+ get(name) { return this.map.get(name.toLowerCase()) || null; }
98
+ has(name) { return this.map.has(name.toLowerCase()); }
99
+ forEach(callback) { this.map.forEach(callback); }
100
+ }
101
+
102
+ export default new TlsClient();
@@ -0,0 +1,134 @@
1
+ import { execSync } from 'child_process';
2
+ import { platform, homedir } from 'os';
3
+ import { join } from 'path';
4
+ import { existsSync } from 'fs';
5
+
6
+ /**
7
+ * Intelligent Version Detection for Antigravity
8
+ * Attempts to find the local installation and extract its version.
9
+ * Falls back to hard-coded stable versions if detection fails.
10
+ */
11
+
12
+ // Fallback constant
13
+ const FALLBACK_ANTIGRAVITY_VERSION = '1.16.5';
14
+
15
+ // Cache for the generated User-Agent string
16
+ let cachedUserAgent = null;
17
+
18
+ /**
19
+ * Compares two semver-ish version strings (X.Y.Z).
20
+ * @param {string} v1 - Version string 1
21
+ * @param {string} v2 - Version string 2
22
+ * @returns {boolean} True if v1 > v2
23
+ */
24
+ function isVersionHigher(v1, v2) {
25
+ const parts1 = v1.split('.').map(Number);
26
+ const parts2 = v2.split('.').map(Number);
27
+
28
+ for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
29
+ const p1 = parts1[i] || 0;
30
+ const p2 = parts2[i] || 0;
31
+ if (p1 > p2) return true;
32
+ if (p1 < p2) return false;
33
+ }
34
+ return false;
35
+ }
36
+
37
+ /**
38
+ * Gets the version config (version, source)
39
+ * @returns {{ version: string, source: string }}
40
+ */
41
+ function getVersionConfig() {
42
+ const os = platform();
43
+ let detectedVersion = null;
44
+ let finalVersion = FALLBACK_ANTIGRAVITY_VERSION;
45
+ let source = 'fallback';
46
+
47
+ try {
48
+ if (os === 'darwin') {
49
+ detectedVersion = getVersionMacos();
50
+ } else if (os === 'win32') {
51
+ detectedVersion = getVersionWindows();
52
+ }
53
+ } catch (error) {
54
+ // Silently fail and use fallback
55
+ }
56
+
57
+ // Only use detected version if it's higher than the fallback version
58
+ if (detectedVersion && isVersionHigher(detectedVersion, FALLBACK_ANTIGRAVITY_VERSION)) {
59
+ finalVersion = detectedVersion;
60
+ source = 'local';
61
+ }
62
+
63
+ return {
64
+ version: finalVersion,
65
+ source
66
+ };
67
+ }
68
+
69
+ /**
70
+ * Generate a simplified User-Agent string used by the Antigravity binary.
71
+ * Format: "antigravity/version os/arch"
72
+ * @returns {string} The User-Agent string
73
+ */
74
+ export function generateSmartUserAgent() {
75
+ if (cachedUserAgent) return cachedUserAgent;
76
+
77
+ const { version } = getVersionConfig();
78
+ const os = platform();
79
+ const architecture = process.arch;
80
+
81
+ // Map Node.js platform names to binary-friendly names
82
+ const osName = os === 'darwin' ? 'darwin' : (os === 'win32' ? 'win32' : 'linux');
83
+
84
+ cachedUserAgent = `antigravity/${version} ${osName}/${architecture}`;
85
+ return cachedUserAgent;
86
+ }
87
+
88
+ /**
89
+ * MacOS-specific version detection using plutil
90
+ */
91
+ function getVersionMacos() {
92
+ const appPath = '/Applications/Antigravity.app';
93
+ const plistPath = join(appPath, 'Contents/Info.plist');
94
+
95
+ if (!existsSync(plistPath)) return null;
96
+
97
+ try {
98
+ const version = execSync(`plutil -extract CFBundleShortVersionString raw "${plistPath}"`, { encoding: 'utf8' }).trim();
99
+ if (/^\d+\.\d+\.\d+/.test(version)) {
100
+ return version;
101
+ }
102
+ } catch (e) {
103
+ // plutil failed or file not found
104
+ }
105
+ return null;
106
+ }
107
+
108
+ /**
109
+ * Windows-specific version detection using PowerShell
110
+ */
111
+ function getVersionWindows() {
112
+ try {
113
+ const localAppData = process.env.LOCALAPPDATA;
114
+ const programFiles = process.env.ProgramFiles || 'C:\\Program Files';
115
+
116
+ const possiblePaths = [
117
+ join(localAppData, 'Programs', 'Antigravity', 'Antigravity.exe'),
118
+ join(programFiles, 'Antigravity', 'Antigravity.exe')
119
+ ];
120
+
121
+ for (const exePath of possiblePaths) {
122
+ if (existsSync(exePath)) {
123
+ const cmd = `powershell -Command "(Get-Item '${exePath}').VersionInfo.FileVersion"`;
124
+ const version = execSync(cmd, { encoding: 'utf8' }).trim();
125
+ const match = version.match(/^(\d+\.\d+\.\d+)/);
126
+ if (match) return match[1];
127
+ }
128
+ }
129
+ } catch (e) {
130
+ // PowerShell or path issues
131
+ }
132
+ return null;
133
+ }
134
+