// Package lspool provides LS-mode integration for the antigravity gateway. // // When LS mode is enabled (via ANTIGRAVITY_LS_MODE=true), requests to // streamGenerateContent are routed through a real Language Server instance // instead of directly to cloudcode-pa. This provides: // // - Authentic TLS fingerprint (Google's own Go binary) // - Real session management and Heartbeat // - Indistinguishable from a real IDE instance // // To enable: set environment variable ANTIGRAVITY_LS_MODE=true // To configure: set ANTIGRAVITY_APP_ROOT to the AntiGravity.app path package lspool import ( "log/slog" "os" "strings" "sync" "github.com/Wei-Shaw/sub2api/internal/config" ) var ( globalBackend Backend globalPoolOnce sync.Once lsModeEnabled bool ) func init() { lsModeEnabled = os.Getenv("ANTIGRAVITY_LS_MODE") == "true" } // IsLSModeEnabled returns whether LS mode is active func IsLSModeEnabled() bool { return lsModeEnabled } const ( LSStrategyDirect = "direct" LSStrategyJSParity = "js-parity" ) // CurrentLSStrategy returns the active LS routing strategy. // Unknown values are treated as "direct" for safety. func CurrentLSStrategy() string { switch strings.ToLower(strings.TrimSpace(os.Getenv("ANTIGRAVITY_LS_STRATEGY"))) { case "", LSStrategyDirect: return LSStrategyDirect case LSStrategyJSParity: return LSStrategyJSParity default: return LSStrategyDirect } } // GlobalPool returns the singleton LS pool instance // Creates it on first call if LS mode is enabled func GlobalPool(cfg *config.Config) Backend { if !lsModeEnabled { return nil } globalPoolOnce.Do(func() { manager, err := NewWorkerManagerFromConfig(cfg) if err != nil { slog.Default().Error("failed to initialize LS worker manager", "err", err) return } globalBackend = manager }) return globalBackend } // Shutdown closes the global pool func Shutdown() { if globalBackend != nil { globalBackend.Close() } } // StatusInfo returns the current LS pool status for diagnostics func StatusInfo() map[string]any { info := map[string]any{ "ls_mode_enabled": lsModeEnabled, "build": "enhanced", "user_agent": "antigravity/1.107.0", } if lsModeEnabled && globalBackend != nil { stats := globalBackend.Stats() info["pool_total"] = stats["total"] info["pool_active"] = stats["active"] } return info }