mirror of
https://gh-proxy.org/https://github.com/klinsmaya/CLIProxyAPI.git
synced 2026-11-08 09:26:52 +08:00
375 lines
10 KiB
Go
375 lines
10 KiB
Go
// Package misc provides miscellaneous utility functions for the CLI Proxy API server.
|
|
package misc
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"encoding/xml"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const (
|
|
antigravityFallbackVersion = "2.1.0"
|
|
antigravityVersionCacheTTL = 6 * time.Hour
|
|
antigravityFetchTimeout = 10 * time.Second
|
|
AntigravityNodeAPIClientUA = "google-api-nodejs-client/10.3.0"
|
|
AntigravityGoogAPIClientUA = "gl-node/22.21.1"
|
|
)
|
|
|
|
var (
|
|
antigravityHubGCSListURL = "https://storage.googleapis.com/antigravity-public/?prefix=antigravity-hub/&delimiter=/"
|
|
antigravityReleasesURL = "https://antigravity-auto-updater-974169037036.us-central1.run.app/releases"
|
|
)
|
|
|
|
type antigravityRelease struct {
|
|
Version string `json:"version"`
|
|
ExecutionID string `json:"execution_id"`
|
|
}
|
|
|
|
type antigravityHubGCSList struct {
|
|
CommonPrefixes []antigravityHubGCSPrefix `xml:"CommonPrefixes"`
|
|
}
|
|
|
|
type antigravityHubGCSPrefix struct {
|
|
Prefix string `xml:"Prefix"`
|
|
}
|
|
|
|
type antigravitySemVersion struct {
|
|
raw string
|
|
parts [3]int
|
|
}
|
|
|
|
var (
|
|
cachedAntigravityVersion = antigravityFallbackVersion
|
|
antigravityVersionMu sync.RWMutex
|
|
antigravityVersionExpiry time.Time
|
|
antigravityUpdaterOnce sync.Once
|
|
)
|
|
|
|
// StartAntigravityVersionUpdater starts a background goroutine that periodically refreshes the cached antigravity version.
|
|
// This is intentionally decoupled from request execution to avoid blocking executors on version lookups.
|
|
func StartAntigravityVersionUpdater(ctx context.Context) {
|
|
antigravityUpdaterOnce.Do(func() {
|
|
go runAntigravityVersionUpdater(ctx)
|
|
})
|
|
}
|
|
|
|
func runAntigravityVersionUpdater(ctx context.Context) {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
|
|
ticker := time.NewTicker(antigravityVersionCacheTTL / 2)
|
|
defer ticker.Stop()
|
|
|
|
log.Infof("periodic antigravity version refresh started (interval=%s)", antigravityVersionCacheTTL/2)
|
|
|
|
refreshAntigravityVersion(ctx)
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
refreshAntigravityVersion(ctx)
|
|
}
|
|
}
|
|
}
|
|
|
|
func refreshAntigravityVersion(ctx context.Context) {
|
|
version, errFetch := fetchAntigravityLatestVersion(ctx)
|
|
|
|
antigravityVersionMu.Lock()
|
|
defer antigravityVersionMu.Unlock()
|
|
|
|
now := time.Now()
|
|
|
|
if errFetch == nil {
|
|
cachedAntigravityVersion = version
|
|
antigravityVersionExpiry = now.Add(antigravityVersionCacheTTL)
|
|
log.WithField("version", version).Info("fetched latest antigravity version")
|
|
return
|
|
}
|
|
|
|
if cachedAntigravityVersion == "" || now.After(antigravityVersionExpiry) {
|
|
cachedAntigravityVersion = antigravityFallbackVersion
|
|
antigravityVersionExpiry = now.Add(antigravityVersionCacheTTL)
|
|
log.WithError(errFetch).Warn("failed to refresh antigravity version, using fallback version")
|
|
return
|
|
}
|
|
|
|
log.WithError(errFetch).Debug("failed to refresh antigravity version, keeping cached value")
|
|
}
|
|
|
|
// AntigravityLatestVersion returns the cached antigravity version refreshed by StartAntigravityVersionUpdater.
|
|
// It falls back to antigravityFallbackVersion if the cache is empty or stale.
|
|
func AntigravityLatestVersion() string {
|
|
antigravityVersionMu.RLock()
|
|
if cachedAntigravityVersion != "" && time.Now().Before(antigravityVersionExpiry) {
|
|
v := cachedAntigravityVersion
|
|
antigravityVersionMu.RUnlock()
|
|
return v
|
|
}
|
|
antigravityVersionMu.RUnlock()
|
|
|
|
return antigravityFallbackVersion
|
|
}
|
|
|
|
// AntigravityUserAgent returns the User-Agent string for antigravity requests
|
|
// using the latest version fetched from the releases API.
|
|
func AntigravityUserAgent() string {
|
|
return fmt.Sprintf("antigravity/%s darwin/arm64", AntigravityLatestVersion())
|
|
}
|
|
|
|
func antigravityBaseUserAgent(userAgent string) string {
|
|
userAgent = strings.TrimSpace(userAgent)
|
|
if userAgent == "" {
|
|
return AntigravityUserAgent()
|
|
}
|
|
lower := strings.ToLower(userAgent)
|
|
if strings.HasPrefix(lower, "antigravity/") {
|
|
if idx := strings.Index(lower, " google-api-nodejs-client/"); idx >= 0 {
|
|
trimmed := strings.TrimSpace(userAgent[:idx])
|
|
if trimmed != "" {
|
|
return trimmed
|
|
}
|
|
}
|
|
}
|
|
return userAgent
|
|
}
|
|
|
|
// AntigravityRequestUserAgent returns the short Antigravity runtime UA used by
|
|
// generate/stream/model-list requests.
|
|
func AntigravityRequestUserAgent(userAgent string) string {
|
|
return antigravityBaseUserAgent(userAgent)
|
|
}
|
|
|
|
// AntigravityLoadCodeAssistUserAgent returns the long Antigravity control-plane
|
|
// UA used by loadCodeAssist requests.
|
|
func AntigravityLoadCodeAssistUserAgent(userAgent string) string {
|
|
userAgent = strings.TrimSpace(userAgent)
|
|
if userAgent == "" {
|
|
return AntigravityUserAgent() + " " + AntigravityNodeAPIClientUA
|
|
}
|
|
lower := strings.ToLower(userAgent)
|
|
if !strings.HasPrefix(lower, "antigravity/") {
|
|
return userAgent
|
|
}
|
|
if strings.Contains(lower, "google-api-nodejs-client/") {
|
|
return userAgent
|
|
}
|
|
return antigravityBaseUserAgent(userAgent) + " " + AntigravityNodeAPIClientUA
|
|
}
|
|
|
|
// AntigravityVersionFromUserAgent extracts the Antigravity version prefix from
|
|
// either the short or long Antigravity UA forms.
|
|
func AntigravityVersionFromUserAgent(userAgent string) string {
|
|
base := antigravityBaseUserAgent(userAgent)
|
|
lower := strings.ToLower(base)
|
|
if !strings.HasPrefix(lower, "antigravity/") {
|
|
return AntigravityLatestVersion()
|
|
}
|
|
rest := base[len("antigravity/"):]
|
|
if idx := strings.IndexAny(rest, " \t"); idx >= 0 {
|
|
rest = rest[:idx]
|
|
}
|
|
rest = strings.TrimSpace(rest)
|
|
if rest == "" {
|
|
return AntigravityLatestVersion()
|
|
}
|
|
return rest
|
|
}
|
|
|
|
func fetchAntigravityLatestVersion(ctx context.Context) (string, error) {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
|
|
client := &http.Client{Timeout: antigravityFetchTimeout}
|
|
|
|
version, errHub := fetchAntigravityHubGCSLatestVersion(ctx, client)
|
|
if errHub == nil {
|
|
return version, nil
|
|
}
|
|
|
|
log.WithError(errHub).Debug("failed to fetch antigravity hub GCS version, trying legacy releases API")
|
|
|
|
version, errLegacy := fetchAntigravityLegacyLatestVersion(ctx, client)
|
|
if errLegacy == nil {
|
|
return version, nil
|
|
}
|
|
|
|
return "", fmt.Errorf("fetch antigravity hub GCS version: %v; fetch legacy releases: %w", errHub, errLegacy)
|
|
}
|
|
|
|
func fetchAntigravityHubGCSLatestVersion(ctx context.Context, client *http.Client) (string, error) {
|
|
httpReq, errReq := http.NewRequestWithContext(ctx, http.MethodGet, antigravityHubGCSListURL, nil)
|
|
if errReq != nil {
|
|
return "", fmt.Errorf("build antigravity hub GCS request: %w", errReq)
|
|
}
|
|
|
|
resp, errDo := client.Do(httpReq)
|
|
if errDo != nil {
|
|
return "", fmt.Errorf("fetch antigravity hub GCS list: %w", errDo)
|
|
}
|
|
defer func() {
|
|
if errClose := resp.Body.Close(); errClose != nil {
|
|
log.WithError(errClose).Warn("antigravity hub GCS response body close error")
|
|
}
|
|
}()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return "", fmt.Errorf("antigravity hub GCS list returned status %d", resp.StatusCode)
|
|
}
|
|
|
|
var list antigravityHubGCSList
|
|
if errDecode := xml.NewDecoder(resp.Body).Decode(&list); errDecode != nil {
|
|
return "", fmt.Errorf("decode antigravity hub GCS list: %w", errDecode)
|
|
}
|
|
|
|
prefixes := make([]string, 0, len(list.CommonPrefixes))
|
|
for _, commonPrefix := range list.CommonPrefixes {
|
|
prefixes = append(prefixes, commonPrefix.Prefix)
|
|
}
|
|
|
|
return latestAntigravityHubVersionFromPrefixes(prefixes)
|
|
}
|
|
|
|
func fetchAntigravityLegacyLatestVersion(ctx context.Context, client *http.Client) (string, error) {
|
|
httpReq, errReq := http.NewRequestWithContext(ctx, http.MethodGet, antigravityReleasesURL, nil)
|
|
if errReq != nil {
|
|
return "", fmt.Errorf("build antigravity releases request: %w", errReq)
|
|
}
|
|
|
|
resp, errDo := client.Do(httpReq)
|
|
if errDo != nil {
|
|
return "", fmt.Errorf("fetch antigravity releases: %w", errDo)
|
|
}
|
|
defer func() {
|
|
if errClose := resp.Body.Close(); errClose != nil {
|
|
log.WithError(errClose).Warn("antigravity releases response body close error")
|
|
}
|
|
}()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return "", fmt.Errorf("antigravity releases API returned status %d", resp.StatusCode)
|
|
}
|
|
|
|
var releases []antigravityRelease
|
|
if errDecode := json.NewDecoder(resp.Body).Decode(&releases); errDecode != nil {
|
|
return "", fmt.Errorf("decode antigravity releases response: %w", errDecode)
|
|
}
|
|
|
|
if len(releases) == 0 {
|
|
return "", errors.New("antigravity releases API returned empty list")
|
|
}
|
|
|
|
version := releases[0].Version
|
|
if version == "" {
|
|
return "", errors.New("antigravity releases API returned empty version")
|
|
}
|
|
|
|
return version, nil
|
|
}
|
|
|
|
func latestAntigravityHubVersionFromPrefixes(prefixes []string) (string, error) {
|
|
var best antigravitySemVersion
|
|
found := false
|
|
|
|
for _, prefix := range prefixes {
|
|
version, ok := antigravityHubVersionFromPrefix(prefix)
|
|
if !ok {
|
|
continue
|
|
}
|
|
semVersion, ok := parseAntigravitySemVersion(version)
|
|
if !ok {
|
|
continue
|
|
}
|
|
if !found || compareAntigravitySemVersion(semVersion, best) > 0 {
|
|
best = semVersion
|
|
found = true
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
return "", errors.New("antigravity hub GCS list contained no version prefixes")
|
|
}
|
|
|
|
return best.raw, nil
|
|
}
|
|
|
|
func antigravityHubVersionFromPrefix(prefix string) (string, bool) {
|
|
const hubPrefix = "antigravity-hub/"
|
|
|
|
prefix = strings.TrimSpace(prefix)
|
|
prefix = strings.TrimSuffix(prefix, "/")
|
|
if !strings.HasPrefix(prefix, hubPrefix) {
|
|
return "", false
|
|
}
|
|
|
|
name := strings.TrimPrefix(prefix, hubPrefix)
|
|
separator := strings.LastIndex(name, "-")
|
|
if separator <= 0 || separator == len(name)-1 {
|
|
return "", false
|
|
}
|
|
|
|
version := strings.TrimSpace(name[:separator])
|
|
executionID := name[separator+1:]
|
|
if version == "" || executionID == "" {
|
|
return "", false
|
|
}
|
|
for _, ch := range executionID {
|
|
if ch < '0' || ch > '9' {
|
|
return "", false
|
|
}
|
|
}
|
|
|
|
return version, true
|
|
}
|
|
|
|
func parseAntigravitySemVersion(version string) (antigravitySemVersion, bool) {
|
|
parts := strings.Split(version, ".")
|
|
if len(parts) != 3 {
|
|
return antigravitySemVersion{}, false
|
|
}
|
|
|
|
semVersion := antigravitySemVersion{raw: version}
|
|
for i, part := range parts {
|
|
if part == "" {
|
|
return antigravitySemVersion{}, false
|
|
}
|
|
for _, ch := range part {
|
|
if ch < '0' || ch > '9' {
|
|
return antigravitySemVersion{}, false
|
|
}
|
|
}
|
|
value, errParse := strconv.Atoi(part)
|
|
if errParse != nil {
|
|
return antigravitySemVersion{}, false
|
|
}
|
|
semVersion.parts[i] = value
|
|
}
|
|
|
|
return semVersion, true
|
|
}
|
|
|
|
func compareAntigravitySemVersion(left antigravitySemVersion, right antigravitySemVersion) int {
|
|
for i := range left.parts {
|
|
if left.parts[i] > right.parts[i] {
|
|
return 1
|
|
}
|
|
if left.parts[i] < right.parts[i] {
|
|
return -1
|
|
}
|
|
}
|
|
return 0
|
|
}
|