9router/open-sse/utils/sessionManager.js
zx a229d79158 feat(antigravity): initial steps for Antigravity anti-ban alignment
Cherry-picked from decolua/9router#141 (author: LinearSakana / zx <me@char.moe>)

- Implement client identity spoofing with numeric enums (ideType: 9, pluginType: 2)
- Add runtime platform detection for User-Agent and metadata
- Implement per-connection session ID caching (binary-compatible format)
- Add ANTIGRAVITY_HEADERS (X-Client-Name, X-Client-Version, x-goog-api-client)
- Add X-Machine-Session-Id header injection
- Align metadata/mode parameters across all Antigravity API calls
- Implement double injection for system prompt (raw + [ignore] wrapped)
- Rename internal anti-loop header to x-request-source for anonymity

Skipped: commit 6 (signature side-channel caching) — kept DEFAULT_THINKING_GEMINI_SIGNATURE
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-20 14:44:29 +07:00

66 lines
2.2 KiB
JavaScript

/**
* Session Manager for Antigravity Cloud Code
*
* Handles session ID generation and caching for prompt caching continuity.
* Mimics the Antigravity binary behavior: generates a session ID at startup
* and keeps it for the process lifetime, scoped per account/connection.
*
* Reference: antigravity-claude-proxy/src/cloudcode/session-manager.js
*/
import crypto from "crypto";
// Runtime storage for session IDs (per connection/account)
// Key: connectionId (email or identifier), Value: sessionId
const runtimeSessionStore = new Map();
/**
* Get or create a session ID for the given connection.
*
* The binary generates a session ID once at startup: `rs() + Date.now()`.
* Since 9router is long-running, we simulate this "per-launch" behavior by
* storing a generated ID in memory for each connection.
*
* - If 9router restarts, the ID changes (matching binary restart behavior).
* - Within a running instance, the ID is stable for that connection.
* - This enables prompt caching while using the EXACT random logic of the binary.
*
* @param {string} connectionId - The connection identifier (email or unique ID)
* @returns {string} A stable session ID string matching binary format
*/
export function deriveSessionId(connectionId) {
if (!connectionId) {
// Fallback for requests without a connection identifier
return generateBinaryStyleId();
}
// Check if we already have a session ID for this connection in this process run
if (runtimeSessionStore.has(connectionId)) {
return runtimeSessionStore.get(connectionId);
}
// Generate a new ID using the binary's exact logic
const newSessionId = generateBinaryStyleId();
// Store it for future requests from this connection
runtimeSessionStore.set(connectionId, newSessionId);
return newSessionId;
}
/**
* Generate a Session ID using the binary's exact logic.
* Format: `rs() + Date.now()` where `rs()` is randomUUID
*
* @returns {string} A session ID in binary format
*/
export function generateBinaryStyleId() {
return crypto.randomUUID() + Date.now().toString();
}
/**
* Clears all session IDs (e.g. useful for testing or explicit reset)
*/
export function clearSessionStore() {
runtimeSessionStore.clear();
}