mirror of
https://github.com/Finesssee/ProxyPilot.git
synced 2026-11-18 18:04:20 +00:00
chore: merge upstream CLIProxyAPI v6.9.36
This commit is contained in:
+1
-1
@@ -1 +1 @@
|
||||
c6baa64b4e3cf85f8400e2cc9d9bd3d7040db187
|
||||
f1ba6151a99240902bcda12102c921b0ead01d2d
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
v6.9.29
|
||||
v6.9.36
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
v6.9.30
|
||||
v6.9.36
|
||||
|
||||
+1
-1
@@ -98,7 +98,7 @@ disable-cooling: false
|
||||
quota-exceeded:
|
||||
switch-project: true # Whether to automatically switch to another project when a quota is exceeded
|
||||
switch-preview-model: true # Whether to automatically switch to a preview model when a quota is exceeded
|
||||
antigravity-credits: true # Whether to retry Antigravity quota_exhausted 429s once with enabledCreditTypes=["GOOGLE_ONE_AI"]
|
||||
antigravity-credits: true # Whether to use credits as last-resort fallback when all free-tier auths are exhausted for Claude models
|
||||
|
||||
# Routing strategy for selecting credentials when multiple match.
|
||||
routing:
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
# Session 2026-04-24
|
||||
|
||||
- Target upstream release: `CLIProxyAPI v6.9.36` (`f1ba6151a99240902bcda12102c921b0ead01d2d`)
|
||||
- Base local release line before merge: `ProxyPilot main` at upstream `v6.9.30` (`2ebbb596`)
|
||||
- Merge branch: `sync-upstream-v6.9.30-to-v6.9.36`
|
||||
- Conflict resolution:
|
||||
- no textual conflicts in the upstream merge; existing ProxyPilot fork surfaces carried forward cleanly
|
||||
- corrected stale upstream marker files from the prior `v6.9.30` sync so `.upstream-sync-tag` and `.upstream-sync-sha` now match the merged upstream release
|
||||
- kept the existing fork behavior around local provider support while taking upstream Codex image-generation, Antigravity credits fallback, logging, and `/healthz` improvements
|
||||
- Validation:
|
||||
- `go test ./...`
|
||||
- `go build ./cmd/server ./cmd/proxypilot-tray ./cmd/proxypilotpack`
|
||||
- `go vet ./...`
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"project": "ProxyPilot",
|
||||
"version": "0.3.0",
|
||||
"upstream": "CLIProxyAPI v6.9.30",
|
||||
"upstream": "CLIProxyAPI v6.9.36",
|
||||
"features": [
|
||||
{
|
||||
"id": 1,
|
||||
|
||||
+20
-2
@@ -330,9 +330,16 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk
|
||||
// setupRoutes configures the API routes for the server.
|
||||
// It defines the endpoints and associates them with their respective handlers.
|
||||
func (s *Server) setupRoutes() {
|
||||
s.engine.GET("/healthz", func(c *gin.Context) {
|
||||
healthzHandler := func(c *gin.Context) {
|
||||
if c.Request.Method == http.MethodHead {
|
||||
c.Status(http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
})
|
||||
}
|
||||
s.engine.GET("/healthz", healthzHandler)
|
||||
s.engine.HEAD("/healthz", healthzHandler)
|
||||
|
||||
s.engine.GET("/management.html", s.serveManagementControlPanel)
|
||||
openaiHandlers := openai.NewOpenAIAPIHandler(s.handlers)
|
||||
@@ -348,6 +355,8 @@ func (s *Server) setupRoutes() {
|
||||
v1.GET("/models", s.unifiedModelsHandler(openaiHandlers, claudeCodeHandlers))
|
||||
v1.POST("/chat/completions", openaiHandlers.ChatCompletions)
|
||||
v1.POST("/completions", openaiHandlers.Completions)
|
||||
v1.POST("/images/generations", openaiHandlers.ImagesGenerations)
|
||||
v1.POST("/images/edits", openaiHandlers.ImagesEdits)
|
||||
v1.POST("/messages", claudeCodeHandlers.ClaudeMessages)
|
||||
v1.POST("/messages/count_tokens", claudeCodeHandlers.ClaudeCountTokens)
|
||||
v1.GET("/responses", openaiResponsesHandlers.ResponsesWebsocket)
|
||||
@@ -355,6 +364,15 @@ func (s *Server) setupRoutes() {
|
||||
v1.POST("/responses/compact", openaiResponsesHandlers.Compact)
|
||||
}
|
||||
|
||||
// Codex CLI direct route aliases (chatgpt_base_url compatible)
|
||||
codexDirect := s.engine.Group("/backend-api/codex")
|
||||
codexDirect.Use(AuthMiddleware(s.accessManager))
|
||||
{
|
||||
codexDirect.GET("/responses", openaiResponsesHandlers.ResponsesWebsocket)
|
||||
codexDirect.POST("/responses", openaiResponsesHandlers.Responses)
|
||||
codexDirect.POST("/responses/compact", openaiResponsesHandlers.Compact)
|
||||
}
|
||||
|
||||
// Gemini compatible API routes
|
||||
v1beta := s.engine.Group("/v1beta")
|
||||
v1beta.Use(AuthMiddleware(s.accessManager))
|
||||
|
||||
+30
-15
@@ -50,23 +50,38 @@ func newTestServer(t *testing.T) *Server {
|
||||
func TestHealthz(t *testing.T) {
|
||||
server := newTestServer(t)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
server.engine.ServeHTTP(rr, req)
|
||||
t.Run("GET", func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
server.engine.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("unexpected status code: got %d want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String())
|
||||
}
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("unexpected status code: got %d want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String())
|
||||
}
|
||||
|
||||
var resp struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("failed to parse response JSON: %v; body=%s", err, rr.Body.String())
|
||||
}
|
||||
if resp.Status != "ok" {
|
||||
t.Fatalf("unexpected response status: got %q want %q", resp.Status, "ok")
|
||||
}
|
||||
var resp struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("failed to parse response JSON: %v; body=%s", err, rr.Body.String())
|
||||
}
|
||||
if resp.Status != "ok" {
|
||||
t.Fatalf("unexpected response status: got %q want %q", resp.Status, "ok")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("HEAD", func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodHead, "/healthz", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
server.engine.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("unexpected status code: got %d want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String())
|
||||
}
|
||||
if rr.Body.Len() != 0 {
|
||||
t.Fatalf("expected empty body for HEAD request, got %q", rr.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestAmpProviderModelRoutes(t *testing.T) {
|
||||
|
||||
@@ -258,8 +258,9 @@ type QuotaExceeded struct {
|
||||
// SwitchPreviewModel indicates whether to automatically switch to a preview model when a quota is exceeded.
|
||||
SwitchPreviewModel bool `yaml:"switch-preview-model" json:"switch-preview-model"`
|
||||
|
||||
// AntigravityCredits indicates whether to retry Antigravity quota_exhausted 429s once
|
||||
// on the same credential with enabledCreditTypes=["GOOGLE_ONE_AI"].
|
||||
// AntigravityCredits enables credits-based last-resort fallback for Claude models.
|
||||
// When all free-tier auths are exhausted (429/503), the conductor retries with
|
||||
// an auth that has available Google One AI credits.
|
||||
AntigravityCredits bool `yaml:"antigravity-credits" json:"antigravity-credits"`
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
var aiAPIPrefixes = []string{
|
||||
"/v1/chat/completions",
|
||||
"/v1/completions",
|
||||
"/v1/images",
|
||||
"/v1/messages",
|
||||
"/v1/responses",
|
||||
"/v1beta/models/",
|
||||
|
||||
@@ -58,3 +58,12 @@ func TestGinLogrusRecoveryHandlesRegularPanic(t *testing.T) {
|
||||
t.Fatalf("expected 500, got %d", recorder.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAIAPIPathIncludesImages(t *testing.T) {
|
||||
if !isAIAPIPath("/v1/images/generations") {
|
||||
t.Fatalf("expected /v1/images/generations to be treated as AI API path")
|
||||
}
|
||||
if !isAIAPIPath("/v1/images/edits") {
|
||||
t.Fatalf("expected /v1/images/edits to be treated as AI API path")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const codexBuiltinImageModelID = "gpt-image-2"
|
||||
|
||||
// staticModelsJSON mirrors the top-level structure of models.json.
|
||||
type staticModelsJSON struct {
|
||||
Claude []*ModelInfo `json:"claude"`
|
||||
@@ -50,22 +52,22 @@ func GetAIStudioModels() []*ModelInfo {
|
||||
|
||||
// GetCodexFreeModels returns model definitions for the Codex free plan tier.
|
||||
func GetCodexFreeModels() []*ModelInfo {
|
||||
return cloneModelInfos(getModels().CodexFree)
|
||||
return WithCodexBuiltins(cloneModelInfos(getModels().CodexFree))
|
||||
}
|
||||
|
||||
// GetCodexTeamModels returns model definitions for the Codex team plan tier.
|
||||
func GetCodexTeamModels() []*ModelInfo {
|
||||
return cloneModelInfos(getModels().CodexTeam)
|
||||
return WithCodexBuiltins(cloneModelInfos(getModels().CodexTeam))
|
||||
}
|
||||
|
||||
// GetCodexPlusModels returns model definitions for the Codex plus plan tier.
|
||||
func GetCodexPlusModels() []*ModelInfo {
|
||||
return cloneModelInfos(getModels().CodexPlus)
|
||||
return WithCodexBuiltins(cloneModelInfos(getModels().CodexPlus))
|
||||
}
|
||||
|
||||
// GetCodexProModels returns model definitions for the Codex pro plan tier.
|
||||
func GetCodexProModels() []*ModelInfo {
|
||||
return cloneModelInfos(getModels().CodexPro)
|
||||
return WithCodexBuiltins(cloneModelInfos(getModels().CodexPro))
|
||||
}
|
||||
|
||||
// GetQwenModels returns the standard Qwen model definitions.
|
||||
@@ -88,6 +90,71 @@ func GetAntigravityModels() []*ModelInfo {
|
||||
return cloneModelInfos(getModels().Antigravity)
|
||||
}
|
||||
|
||||
// WithCodexBuiltins injects hard-coded Codex-only model definitions that should
|
||||
// not depend on remote models.json updates. Built-ins replace any matching IDs
|
||||
// already present in the provided slice.
|
||||
func WithCodexBuiltins(models []*ModelInfo) []*ModelInfo {
|
||||
return upsertModelInfos(models, codexBuiltinImageModelInfo())
|
||||
}
|
||||
|
||||
func codexBuiltinImageModelInfo() *ModelInfo {
|
||||
return &ModelInfo{
|
||||
ID: codexBuiltinImageModelID,
|
||||
Object: "model",
|
||||
Created: 1704067200, // 2024-01-01
|
||||
OwnedBy: "openai",
|
||||
Type: "openai",
|
||||
DisplayName: "GPT Image 2",
|
||||
Version: codexBuiltinImageModelID,
|
||||
}
|
||||
}
|
||||
|
||||
func upsertModelInfos(models []*ModelInfo, extras ...*ModelInfo) []*ModelInfo {
|
||||
if len(extras) == 0 {
|
||||
return models
|
||||
}
|
||||
|
||||
extraIDs := make(map[string]struct{}, len(extras))
|
||||
extraList := make([]*ModelInfo, 0, len(extras))
|
||||
for _, extra := range extras {
|
||||
if extra == nil {
|
||||
continue
|
||||
}
|
||||
id := strings.TrimSpace(extra.ID)
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
key := strings.ToLower(id)
|
||||
if _, exists := extraIDs[key]; exists {
|
||||
continue
|
||||
}
|
||||
extraIDs[key] = struct{}{}
|
||||
extraList = append(extraList, cloneModelInfo(extra))
|
||||
}
|
||||
|
||||
if len(extraList) == 0 {
|
||||
return models
|
||||
}
|
||||
|
||||
filtered := make([]*ModelInfo, 0, len(models)+len(extraList))
|
||||
for _, model := range models {
|
||||
if model == nil {
|
||||
continue
|
||||
}
|
||||
id := strings.TrimSpace(model.ID)
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := extraIDs[strings.ToLower(id)]; exists {
|
||||
continue
|
||||
}
|
||||
filtered = append(filtered, model)
|
||||
}
|
||||
|
||||
filtered = append(filtered, extraList...)
|
||||
return filtered
|
||||
}
|
||||
|
||||
// cloneModelInfos returns a shallow copy of the slice with each element deep-cloned.
|
||||
func cloneModelInfos(models []*ModelInfo) []*ModelInfo {
|
||||
if len(models) == 0 {
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package registry
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCodexStaticModelsIncludeGPT55(t *testing.T) {
|
||||
if model := findModelInfo(GetCodexFreeModels(), "gpt-5.5"); model != nil {
|
||||
t.Fatalf("expected codex free tier to exclude gpt-5.5, got %+v", model)
|
||||
}
|
||||
|
||||
paidTierModels := map[string][]*ModelInfo{
|
||||
"team": GetCodexTeamModels(),
|
||||
"plus": GetCodexPlusModels(),
|
||||
"pro": GetCodexProModels(),
|
||||
}
|
||||
|
||||
for tier, models := range paidTierModels {
|
||||
t.Run(tier, func(t *testing.T) {
|
||||
model := findModelInfo(models, "gpt-5.5")
|
||||
if model == nil {
|
||||
t.Fatalf("expected codex %s tier to include gpt-5.5", tier)
|
||||
}
|
||||
assertGPT55ModelInfo(t, tier, model)
|
||||
})
|
||||
}
|
||||
|
||||
model := LookupStaticModelInfo("gpt-5.5")
|
||||
if model == nil {
|
||||
t.Fatal("expected LookupStaticModelInfo to find gpt-5.5")
|
||||
}
|
||||
assertGPT55ModelInfo(t, "lookup", model)
|
||||
}
|
||||
|
||||
func findModelInfo(models []*ModelInfo, id string) *ModelInfo {
|
||||
for _, model := range models {
|
||||
if model != nil && model.ID == id {
|
||||
return model
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func assertGPT55ModelInfo(t *testing.T, source string, model *ModelInfo) {
|
||||
t.Helper()
|
||||
|
||||
if model.ID != "gpt-5.5" {
|
||||
t.Fatalf("%s id mismatch: got %q", source, model.ID)
|
||||
}
|
||||
if model.Object != "model" {
|
||||
t.Fatalf("%s object mismatch: got %q", source, model.Object)
|
||||
}
|
||||
if model.Created != 1776902400 {
|
||||
t.Fatalf("%s created timestamp mismatch: got %d", source, model.Created)
|
||||
}
|
||||
if model.OwnedBy != "openai" {
|
||||
t.Fatalf("%s owned_by mismatch: got %q", source, model.OwnedBy)
|
||||
}
|
||||
if model.Type != "openai" {
|
||||
t.Fatalf("%s type mismatch: got %q", source, model.Type)
|
||||
}
|
||||
if model.DisplayName != "GPT 5.5" {
|
||||
t.Fatalf("%s display name mismatch: got %q", source, model.DisplayName)
|
||||
}
|
||||
if model.Version != "gpt-5.5" {
|
||||
t.Fatalf("%s version mismatch: got %q", source, model.Version)
|
||||
}
|
||||
if model.Description != "Frontier model for complex coding, research, and real-world work." {
|
||||
t.Fatalf("%s description mismatch: got %q", source, model.Description)
|
||||
}
|
||||
if model.ContextLength != 272000 {
|
||||
t.Fatalf("%s context length mismatch: got %d", source, model.ContextLength)
|
||||
}
|
||||
if model.MaxCompletionTokens != 128000 {
|
||||
t.Fatalf("%s max completion tokens mismatch: got %d", source, model.MaxCompletionTokens)
|
||||
}
|
||||
if len(model.SupportedParameters) != 1 || model.SupportedParameters[0] != "tools" {
|
||||
t.Fatalf("%s supported parameters mismatch: got %v", source, model.SupportedParameters)
|
||||
}
|
||||
if model.Thinking == nil {
|
||||
t.Fatalf("%s missing thinking support", source)
|
||||
}
|
||||
|
||||
want := []string{"low", "medium", "high", "xhigh"}
|
||||
if len(model.Thinking.Levels) != len(want) {
|
||||
t.Fatalf("%s thinking level count mismatch: got %d, want %d", source, len(model.Thinking.Levels), len(want))
|
||||
}
|
||||
for i, level := range want {
|
||||
if model.Thinking.Levels[i] != level {
|
||||
t.Fatalf("%s thinking level %d mismatch: got %q, want %q", source, i, model.Thinking.Levels[i], level)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1387,6 +1387,29 @@
|
||||
"xhigh"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "gpt-5.5",
|
||||
"object": "model",
|
||||
"created": 1776902400,
|
||||
"owned_by": "openai",
|
||||
"type": "openai",
|
||||
"display_name": "GPT 5.5",
|
||||
"version": "gpt-5.5",
|
||||
"description": "Frontier model for complex coding, research, and real-world work.",
|
||||
"context_length": 272000,
|
||||
"max_completion_tokens": 128000,
|
||||
"supported_parameters": [
|
||||
"tools"
|
||||
],
|
||||
"thinking": {
|
||||
"levels": [
|
||||
"low",
|
||||
"medium",
|
||||
"high",
|
||||
"xhigh"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"codex-plus": [
|
||||
@@ -1505,6 +1528,29 @@
|
||||
"xhigh"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "gpt-5.5",
|
||||
"object": "model",
|
||||
"created": 1776902400,
|
||||
"owned_by": "openai",
|
||||
"type": "openai",
|
||||
"display_name": "GPT 5.5",
|
||||
"version": "gpt-5.5",
|
||||
"description": "Frontier model for complex coding, research, and real-world work.",
|
||||
"context_length": 272000,
|
||||
"max_completion_tokens": 128000,
|
||||
"supported_parameters": [
|
||||
"tools"
|
||||
],
|
||||
"thinking": {
|
||||
"levels": [
|
||||
"low",
|
||||
"medium",
|
||||
"high",
|
||||
"xhigh"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"codex-pro": [
|
||||
@@ -1623,6 +1669,29 @@
|
||||
"xhigh"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "gpt-5.5",
|
||||
"object": "model",
|
||||
"created": 1776902400,
|
||||
"owned_by": "openai",
|
||||
"type": "openai",
|
||||
"display_name": "GPT 5.5",
|
||||
"version": "gpt-5.5",
|
||||
"description": "Frontier model for complex coding, research, and real-world work.",
|
||||
"context_length": 272000,
|
||||
"max_completion_tokens": 128000,
|
||||
"supported_parameters": [
|
||||
"tools"
|
||||
],
|
||||
"thinking": {
|
||||
"levels": [
|
||||
"low",
|
||||
"medium",
|
||||
"high",
|
||||
"xhigh"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"qwen": [
|
||||
@@ -1872,6 +1941,23 @@
|
||||
"zero_allowed": true,
|
||||
"dynamic_allowed": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "kimi-k2.6",
|
||||
"object": "model",
|
||||
"created": 1776729600,
|
||||
"owned_by": "moonshot",
|
||||
"type": "kimi",
|
||||
"display_name": "Kimi K2.6",
|
||||
"description": "Kimi K2.6 - Latest Moonshot AI coding model with improved capabilities",
|
||||
"context_length": 262144,
|
||||
"max_completion_tokens": 65536,
|
||||
"thinking": {
|
||||
"min": 1024,
|
||||
"max": 32000,
|
||||
"zero_allowed": true,
|
||||
"dynamic_allowed": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"antigravity": [
|
||||
|
||||
@@ -52,8 +52,8 @@ const (
|
||||
defaultAntigravityAgent = "antigravity/1.21.9 darwin/arm64" // fallback only; overridden at runtime by misc.AntigravityUserAgent()
|
||||
antigravityAuthType = "antigravity"
|
||||
refreshSkew = 3000 * time.Second
|
||||
antigravityCreditsRetryTTL = 5 * time.Hour
|
||||
antigravityCreditsAutoDisableDuration = 5 * time.Hour
|
||||
antigravityCreditsHintRefreshInterval = 10 * time.Minute
|
||||
antigravityCreditsHintRefreshTimeout = 5 * time.Second
|
||||
antigravityShortQuotaCooldownThreshold = 5 * time.Minute
|
||||
antigravityInstantRetryThreshold = 3 * time.Second
|
||||
// systemInstruction = "You are Antigravity, a powerful agentic AI coding assistant designed by the Google Deepmind team working on Advanced Agentic Coding.You are pair programming with a USER to solve their coding task. The task may require creating a new codebase, modifying or debugging an existing codebase, or simply answering a question.**Absolute paths only****Proactiveness**"
|
||||
@@ -62,8 +62,6 @@ const (
|
||||
type antigravity429Category string
|
||||
|
||||
type antigravityCreditsFailureState struct {
|
||||
Count int
|
||||
DisabledUntil time.Time
|
||||
PermanentlyDisabled bool
|
||||
ExplicitBalanceExhausted bool
|
||||
}
|
||||
@@ -91,28 +89,85 @@ var (
|
||||
randSource = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
randSourceMutex sync.Mutex
|
||||
antigravityCreditsFailureByAuth sync.Map
|
||||
antigravityPreferCreditsByModel sync.Map
|
||||
antigravityShortCooldownByAuth sync.Map
|
||||
antigravityCreditsBalanceByAuth sync.Map // auth.ID → antigravityCreditsBalance
|
||||
antigravityCreditsHintRefreshByID sync.Map // auth.ID → *antigravityCreditsHintRefreshState
|
||||
antigravityQuotaExhaustedKeywords = []string{
|
||||
"quota_exhausted",
|
||||
"quota exhausted",
|
||||
}
|
||||
antigravityCreditsExhaustedKeywords = []string{
|
||||
"google_one_ai",
|
||||
"insufficient credit",
|
||||
"insufficient credits",
|
||||
"not enough credit",
|
||||
"not enough credits",
|
||||
"credit exhausted",
|
||||
"credits exhausted",
|
||||
"credit balance",
|
||||
"minimumcreditamountforusage",
|
||||
"minimum credit amount for usage",
|
||||
"minimum credit",
|
||||
"resource has been exhausted",
|
||||
}
|
||||
)
|
||||
|
||||
type antigravityCreditsBalance struct {
|
||||
CreditAmount float64
|
||||
MinCreditAmount float64
|
||||
PaidTierID string
|
||||
Known bool
|
||||
}
|
||||
|
||||
type antigravityCreditsHintRefreshState struct {
|
||||
mu sync.Mutex
|
||||
lastAttempt time.Time
|
||||
}
|
||||
|
||||
func antigravityAuthHasCredits(auth *cliproxyauth.Auth) bool {
|
||||
if auth == nil || strings.TrimSpace(auth.ID) == "" {
|
||||
return false
|
||||
}
|
||||
if hint, ok := cliproxyauth.GetAntigravityCreditsHint(auth.ID); ok && hint.Known {
|
||||
return hint.Available
|
||||
}
|
||||
val, ok := antigravityCreditsBalanceByAuth.Load(strings.TrimSpace(auth.ID))
|
||||
if !ok {
|
||||
return true // optimistic: assume credits available when balance unknown
|
||||
}
|
||||
bal, valid := val.(antigravityCreditsBalance)
|
||||
if !valid {
|
||||
antigravityCreditsBalanceByAuth.Delete(strings.TrimSpace(auth.ID))
|
||||
return false
|
||||
}
|
||||
if !bal.Known {
|
||||
return false
|
||||
}
|
||||
available := bal.CreditAmount >= bal.MinCreditAmount
|
||||
cliproxyauth.SetAntigravityCreditsHint(strings.TrimSpace(auth.ID), cliproxyauth.AntigravityCreditsHint{
|
||||
Known: true,
|
||||
Available: available,
|
||||
CreditAmount: bal.CreditAmount,
|
||||
MinCreditAmount: bal.MinCreditAmount,
|
||||
PaidTierID: bal.PaidTierID,
|
||||
UpdatedAt: time.Now(),
|
||||
})
|
||||
return available
|
||||
}
|
||||
|
||||
// parseMetaFloat extracts a float64 from auth.Metadata (handles string and numeric types).
|
||||
func parseMetaFloat(metadata map[string]any, key string) (float64, bool) {
|
||||
v, ok := metadata[key]
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
switch typed := v.(type) {
|
||||
case float64:
|
||||
return typed, true
|
||||
case int:
|
||||
return float64(typed), true
|
||||
case int64:
|
||||
return float64(typed), true
|
||||
case uint64:
|
||||
return float64(typed), true
|
||||
case json.Number:
|
||||
if f, err := typed.Float64(); err == nil {
|
||||
return f, true
|
||||
}
|
||||
case string:
|
||||
if f, err := strconv.ParseFloat(strings.TrimSpace(typed), 64); err == nil {
|
||||
return f, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// AntigravityExecutor proxies requests to the antigravity upstream.
|
||||
type AntigravityExecutor struct {
|
||||
cfg *config.Config
|
||||
@@ -189,7 +244,7 @@ func validateAntigravityRequestSignatures(from sdktranslator.Format, rawJSON []b
|
||||
if from.String() != "claude" {
|
||||
return rawJSON, nil
|
||||
}
|
||||
// Always strip thinking blocks with empty signatures (proxy-generated).
|
||||
// Always strip thinking blocks with invalid signatures (empty or non-Claude-format).
|
||||
rawJSON = antigravityclaude.StripEmptySignatureThinkingBlocks(rawJSON)
|
||||
if cache.SignatureCacheEnabled() {
|
||||
return rawJSON, nil
|
||||
@@ -298,6 +353,41 @@ func decideAntigravity429(body []byte) antigravity429Decision {
|
||||
decision.retryAfter = retryAfter
|
||||
}
|
||||
|
||||
status := strings.TrimSpace(gjson.GetBytes(body, "error.status").String())
|
||||
if !strings.EqualFold(status, "RESOURCE_EXHAUSTED") {
|
||||
return decision
|
||||
}
|
||||
|
||||
details := gjson.GetBytes(body, "error.details")
|
||||
if details.Exists() && details.IsArray() {
|
||||
for _, detail := range details.Array() {
|
||||
if detail.Get("@type").String() != "type.googleapis.com/google.rpc.ErrorInfo" {
|
||||
continue
|
||||
}
|
||||
reason := strings.TrimSpace(detail.Get("reason").String())
|
||||
decision.reason = reason
|
||||
switch {
|
||||
case strings.EqualFold(reason, "QUOTA_EXHAUSTED"):
|
||||
decision.kind = antigravity429DecisionFullQuotaExhausted
|
||||
return decision
|
||||
case strings.EqualFold(reason, "RATE_LIMIT_EXCEEDED"):
|
||||
if decision.retryAfter == nil {
|
||||
decision.kind = antigravity429DecisionSoftRetry
|
||||
return decision
|
||||
}
|
||||
switch {
|
||||
case *decision.retryAfter < antigravityInstantRetryThreshold:
|
||||
decision.kind = antigravity429DecisionInstantRetrySameAuth
|
||||
case *decision.retryAfter < antigravityShortQuotaCooldownThreshold:
|
||||
decision.kind = antigravity429DecisionShortCooldownSwitchAuth
|
||||
default:
|
||||
decision.kind = antigravity429DecisionFullQuotaExhausted
|
||||
}
|
||||
return decision
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lowerBody := strings.ToLower(string(body))
|
||||
for _, keyword := range antigravityQuotaExhaustedKeywords {
|
||||
if strings.Contains(lowerBody, keyword) {
|
||||
@@ -307,123 +397,14 @@ func decideAntigravity429(body []byte) antigravity429Decision {
|
||||
}
|
||||
}
|
||||
|
||||
status := strings.TrimSpace(gjson.GetBytes(body, "error.status").String())
|
||||
if !strings.EqualFold(status, "RESOURCE_EXHAUSTED") {
|
||||
return decision
|
||||
}
|
||||
|
||||
details := gjson.GetBytes(body, "error.details")
|
||||
if !details.Exists() || !details.IsArray() {
|
||||
decision.kind = antigravity429DecisionSoftRetry
|
||||
return decision
|
||||
}
|
||||
|
||||
for _, detail := range details.Array() {
|
||||
if detail.Get("@type").String() != "type.googleapis.com/google.rpc.ErrorInfo" {
|
||||
continue
|
||||
}
|
||||
reason := strings.TrimSpace(detail.Get("reason").String())
|
||||
decision.reason = reason
|
||||
switch {
|
||||
case strings.EqualFold(reason, "QUOTA_EXHAUSTED"):
|
||||
decision.kind = antigravity429DecisionFullQuotaExhausted
|
||||
return decision
|
||||
case strings.EqualFold(reason, "RATE_LIMIT_EXCEEDED"):
|
||||
if decision.retryAfter == nil {
|
||||
decision.kind = antigravity429DecisionSoftRetry
|
||||
return decision
|
||||
}
|
||||
switch {
|
||||
case *decision.retryAfter < antigravityInstantRetryThreshold:
|
||||
decision.kind = antigravity429DecisionInstantRetrySameAuth
|
||||
case *decision.retryAfter < antigravityShortQuotaCooldownThreshold:
|
||||
decision.kind = antigravity429DecisionShortCooldownSwitchAuth
|
||||
default:
|
||||
decision.kind = antigravity429DecisionFullQuotaExhausted
|
||||
}
|
||||
return decision
|
||||
}
|
||||
}
|
||||
|
||||
decision.kind = antigravity429DecisionSoftRetry
|
||||
return decision
|
||||
}
|
||||
|
||||
func antigravityHasQuotaResetDelayOrModelInfo(body []byte) bool {
|
||||
if len(body) == 0 {
|
||||
return false
|
||||
}
|
||||
details := gjson.GetBytes(body, "error.details")
|
||||
if !details.Exists() || !details.IsArray() {
|
||||
return false
|
||||
}
|
||||
for _, detail := range details.Array() {
|
||||
if detail.Get("@type").String() != "type.googleapis.com/google.rpc.ErrorInfo" {
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(detail.Get("metadata.quotaResetDelay").String()) != "" {
|
||||
return true
|
||||
}
|
||||
if strings.TrimSpace(detail.Get("metadata.model").String()) != "" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func antigravityCreditsRetryEnabled(cfg *config.Config) bool {
|
||||
return cfg != nil && cfg.QuotaExceeded.AntigravityCredits
|
||||
}
|
||||
|
||||
func antigravityCreditsFailureStateForAuth(auth *cliproxyauth.Auth) (string, antigravityCreditsFailureState, bool) {
|
||||
if auth == nil || strings.TrimSpace(auth.ID) == "" {
|
||||
return "", antigravityCreditsFailureState{}, false
|
||||
}
|
||||
authID := strings.TrimSpace(auth.ID)
|
||||
value, ok := antigravityCreditsFailureByAuth.Load(authID)
|
||||
if !ok {
|
||||
return authID, antigravityCreditsFailureState{}, true
|
||||
}
|
||||
state, ok := value.(antigravityCreditsFailureState)
|
||||
if !ok {
|
||||
antigravityCreditsFailureByAuth.Delete(authID)
|
||||
return authID, antigravityCreditsFailureState{}, true
|
||||
}
|
||||
return authID, state, true
|
||||
}
|
||||
|
||||
func antigravityCreditsDisabled(auth *cliproxyauth.Auth, now time.Time) bool {
|
||||
authID, state, ok := antigravityCreditsFailureStateForAuth(auth)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if state.PermanentlyDisabled {
|
||||
return true
|
||||
}
|
||||
if state.DisabledUntil.IsZero() {
|
||||
return false
|
||||
}
|
||||
if state.DisabledUntil.After(now) {
|
||||
return true
|
||||
}
|
||||
antigravityCreditsFailureByAuth.Delete(authID)
|
||||
return false
|
||||
}
|
||||
|
||||
func recordAntigravityCreditsFailure(auth *cliproxyauth.Auth, now time.Time) {
|
||||
authID, state, ok := antigravityCreditsFailureStateForAuth(auth)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if state.PermanentlyDisabled {
|
||||
antigravityCreditsFailureByAuth.Store(authID, state)
|
||||
return
|
||||
}
|
||||
state.Count++
|
||||
state.DisabledUntil = now.Add(antigravityCreditsAutoDisableDuration)
|
||||
antigravityCreditsFailureByAuth.Store(authID, state)
|
||||
}
|
||||
|
||||
func clearAntigravityCreditsFailureState(auth *cliproxyauth.Auth) {
|
||||
if auth == nil || strings.TrimSpace(auth.ID) == "" {
|
||||
return
|
||||
@@ -440,6 +421,25 @@ func markAntigravityCreditsPermanentlyDisabled(auth *cliproxyauth.Auth) {
|
||||
ExplicitBalanceExhausted: true,
|
||||
}
|
||||
antigravityCreditsFailureByAuth.Store(authID, state)
|
||||
antigravityCreditsBalanceByAuth.Store(authID, antigravityCreditsBalance{
|
||||
CreditAmount: 0,
|
||||
MinCreditAmount: 1,
|
||||
Known: true,
|
||||
})
|
||||
cliproxyauth.SetAntigravityCreditsHint(authID, cliproxyauth.AntigravityCreditsHint{
|
||||
Known: true,
|
||||
Available: false,
|
||||
CreditAmount: 0,
|
||||
MinCreditAmount: 1,
|
||||
UpdatedAt: time.Now(),
|
||||
})
|
||||
}
|
||||
|
||||
func clearAntigravityCreditsPermanentlyDisabled(auth *cliproxyauth.Auth) {
|
||||
if auth == nil || strings.TrimSpace(auth.ID) == "" {
|
||||
return
|
||||
}
|
||||
antigravityCreditsFailureByAuth.Delete(strings.TrimSpace(auth.ID))
|
||||
}
|
||||
|
||||
func antigravityHasExplicitCreditsBalanceExhaustedReason(body []byte) bool {
|
||||
@@ -462,81 +462,6 @@ func antigravityHasExplicitCreditsBalanceExhaustedReason(body []byte) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func antigravityPreferCreditsKey(auth *cliproxyauth.Auth, modelName string) string {
|
||||
if auth == nil {
|
||||
return ""
|
||||
}
|
||||
authID := strings.TrimSpace(auth.ID)
|
||||
modelName = strings.TrimSpace(modelName)
|
||||
if authID == "" || modelName == "" {
|
||||
return ""
|
||||
}
|
||||
return authID + "|" + modelName
|
||||
}
|
||||
|
||||
func antigravityShouldPreferCredits(auth *cliproxyauth.Auth, modelName string, now time.Time) bool {
|
||||
key := antigravityPreferCreditsKey(auth, modelName)
|
||||
if key == "" {
|
||||
return false
|
||||
}
|
||||
value, ok := antigravityPreferCreditsByModel.Load(key)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
until, ok := value.(time.Time)
|
||||
if !ok || until.IsZero() {
|
||||
antigravityPreferCreditsByModel.Delete(key)
|
||||
return false
|
||||
}
|
||||
if !until.After(now) {
|
||||
antigravityPreferCreditsByModel.Delete(key)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func markAntigravityPreferCredits(auth *cliproxyauth.Auth, modelName string, now time.Time, retryAfter *time.Duration) {
|
||||
key := antigravityPreferCreditsKey(auth, modelName)
|
||||
if key == "" {
|
||||
return
|
||||
}
|
||||
until := now.Add(antigravityCreditsRetryTTL)
|
||||
if retryAfter != nil && *retryAfter > 0 {
|
||||
until = now.Add(*retryAfter)
|
||||
}
|
||||
antigravityPreferCreditsByModel.Store(key, until)
|
||||
}
|
||||
|
||||
func clearAntigravityPreferCredits(auth *cliproxyauth.Auth, modelName string) {
|
||||
key := antigravityPreferCreditsKey(auth, modelName)
|
||||
if key == "" {
|
||||
return
|
||||
}
|
||||
antigravityPreferCreditsByModel.Delete(key)
|
||||
}
|
||||
|
||||
func shouldMarkAntigravityCreditsExhausted(statusCode int, body []byte, reqErr error) bool {
|
||||
if reqErr != nil || statusCode == 0 {
|
||||
return false
|
||||
}
|
||||
if statusCode >= http.StatusInternalServerError || statusCode == http.StatusRequestTimeout {
|
||||
return false
|
||||
}
|
||||
lowerBody := strings.ToLower(string(body))
|
||||
for _, keyword := range antigravityCreditsExhaustedKeywords {
|
||||
if strings.Contains(lowerBody, keyword) {
|
||||
if keyword == "resource has been exhausted" &&
|
||||
statusCode == http.StatusTooManyRequests &&
|
||||
decideAntigravity429(body).kind == antigravity429DecisionSoftRetry &&
|
||||
!antigravityHasQuotaResetDelayOrModelInfo(body) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func newAntigravityStatusErr(statusCode int, body []byte) statusErr {
|
||||
err := statusErr{code: statusCode, msg: string(body)}
|
||||
if statusCode == http.StatusTooManyRequests {
|
||||
@@ -547,129 +472,6 @@ func newAntigravityStatusErr(statusCode int, body []byte) statusErr {
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *AntigravityExecutor) attemptCreditsFallback(
|
||||
ctx context.Context,
|
||||
auth *cliproxyauth.Auth,
|
||||
httpClient *http.Client,
|
||||
token string,
|
||||
modelName string,
|
||||
payload []byte,
|
||||
stream bool,
|
||||
alt string,
|
||||
baseURL string,
|
||||
originalBody []byte,
|
||||
) (*http.Response, bool) {
|
||||
if !antigravityCreditsRetryEnabled(e.cfg) {
|
||||
return nil, false
|
||||
}
|
||||
if decideAntigravity429(originalBody).kind != antigravity429DecisionFullQuotaExhausted {
|
||||
return nil, false
|
||||
}
|
||||
now := time.Now()
|
||||
if shouldForcePermanentDisableCredits(originalBody) {
|
||||
clearAntigravityPreferCredits(auth, modelName)
|
||||
markAntigravityCreditsPermanentlyDisabled(auth)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if antigravityHasExplicitCreditsBalanceExhaustedReason(originalBody) {
|
||||
clearAntigravityPreferCredits(auth, modelName)
|
||||
markAntigravityCreditsPermanentlyDisabled(auth)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if antigravityCreditsDisabled(auth, now) {
|
||||
return nil, false
|
||||
}
|
||||
creditsPayload := injectEnabledCreditTypes(payload)
|
||||
if len(creditsPayload) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
httpReq, errReq := e.buildRequest(ctx, auth, token, modelName, creditsPayload, stream, alt, baseURL)
|
||||
if errReq != nil {
|
||||
helps.RecordAPIResponseError(ctx, e.cfg, errReq)
|
||||
clearAntigravityPreferCredits(auth, modelName)
|
||||
recordAntigravityCreditsFailure(auth, now)
|
||||
return nil, true
|
||||
}
|
||||
httpResp, errDo := httpClient.Do(httpReq)
|
||||
if errDo != nil {
|
||||
helps.RecordAPIResponseError(ctx, e.cfg, errDo)
|
||||
clearAntigravityPreferCredits(auth, modelName)
|
||||
recordAntigravityCreditsFailure(auth, now)
|
||||
return nil, true
|
||||
}
|
||||
if httpResp.StatusCode >= http.StatusOK && httpResp.StatusCode < http.StatusMultipleChoices {
|
||||
retryAfter, _ := parseRetryDelay(originalBody)
|
||||
markAntigravityPreferCredits(auth, modelName, now, retryAfter)
|
||||
clearAntigravityCreditsFailureState(auth)
|
||||
return httpResp, true
|
||||
}
|
||||
|
||||
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
|
||||
bodyBytes, errRead := io.ReadAll(httpResp.Body)
|
||||
if errClose := httpResp.Body.Close(); errClose != nil {
|
||||
log.Errorf("antigravity executor: close credits fallback response body error: %v", errClose)
|
||||
}
|
||||
if errRead != nil {
|
||||
helps.RecordAPIResponseError(ctx, e.cfg, errRead)
|
||||
clearAntigravityPreferCredits(auth, modelName)
|
||||
recordAntigravityCreditsFailure(auth, now)
|
||||
return nil, true
|
||||
}
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, bodyBytes)
|
||||
if shouldForcePermanentDisableCredits(bodyBytes) {
|
||||
clearAntigravityPreferCredits(auth, modelName)
|
||||
markAntigravityCreditsPermanentlyDisabled(auth)
|
||||
return nil, true
|
||||
}
|
||||
|
||||
if antigravityHasExplicitCreditsBalanceExhaustedReason(bodyBytes) {
|
||||
clearAntigravityPreferCredits(auth, modelName)
|
||||
markAntigravityCreditsPermanentlyDisabled(auth)
|
||||
return nil, true
|
||||
}
|
||||
|
||||
clearAntigravityPreferCredits(auth, modelName)
|
||||
recordAntigravityCreditsFailure(auth, now)
|
||||
return nil, true
|
||||
}
|
||||
|
||||
func (e *AntigravityExecutor) handleDirectCreditsFailure(ctx context.Context, auth *cliproxyauth.Auth, modelName string, reqErr error) {
|
||||
if reqErr != nil {
|
||||
if shouldForcePermanentDisableCredits(reqErrBody(reqErr)) {
|
||||
clearAntigravityPreferCredits(auth, modelName)
|
||||
markAntigravityCreditsPermanentlyDisabled(auth)
|
||||
return
|
||||
}
|
||||
|
||||
if antigravityHasExplicitCreditsBalanceExhaustedReason(reqErrBody(reqErr)) {
|
||||
clearAntigravityPreferCredits(auth, modelName)
|
||||
markAntigravityCreditsPermanentlyDisabled(auth)
|
||||
return
|
||||
}
|
||||
|
||||
helps.RecordAPIResponseError(ctx, e.cfg, reqErr)
|
||||
}
|
||||
clearAntigravityPreferCredits(auth, modelName)
|
||||
recordAntigravityCreditsFailure(auth, time.Now())
|
||||
}
|
||||
func reqErrBody(reqErr error) []byte {
|
||||
if reqErr == nil {
|
||||
return nil
|
||||
}
|
||||
msg := reqErr.Error()
|
||||
if strings.TrimSpace(msg) == "" {
|
||||
return nil
|
||||
}
|
||||
return []byte(msg)
|
||||
}
|
||||
|
||||
func shouldForcePermanentDisableCredits(body []byte) bool {
|
||||
return antigravityHasExplicitCreditsBalanceExhaustedReason(body)
|
||||
}
|
||||
|
||||
// Execute performs a non-streaming request to the Antigravity API.
|
||||
func (e *AntigravityExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (resp cliproxyexecutor.Response, err error) {
|
||||
if opts.Alt == "responses/compact" {
|
||||
@@ -721,6 +523,8 @@ func (e *AntigravityExecutor) Execute(ctx context.Context, auth *cliproxyauth.Au
|
||||
requestedModel := helps.PayloadRequestedModel(opts, req.Model)
|
||||
translated = helps.ApplyPayloadConfigWithRoot(e.cfg, baseModel, "antigravity", "request", translated, originalTranslated, requestedModel)
|
||||
|
||||
useCredits := cliproxyauth.AntigravityCreditsRequested(ctx) && antigravityCreditsRetryEnabled(e.cfg)
|
||||
|
||||
baseURLs := antigravityBaseURLFallbackOrder(auth)
|
||||
httpClient := newAntigravityHTTPClient(ctx, e.cfg, auth, 0)
|
||||
attempts := antigravityRetryAttempts(auth, e.cfg)
|
||||
@@ -733,11 +537,10 @@ attemptLoop:
|
||||
|
||||
for idx, baseURL := range baseURLs {
|
||||
requestPayload := translated
|
||||
usedCreditsDirect := false
|
||||
if antigravityCreditsRetryEnabled(e.cfg) && antigravityShouldPreferCredits(auth, baseModel, time.Now()) {
|
||||
if creditsPayload := injectEnabledCreditTypes(translated); len(creditsPayload) > 0 {
|
||||
requestPayload = creditsPayload
|
||||
usedCreditsDirect = true
|
||||
if useCredits {
|
||||
if cp := injectEnabledCreditTypes(translated); len(cp) > 0 {
|
||||
requestPayload = cp
|
||||
helps.MarkCreditsUsed(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -785,7 +588,6 @@ attemptLoop:
|
||||
wait := antigravityInstantRetryDelay(*decision.retryAfter)
|
||||
log.Debugf("antigravity executor: instant retry for model %s, waiting %s", baseModel, wait)
|
||||
if errWait := antigravityWait(ctx, wait); errWait != nil {
|
||||
|
||||
return resp, errWait
|
||||
}
|
||||
}
|
||||
@@ -794,34 +596,13 @@ attemptLoop:
|
||||
case antigravity429DecisionShortCooldownSwitchAuth:
|
||||
if decision.retryAfter != nil && *decision.retryAfter > 0 {
|
||||
markAntigravityShortCooldown(auth, baseModel, time.Now(), *decision.retryAfter)
|
||||
log.Debugf("antigravity executor: short quota cooldown (%s) for model %s, recorded cooldown and skipping credits fallback", *decision.retryAfter, baseModel)
|
||||
log.Debugf("antigravity executor: short quota cooldown (%s) for model %s, recorded cooldown", *decision.retryAfter, baseModel)
|
||||
}
|
||||
case antigravity429DecisionFullQuotaExhausted:
|
||||
if usedCreditsDirect {
|
||||
clearAntigravityPreferCredits(auth, baseModel)
|
||||
recordAntigravityCreditsFailure(auth, time.Now())
|
||||
} else {
|
||||
creditsResp, _ := e.attemptCreditsFallback(ctx, auth, httpClient, token, baseModel, translated, false, opts.Alt, baseURL, bodyBytes)
|
||||
if creditsResp != nil {
|
||||
helps.RecordAPIResponseMetadata(ctx, e.cfg, creditsResp.StatusCode, creditsResp.Header.Clone())
|
||||
creditsBody, errCreditsRead := io.ReadAll(creditsResp.Body)
|
||||
if errClose := creditsResp.Body.Close(); errClose != nil {
|
||||
log.Errorf("antigravity executor: close credits success response body error: %v", errClose)
|
||||
}
|
||||
if errCreditsRead != nil {
|
||||
helps.RecordAPIResponseError(ctx, e.cfg, errCreditsRead)
|
||||
err = errCreditsRead
|
||||
return resp, err
|
||||
}
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, creditsBody)
|
||||
reporter.Publish(ctx, helps.ParseAntigravityUsage(creditsBody))
|
||||
var param any
|
||||
converted := sdktranslator.TranslateNonStream(ctx, to, from, req.Model, opts.OriginalRequest, translated, creditsBody, ¶m)
|
||||
resp = cliproxyexecutor.Response{Payload: converted, Headers: creditsResp.Header.Clone()}
|
||||
reporter.EnsurePublished(ctx)
|
||||
return resp, nil
|
||||
}
|
||||
if useCredits && antigravityHasExplicitCreditsBalanceExhaustedReason(bodyBytes) {
|
||||
markAntigravityCreditsPermanentlyDisabled(auth)
|
||||
}
|
||||
// No credits logic - just fall through to error return below
|
||||
}
|
||||
}
|
||||
|
||||
@@ -870,6 +651,10 @@ attemptLoop:
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// Success
|
||||
if useCredits {
|
||||
clearAntigravityCreditsFailureState(auth)
|
||||
}
|
||||
reporter.Publish(ctx, helps.ParseAntigravityUsage(bodyBytes))
|
||||
var param any
|
||||
converted := sdktranslator.TranslateNonStream(ctx, to, from, req.Model, opts.OriginalRequest, translated, bodyBytes, ¶m)
|
||||
@@ -935,6 +720,8 @@ func (e *AntigravityExecutor) executeClaudeNonStream(ctx context.Context, auth *
|
||||
requestedModel := helps.PayloadRequestedModel(opts, req.Model)
|
||||
translated = helps.ApplyPayloadConfigWithRoot(e.cfg, baseModel, "antigravity", "request", translated, originalTranslated, requestedModel)
|
||||
|
||||
useCredits := cliproxyauth.AntigravityCreditsRequested(ctx) && antigravityCreditsRetryEnabled(e.cfg)
|
||||
|
||||
baseURLs := antigravityBaseURLFallbackOrder(auth)
|
||||
httpClient := newAntigravityHTTPClient(ctx, e.cfg, auth, 0)
|
||||
|
||||
@@ -948,11 +735,10 @@ attemptLoop:
|
||||
|
||||
for idx, baseURL := range baseURLs {
|
||||
requestPayload := translated
|
||||
usedCreditsDirect := false
|
||||
if antigravityCreditsRetryEnabled(e.cfg) && antigravityShouldPreferCredits(auth, baseModel, time.Now()) {
|
||||
if creditsPayload := injectEnabledCreditTypes(translated); len(creditsPayload) > 0 {
|
||||
requestPayload = creditsPayload
|
||||
usedCreditsDirect = true
|
||||
if useCredits {
|
||||
if cp := injectEnabledCreditTypes(translated); len(cp) > 0 {
|
||||
requestPayload = cp
|
||||
helps.MarkCreditsUsed(ctx)
|
||||
}
|
||||
}
|
||||
httpReq, errReq := e.buildRequest(ctx, auth, token, baseModel, requestPayload, true, opts.Alt, baseURL)
|
||||
@@ -1014,7 +800,6 @@ attemptLoop:
|
||||
wait := antigravityInstantRetryDelay(*decision.retryAfter)
|
||||
log.Debugf("antigravity executor: instant retry for model %s, waiting %s", baseModel, wait)
|
||||
if errWait := antigravityWait(ctx, wait); errWait != nil {
|
||||
|
||||
return resp, errWait
|
||||
}
|
||||
}
|
||||
@@ -1023,25 +808,16 @@ attemptLoop:
|
||||
case antigravity429DecisionShortCooldownSwitchAuth:
|
||||
if decision.retryAfter != nil && *decision.retryAfter > 0 {
|
||||
markAntigravityShortCooldown(auth, baseModel, time.Now(), *decision.retryAfter)
|
||||
log.Debugf("antigravity executor: short quota cooldown (%s) for model %s, recorded cooldown and skipping credits fallback", *decision.retryAfter, baseModel)
|
||||
log.Debugf("antigravity executor: short quota cooldown (%s) for model %s, recorded cooldown", *decision.retryAfter, baseModel)
|
||||
}
|
||||
case antigravity429DecisionFullQuotaExhausted:
|
||||
if usedCreditsDirect {
|
||||
clearAntigravityPreferCredits(auth, baseModel)
|
||||
recordAntigravityCreditsFailure(auth, time.Now())
|
||||
} else {
|
||||
creditsResp, _ := e.attemptCreditsFallback(ctx, auth, httpClient, token, baseModel, translated, true, opts.Alt, baseURL, bodyBytes)
|
||||
if creditsResp != nil {
|
||||
httpResp = creditsResp
|
||||
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
|
||||
}
|
||||
if useCredits && antigravityHasExplicitCreditsBalanceExhaustedReason(bodyBytes) {
|
||||
markAntigravityCreditsPermanentlyDisabled(auth)
|
||||
}
|
||||
// No credits logic - just fall through to error return below
|
||||
}
|
||||
}
|
||||
|
||||
if httpResp.StatusCode >= http.StatusOK && httpResp.StatusCode < http.StatusMultipleChoices {
|
||||
goto streamSuccessClaudeNonStream
|
||||
}
|
||||
lastStatus = httpResp.StatusCode
|
||||
lastBody = append([]byte(nil), bodyBytes...)
|
||||
lastErr = nil
|
||||
@@ -1085,7 +861,10 @@ attemptLoop:
|
||||
return resp, err
|
||||
}
|
||||
|
||||
streamSuccessClaudeNonStream:
|
||||
// Stream success
|
||||
if useCredits {
|
||||
clearAntigravityCreditsFailureState(auth)
|
||||
}
|
||||
out := make(chan cliproxyexecutor.StreamChunk)
|
||||
go func(resp *http.Response) {
|
||||
defer close(out)
|
||||
@@ -1389,6 +1168,7 @@ func (e *AntigravityExecutor) ExecuteStream(ctx context.Context, auth *cliproxya
|
||||
if updatedAuth != nil {
|
||||
auth = updatedAuth
|
||||
}
|
||||
|
||||
originalTranslated := sdktranslator.TranslateRequest(from, to, baseModel, originalPayload, true)
|
||||
translated := sdktranslator.TranslateRequest(from, to, baseModel, req.Payload, true)
|
||||
|
||||
@@ -1400,6 +1180,8 @@ func (e *AntigravityExecutor) ExecuteStream(ctx context.Context, auth *cliproxya
|
||||
requestedModel := helps.PayloadRequestedModel(opts, req.Model)
|
||||
translated = helps.ApplyPayloadConfigWithRoot(e.cfg, baseModel, "antigravity", "request", translated, originalTranslated, requestedModel)
|
||||
|
||||
useCredits := cliproxyauth.AntigravityCreditsRequested(ctx) && antigravityCreditsRetryEnabled(e.cfg)
|
||||
|
||||
baseURLs := antigravityBaseURLFallbackOrder(auth)
|
||||
httpClient := newAntigravityHTTPClient(ctx, e.cfg, auth, 0)
|
||||
|
||||
@@ -1413,11 +1195,10 @@ attemptLoop:
|
||||
|
||||
for idx, baseURL := range baseURLs {
|
||||
requestPayload := translated
|
||||
usedCreditsDirect := false
|
||||
if antigravityCreditsRetryEnabled(e.cfg) && antigravityShouldPreferCredits(auth, baseModel, time.Now()) {
|
||||
if creditsPayload := injectEnabledCreditTypes(translated); len(creditsPayload) > 0 {
|
||||
requestPayload = creditsPayload
|
||||
usedCreditsDirect = true
|
||||
if useCredits {
|
||||
if cp := injectEnabledCreditTypes(translated); len(cp) > 0 {
|
||||
requestPayload = cp
|
||||
helps.MarkCreditsUsed(ctx)
|
||||
}
|
||||
}
|
||||
httpReq, errReq := e.buildRequest(ctx, auth, token, baseModel, requestPayload, true, opts.Alt, baseURL)
|
||||
@@ -1478,7 +1259,6 @@ attemptLoop:
|
||||
wait := antigravityInstantRetryDelay(*decision.retryAfter)
|
||||
log.Debugf("antigravity executor: instant retry for model %s, waiting %s", baseModel, wait)
|
||||
if errWait := antigravityWait(ctx, wait); errWait != nil {
|
||||
|
||||
return nil, errWait
|
||||
}
|
||||
}
|
||||
@@ -1487,25 +1267,16 @@ attemptLoop:
|
||||
case antigravity429DecisionShortCooldownSwitchAuth:
|
||||
if decision.retryAfter != nil && *decision.retryAfter > 0 {
|
||||
markAntigravityShortCooldown(auth, baseModel, time.Now(), *decision.retryAfter)
|
||||
log.Debugf("antigravity executor: short quota cooldown (%s) for model %s, recorded cooldown and skipping credits fallback", *decision.retryAfter, baseModel)
|
||||
log.Debugf("antigravity executor: short quota cooldown (%s) for model %s recorded", *decision.retryAfter, baseModel)
|
||||
}
|
||||
case antigravity429DecisionFullQuotaExhausted:
|
||||
if usedCreditsDirect {
|
||||
clearAntigravityPreferCredits(auth, baseModel)
|
||||
recordAntigravityCreditsFailure(auth, time.Now())
|
||||
} else {
|
||||
creditsResp, _ := e.attemptCreditsFallback(ctx, auth, httpClient, token, baseModel, translated, true, opts.Alt, baseURL, bodyBytes)
|
||||
if creditsResp != nil {
|
||||
httpResp = creditsResp
|
||||
helps.RecordAPIResponseMetadata(ctx, e.cfg, httpResp.StatusCode, httpResp.Header.Clone())
|
||||
}
|
||||
if useCredits && antigravityHasExplicitCreditsBalanceExhaustedReason(bodyBytes) {
|
||||
markAntigravityCreditsPermanentlyDisabled(auth)
|
||||
}
|
||||
// No credits logic - just fall through to error return below
|
||||
}
|
||||
}
|
||||
|
||||
if httpResp.StatusCode >= http.StatusOK && httpResp.StatusCode < http.StatusMultipleChoices {
|
||||
goto streamSuccessExecuteStream
|
||||
}
|
||||
lastStatus = httpResp.StatusCode
|
||||
lastBody = append([]byte(nil), bodyBytes...)
|
||||
lastErr = nil
|
||||
@@ -1549,7 +1320,10 @@ attemptLoop:
|
||||
return nil, err
|
||||
}
|
||||
|
||||
streamSuccessExecuteStream:
|
||||
// Stream success
|
||||
if useCredits {
|
||||
clearAntigravityCreditsFailureState(auth)
|
||||
}
|
||||
out := make(chan cliproxyexecutor.StreamChunk)
|
||||
go func(resp *http.Response) {
|
||||
defer close(out)
|
||||
@@ -1792,6 +1566,7 @@ func (e *AntigravityExecutor) ensureAccessToken(ctx context.Context, auth *clipr
|
||||
accessToken := metaStringValue(auth.Metadata, "access_token")
|
||||
expiry := tokenExpiry(auth.Metadata)
|
||||
if accessToken != "" && expiry.After(time.Now().Add(refreshSkew)) {
|
||||
e.maybeRefreshAntigravityCreditsHint(ctx, auth, accessToken)
|
||||
return accessToken, nil, nil
|
||||
}
|
||||
refreshCtx := context.Background()
|
||||
@@ -1807,6 +1582,63 @@ func (e *AntigravityExecutor) ensureAccessToken(ctx context.Context, auth *clipr
|
||||
return metaStringValue(updated.Metadata, "access_token"), updated, nil
|
||||
}
|
||||
|
||||
func (e *AntigravityExecutor) maybeRefreshAntigravityCreditsHint(ctx context.Context, auth *cliproxyauth.Auth, accessToken string) {
|
||||
if e == nil || auth == nil || !antigravityCreditsRetryEnabled(e.cfg) {
|
||||
return
|
||||
}
|
||||
if ctx != nil && ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
authID := strings.TrimSpace(auth.ID)
|
||||
if authID == "" {
|
||||
return
|
||||
}
|
||||
if hint, ok := cliproxyauth.GetAntigravityCreditsHint(authID); ok && hint.Known {
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(accessToken) == "" {
|
||||
accessToken = metaStringValue(auth.Metadata, "access_token")
|
||||
}
|
||||
if strings.TrimSpace(accessToken) == "" {
|
||||
return
|
||||
}
|
||||
|
||||
state := &antigravityCreditsHintRefreshState{}
|
||||
if existing, loaded := antigravityCreditsHintRefreshByID.LoadOrStore(authID, state); loaded {
|
||||
if cast, ok := existing.(*antigravityCreditsHintRefreshState); ok && cast != nil {
|
||||
state = cast
|
||||
} else {
|
||||
antigravityCreditsHintRefreshByID.Delete(authID)
|
||||
antigravityCreditsHintRefreshByID.Store(authID, state)
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
if !state.mu.TryLock() {
|
||||
return
|
||||
}
|
||||
if !state.lastAttempt.IsZero() && now.Sub(state.lastAttempt) < antigravityCreditsHintRefreshInterval {
|
||||
state.mu.Unlock()
|
||||
return
|
||||
}
|
||||
state.lastAttempt = now
|
||||
|
||||
refreshCtx := context.Background()
|
||||
if ctx != nil {
|
||||
if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
|
||||
refreshCtx = context.WithValue(refreshCtx, "cliproxy.roundtripper", rt)
|
||||
}
|
||||
}
|
||||
refreshCtx, cancel := context.WithTimeout(refreshCtx, antigravityCreditsHintRefreshTimeout)
|
||||
authCopy := auth.Clone()
|
||||
|
||||
go func(state *antigravityCreditsHintRefreshState, auth *cliproxyauth.Auth, token string) {
|
||||
defer cancel()
|
||||
defer state.mu.Unlock()
|
||||
e.updateAntigravityCreditsBalance(refreshCtx, auth, token)
|
||||
}(state, authCopy, accessToken)
|
||||
}
|
||||
|
||||
func (e *AntigravityExecutor) refreshToken(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error) {
|
||||
if auth == nil {
|
||||
return nil, statusErr{code: http.StatusUnauthorized, msg: "missing auth"}
|
||||
@@ -1882,6 +1714,7 @@ func (e *AntigravityExecutor) refreshToken(ctx context.Context, auth *cliproxyau
|
||||
if errProject := e.ensureAntigravityProjectID(ctx, auth, tokenResp.AccessToken); errProject != nil {
|
||||
log.Warnf("antigravity executor: ensure project id failed: %v", errProject)
|
||||
}
|
||||
e.updateAntigravityCreditsBalance(ctx, auth, tokenResp.AccessToken)
|
||||
return auth, nil
|
||||
}
|
||||
|
||||
@@ -1918,6 +1751,94 @@ func (e *AntigravityExecutor) ensureAntigravityProjectID(ctx context.Context, au
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *AntigravityExecutor) updateAntigravityCreditsBalance(ctx context.Context, auth *cliproxyauth.Auth, accessToken string) {
|
||||
if auth == nil || strings.TrimSpace(auth.ID) == "" {
|
||||
return
|
||||
}
|
||||
token := strings.TrimSpace(accessToken)
|
||||
if token == "" {
|
||||
token = metaStringValue(auth.Metadata, "access_token")
|
||||
}
|
||||
if token == "" {
|
||||
return
|
||||
}
|
||||
|
||||
loadReqBody := `{"metadata":{"ideType":"ANTIGRAVITY","platform":"PLATFORM_UNSPECIFIED","pluginType":"GEMINI"}}`
|
||||
endpointURL := "https://cloudcode-pa.googleapis.com/v1internal:loadCodeAssist"
|
||||
httpReq, errReq := http.NewRequestWithContext(ctx, http.MethodPost, endpointURL, strings.NewReader(loadReqBody))
|
||||
if errReq != nil {
|
||||
log.Debugf("antigravity executor: create loadCodeAssist request error: %v", errReq)
|
||||
return
|
||||
}
|
||||
httpReq.Header.Set("Authorization", "Bearer "+token)
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("User-Agent", "google-api-nodejs-client/9.15.1")
|
||||
|
||||
httpClient := newAntigravityHTTPClient(ctx, e.cfg, auth, 0)
|
||||
httpResp, errDo := httpClient.Do(httpReq)
|
||||
if errDo != nil {
|
||||
log.Debugf("antigravity executor: loadCodeAssist request error: %v", errDo)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if errClose := httpResp.Body.Close(); errClose != nil {
|
||||
log.Errorf("antigravity executor: close loadCodeAssist response body error: %v", errClose)
|
||||
}
|
||||
}()
|
||||
|
||||
bodyBytes, errRead := io.ReadAll(httpResp.Body)
|
||||
if errRead != nil || httpResp.StatusCode < http.StatusOK || httpResp.StatusCode >= http.StatusMultipleChoices {
|
||||
log.Debugf("antigravity executor: loadCodeAssist returned status %d, err=%v", httpResp.StatusCode, errRead)
|
||||
return
|
||||
}
|
||||
|
||||
authID := strings.TrimSpace(auth.ID)
|
||||
paidTierID := strings.TrimSpace(gjson.GetBytes(bodyBytes, "paidTier.id").String())
|
||||
|
||||
credits := gjson.GetBytes(bodyBytes, "paidTier.availableCredits")
|
||||
if !credits.IsArray() {
|
||||
cliproxyauth.SetAntigravityCreditsHint(authID, cliproxyauth.AntigravityCreditsHint{
|
||||
Known: true,
|
||||
Available: false,
|
||||
PaidTierID: paidTierID,
|
||||
UpdatedAt: time.Now(),
|
||||
})
|
||||
return
|
||||
}
|
||||
for _, credit := range credits.Array() {
|
||||
if !strings.EqualFold(credit.Get("creditType").String(), "GOOGLE_ONE_AI") {
|
||||
continue
|
||||
}
|
||||
creditAmount, errCA := strconv.ParseFloat(strings.TrimSpace(credit.Get("creditAmount").String()), 64)
|
||||
if errCA != nil {
|
||||
continue
|
||||
}
|
||||
minAmount, errMA := strconv.ParseFloat(strings.TrimSpace(credit.Get("minimumCreditAmountForUsage").String()), 64)
|
||||
if errMA != nil {
|
||||
continue
|
||||
}
|
||||
bal := antigravityCreditsBalance{
|
||||
CreditAmount: creditAmount,
|
||||
MinCreditAmount: minAmount,
|
||||
PaidTierID: paidTierID,
|
||||
Known: true,
|
||||
}
|
||||
antigravityCreditsBalanceByAuth.Store(authID, bal)
|
||||
cliproxyauth.SetAntigravityCreditsHint(authID, cliproxyauth.AntigravityCreditsHint{
|
||||
Known: true,
|
||||
Available: creditAmount >= minAmount,
|
||||
CreditAmount: creditAmount,
|
||||
MinCreditAmount: minAmount,
|
||||
PaidTierID: paidTierID,
|
||||
UpdatedAt: time.Now(),
|
||||
})
|
||||
if creditAmount >= minAmount {
|
||||
clearAntigravityCreditsPermanentlyDisabled(auth)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e *AntigravityExecutor) buildRequest(ctx context.Context, auth *cliproxyauth.Auth, token, modelName string, payload []byte, stream bool, alt, baseURL string) (*http.Request, error) {
|
||||
if token == "" {
|
||||
return nil, statusErr{code: http.StatusUnauthorized, msg: "missing access token"}
|
||||
|
||||
@@ -18,8 +18,9 @@ import (
|
||||
|
||||
func resetAntigravityCreditsRetryState() {
|
||||
antigravityCreditsFailureByAuth = sync.Map{}
|
||||
antigravityPreferCreditsByModel = sync.Map{}
|
||||
antigravityShortCooldownByAuth = sync.Map{}
|
||||
antigravityCreditsBalanceByAuth = sync.Map{}
|
||||
antigravityCreditsHintRefreshByID = sync.Map{}
|
||||
}
|
||||
|
||||
func TestClassifyAntigravity429(t *testing.T) {
|
||||
@@ -30,6 +31,43 @@ func TestClassifyAntigravity429(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("standard antigravity rate limit with ui message stays rate limited", func(t *testing.T) {
|
||||
body := []byte(`{
|
||||
"error": {
|
||||
"code": 429,
|
||||
"message": "You have exhausted your capacity on this model. Your quota will reset after 0s.",
|
||||
"status": "RESOURCE_EXHAUSTED",
|
||||
"details": [
|
||||
{
|
||||
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
|
||||
"reason": "RATE_LIMIT_EXCEEDED",
|
||||
"domain": "cloudcode-pa.googleapis.com",
|
||||
"metadata": {
|
||||
"model": "claude-opus-4-6-thinking",
|
||||
"quotaResetDelay": "479.417207ms",
|
||||
"quotaResetTimeStamp": "2026-04-20T09:19:49Z",
|
||||
"uiMessage": "true"
|
||||
}
|
||||
},
|
||||
{
|
||||
"@type": "type.googleapis.com/google.rpc.RetryInfo",
|
||||
"retryDelay": "0.479417207s"
|
||||
}
|
||||
]
|
||||
}
|
||||
}`)
|
||||
if got := classifyAntigravity429(body); got != antigravity429RateLimited {
|
||||
t.Fatalf("classifyAntigravity429() = %q, want %q", got, antigravity429RateLimited)
|
||||
}
|
||||
decision := decideAntigravity429(body)
|
||||
if decision.kind != antigravity429DecisionInstantRetrySameAuth {
|
||||
t.Fatalf("decideAntigravity429().kind = %q, want %q", decision.kind, antigravity429DecisionInstantRetrySameAuth)
|
||||
}
|
||||
if decision.retryAfter == nil {
|
||||
t.Fatal("decideAntigravity429().retryAfter = nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("structured rate limit", func(t *testing.T) {
|
||||
body := []byte(`{
|
||||
"error": {
|
||||
@@ -67,8 +105,31 @@ func TestClassifyAntigravity429(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestAntigravityShouldRetryNoCapacity_Standard503(t *testing.T) {
|
||||
body := []byte(`{
|
||||
"error": {
|
||||
"code": 503,
|
||||
"message": "No capacity available for model gemini-3.1-flash-image on the server",
|
||||
"status": "UNAVAILABLE",
|
||||
"details": [
|
||||
{
|
||||
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
|
||||
"reason": "MODEL_CAPACITY_EXHAUSTED",
|
||||
"domain": "cloudcode-pa.googleapis.com",
|
||||
"metadata": {
|
||||
"model": "gemini-3.1-flash-image"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}`)
|
||||
if !antigravityShouldRetryNoCapacity(http.StatusServiceUnavailable, body) {
|
||||
t.Fatal("antigravityShouldRetryNoCapacity() = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInjectEnabledCreditTypes(t *testing.T) {
|
||||
body := []byte(`{"model":"gemini-2.5-flash","request":{}}`)
|
||||
body := []byte(`{"model":"claude-sonnet-4-6","request":{}}`)
|
||||
got := injectEnabledCreditTypes(body)
|
||||
if got == nil {
|
||||
t.Fatal("injectEnabledCreditTypes() returned nil")
|
||||
@@ -82,34 +143,18 @@ func TestInjectEnabledCreditTypes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldMarkAntigravityCreditsExhausted(t *testing.T) {
|
||||
t.Run("credit errors are marked", func(t *testing.T) {
|
||||
for _, body := range [][]byte{
|
||||
[]byte(`{"error":{"message":"Insufficient GOOGLE_ONE_AI credits"}}`),
|
||||
[]byte(`{"error":{"message":"minimumCreditAmountForUsage requirement not met"}}`),
|
||||
} {
|
||||
if !shouldMarkAntigravityCreditsExhausted(http.StatusForbidden, body, nil) {
|
||||
t.Fatalf("shouldMarkAntigravityCreditsExhausted(%s) = false, want true", string(body))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("transient 429 resource exhausted is not marked", func(t *testing.T) {
|
||||
body := []byte(`{"error":{"code":429,"message":"Resource has been exhausted (e.g. check quota).","status":"RESOURCE_EXHAUSTED"}}`)
|
||||
if shouldMarkAntigravityCreditsExhausted(http.StatusTooManyRequests, body, nil) {
|
||||
t.Fatalf("shouldMarkAntigravityCreditsExhausted(%s) = true, want false", string(body))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("resource exhausted with quota metadata is still marked", func(t *testing.T) {
|
||||
body := []byte(`{"error":{"code":429,"message":"Resource has been exhausted","status":"RESOURCE_EXHAUSTED","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","metadata":{"quotaResetDelay":"1h","model":"claude-sonnet-4-6"}}]}}`)
|
||||
if !shouldMarkAntigravityCreditsExhausted(http.StatusTooManyRequests, body, nil) {
|
||||
t.Fatalf("shouldMarkAntigravityCreditsExhausted(%s) = false, want true", string(body))
|
||||
}
|
||||
})
|
||||
|
||||
if shouldMarkAntigravityCreditsExhausted(http.StatusServiceUnavailable, []byte(`{"error":{"message":"credits exhausted"}}`), nil) {
|
||||
t.Fatal("shouldMarkAntigravityCreditsExhausted() = true for 5xx, want false")
|
||||
func TestParseRetryDelay_HumanReadableDuration(t *testing.T) {
|
||||
body := []byte(`{"error":{"message":"You have exhausted your capacity on this model. Your quota will reset after 1h43m56s."}}`)
|
||||
retryAfter, err := parseRetryDelay(body)
|
||||
if err != nil {
|
||||
t.Fatalf("parseRetryDelay() error = %v", err)
|
||||
}
|
||||
if retryAfter == nil {
|
||||
t.Fatal("parseRetryDelay() returned nil")
|
||||
}
|
||||
want := time.Hour + 43*time.Minute + 56*time.Second
|
||||
if *retryAfter != want {
|
||||
t.Fatalf("parseRetryDelay() = %v, want %v", *retryAfter, want)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +192,7 @@ func TestAntigravityExecute_RetriesTransient429ResourceExhausted(t *testing.T) {
|
||||
}
|
||||
|
||||
resp, err := exec.Execute(context.Background(), auth, cliproxyexecutor.Request{
|
||||
Model: "gemini-2.5-flash",
|
||||
Model: "claude-sonnet-4-6",
|
||||
Payload: []byte(`{"request":{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}}`),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FormatAntigravity,
|
||||
@@ -163,32 +208,18 @@ func TestAntigravityExecute_RetriesTransient429ResourceExhausted(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAntigravityExecute_RetriesQuotaExhaustedWithCredits(t *testing.T) {
|
||||
func TestAntigravityExecute_CreditsInjectedWhenConductorRequests(t *testing.T) {
|
||||
resetAntigravityCreditsRetryState()
|
||||
t.Cleanup(resetAntigravityCreditsRetryState)
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
requestBodies []string
|
||||
)
|
||||
|
||||
var requestBodies []string
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
_ = r.Body.Close()
|
||||
|
||||
mu.Lock()
|
||||
requestBodies = append(requestBodies, string(body))
|
||||
reqNum := len(requestBodies)
|
||||
mu.Unlock()
|
||||
|
||||
if reqNum == 1 {
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
_, _ = w.Write([]byte(`{"error":{"status":"RESOURCE_EXHAUSTED","message":"QUOTA_EXHAUSTED"}}`))
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.Contains(string(body), `"enabledCreditTypes":["GOOGLE_ONE_AI"]`) {
|
||||
t.Fatalf("second request body missing enabledCreditTypes: %s", string(body))
|
||||
t.Fatalf("request body missing enabledCreditTypes: %s", string(body))
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"ok"}]}}],"usageMetadata":{"promptTokenCount":1,"candidatesTokenCount":1,"totalTokenCount":2}}}`))
|
||||
@@ -199,7 +230,7 @@ func TestAntigravityExecute_RetriesQuotaExhaustedWithCredits(t *testing.T) {
|
||||
QuotaExceeded: config.QuotaExceeded{AntigravityCredits: true},
|
||||
})
|
||||
auth := &cliproxyauth.Auth{
|
||||
ID: "auth-credits-ok",
|
||||
ID: "auth-credits-conductor",
|
||||
Attributes: map[string]string{
|
||||
"base_url": server.URL,
|
||||
},
|
||||
@@ -210,8 +241,11 @@ func TestAntigravityExecute_RetriesQuotaExhaustedWithCredits(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
resp, err := exec.Execute(context.Background(), auth, cliproxyexecutor.Request{
|
||||
Model: "gemini-2.5-flash",
|
||||
// Simulate conductor setting credits requested flag in context
|
||||
ctx := cliproxyauth.WithAntigravityCredits(context.Background())
|
||||
|
||||
resp, err := exec.Execute(ctx, auth, cliproxyexecutor.Request{
|
||||
Model: "claude-sonnet-4-6",
|
||||
Payload: []byte(`{"request":{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}}`),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FormatAntigravity,
|
||||
@@ -222,227 +256,12 @@ func TestAntigravityExecute_RetriesQuotaExhaustedWithCredits(t *testing.T) {
|
||||
if len(resp.Payload) == 0 {
|
||||
t.Fatal("Execute() returned empty payload")
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if len(requestBodies) != 2 {
|
||||
t.Fatalf("request count = %d, want 2", len(requestBodies))
|
||||
if len(requestBodies) != 1 {
|
||||
t.Fatalf("request count = %d, want 1", len(requestBodies))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAntigravityExecute_SkipsCreditsRetryWhenAlreadyExhausted(t *testing.T) {
|
||||
resetAntigravityCreditsRetryState()
|
||||
t.Cleanup(resetAntigravityCreditsRetryState)
|
||||
|
||||
var requestCount int
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestCount++
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
_, _ = w.Write([]byte(`{"error":{"status":"RESOURCE_EXHAUSTED","message":"QUOTA_EXHAUSTED"}}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
exec := NewAntigravityExecutor(&config.Config{
|
||||
QuotaExceeded: config.QuotaExceeded{AntigravityCredits: true},
|
||||
})
|
||||
auth := &cliproxyauth.Auth{
|
||||
ID: "auth-credits-exhausted",
|
||||
Attributes: map[string]string{
|
||||
"base_url": server.URL,
|
||||
},
|
||||
Metadata: map[string]any{
|
||||
"access_token": "token",
|
||||
"project_id": "project-1",
|
||||
"expired": time.Now().Add(1 * time.Hour).Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
recordAntigravityCreditsFailure(auth, time.Now())
|
||||
|
||||
_, err := exec.Execute(context.Background(), auth, cliproxyexecutor.Request{
|
||||
Model: "gemini-2.5-flash",
|
||||
Payload: []byte(`{"request":{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}}`),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FormatAntigravity,
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("Execute() error = nil, want 429")
|
||||
}
|
||||
sErr, ok := err.(statusErr)
|
||||
if !ok {
|
||||
t.Fatalf("Execute() error type = %T, want statusErr", err)
|
||||
}
|
||||
if got := sErr.StatusCode(); got != http.StatusTooManyRequests {
|
||||
t.Fatalf("Execute() status code = %d, want %d", got, http.StatusTooManyRequests)
|
||||
}
|
||||
if requestCount != 1 {
|
||||
t.Fatalf("request count = %d, want 1", requestCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAntigravityExecute_PrefersCreditsAfterSuccessfulFallback(t *testing.T) {
|
||||
resetAntigravityCreditsRetryState()
|
||||
t.Cleanup(resetAntigravityCreditsRetryState)
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
requestBodies []string
|
||||
)
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
_ = r.Body.Close()
|
||||
|
||||
mu.Lock()
|
||||
requestBodies = append(requestBodies, string(body))
|
||||
reqNum := len(requestBodies)
|
||||
mu.Unlock()
|
||||
|
||||
switch reqNum {
|
||||
case 1:
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
_, _ = w.Write([]byte(`{"error":{"status":"RESOURCE_EXHAUSTED","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"QUOTA_EXHAUSTED"},{"@type":"type.googleapis.com/google.rpc.RetryInfo","retryDelay":"10s"}]}}`))
|
||||
case 2, 3:
|
||||
if !strings.Contains(string(body), `"enabledCreditTypes":["GOOGLE_ONE_AI"]`) {
|
||||
t.Fatalf("request %d body missing enabledCreditTypes: %s", reqNum, string(body))
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"OK"}]}}],"usageMetadata":{"promptTokenCount":1,"candidatesTokenCount":1,"totalTokenCount":2}}}`))
|
||||
default:
|
||||
t.Fatalf("unexpected request count %d", reqNum)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
exec := NewAntigravityExecutor(&config.Config{
|
||||
QuotaExceeded: config.QuotaExceeded{AntigravityCredits: true},
|
||||
})
|
||||
auth := &cliproxyauth.Auth{
|
||||
ID: "auth-prefer-credits",
|
||||
Attributes: map[string]string{
|
||||
"base_url": server.URL,
|
||||
},
|
||||
Metadata: map[string]any{
|
||||
"access_token": "token",
|
||||
"project_id": "project-1",
|
||||
"expired": time.Now().Add(1 * time.Hour).Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
|
||||
request := cliproxyexecutor.Request{
|
||||
Model: "gemini-2.5-flash",
|
||||
Payload: []byte(`{"request":{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}}`),
|
||||
}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatAntigravity}
|
||||
|
||||
if _, err := exec.Execute(context.Background(), auth, request, opts); err != nil {
|
||||
t.Fatalf("first Execute() error = %v", err)
|
||||
}
|
||||
if _, err := exec.Execute(context.Background(), auth, request, opts); err != nil {
|
||||
t.Fatalf("second Execute() error = %v", err)
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if len(requestBodies) != 3 {
|
||||
t.Fatalf("request count = %d, want 3", len(requestBodies))
|
||||
}
|
||||
if strings.Contains(requestBodies[0], `"enabledCreditTypes":["GOOGLE_ONE_AI"]`) {
|
||||
t.Fatalf("first request unexpectedly used credits: %s", requestBodies[0])
|
||||
}
|
||||
if !strings.Contains(requestBodies[1], `"enabledCreditTypes":["GOOGLE_ONE_AI"]`) {
|
||||
t.Fatalf("fallback request missing credits: %s", requestBodies[1])
|
||||
}
|
||||
if !strings.Contains(requestBodies[2], `"enabledCreditTypes":["GOOGLE_ONE_AI"]`) {
|
||||
t.Fatalf("preferred request missing credits: %s", requestBodies[2])
|
||||
}
|
||||
}
|
||||
|
||||
func TestAntigravityExecute_PreservesBaseURLFallbackAfterCreditsRetryFailure(t *testing.T) {
|
||||
resetAntigravityCreditsRetryState()
|
||||
t.Cleanup(resetAntigravityCreditsRetryState)
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
firstCount int
|
||||
secondCount int
|
||||
)
|
||||
|
||||
firstServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
_ = r.Body.Close()
|
||||
|
||||
mu.Lock()
|
||||
firstCount++
|
||||
reqNum := firstCount
|
||||
mu.Unlock()
|
||||
|
||||
switch reqNum {
|
||||
case 1:
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
_, _ = w.Write([]byte(`{"error":{"status":"RESOURCE_EXHAUSTED","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"QUOTA_EXHAUSTED"}]}}`))
|
||||
case 2:
|
||||
if !strings.Contains(string(body), `"enabledCreditTypes":["GOOGLE_ONE_AI"]`) {
|
||||
t.Fatalf("credits retry missing enabledCreditTypes: %s", string(body))
|
||||
}
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
_, _ = w.Write([]byte(`{"error":{"message":"permission denied"}}`))
|
||||
default:
|
||||
t.Fatalf("unexpected first server request count %d", reqNum)
|
||||
}
|
||||
}))
|
||||
defer firstServer.Close()
|
||||
|
||||
secondServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mu.Lock()
|
||||
secondCount++
|
||||
mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"response":{"candidates":[{"content":{"role":"model","parts":[{"text":"ok"}]}}],"usageMetadata":{"promptTokenCount":1,"candidatesTokenCount":1,"totalTokenCount":2}}}`))
|
||||
}))
|
||||
defer secondServer.Close()
|
||||
|
||||
exec := NewAntigravityExecutor(&config.Config{
|
||||
QuotaExceeded: config.QuotaExceeded{AntigravityCredits: true},
|
||||
})
|
||||
auth := &cliproxyauth.Auth{
|
||||
ID: "auth-baseurl-fallback",
|
||||
Attributes: map[string]string{
|
||||
"base_url": firstServer.URL,
|
||||
},
|
||||
Metadata: map[string]any{
|
||||
"access_token": "token",
|
||||
"project_id": "project-1",
|
||||
"expired": time.Now().Add(1 * time.Hour).Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
|
||||
originalOrder := antigravityBaseURLFallbackOrder
|
||||
defer func() { antigravityBaseURLFallbackOrder = originalOrder }()
|
||||
antigravityBaseURLFallbackOrder = func(auth *cliproxyauth.Auth) []string {
|
||||
return []string{firstServer.URL, secondServer.URL}
|
||||
}
|
||||
|
||||
resp, err := exec.Execute(context.Background(), auth, cliproxyexecutor.Request{
|
||||
Model: "gemini-2.5-flash",
|
||||
Payload: []byte(`{"request":{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}}`),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FormatAntigravity,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Execute() error = %v", err)
|
||||
}
|
||||
if len(resp.Payload) == 0 {
|
||||
t.Fatal("Execute() returned empty payload")
|
||||
}
|
||||
if firstCount != 2 {
|
||||
t.Fatalf("first server request count = %d, want 2", firstCount)
|
||||
}
|
||||
if secondCount != 1 {
|
||||
t.Fatalf("second server request count = %d, want 1", secondCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAntigravityExecute_DoesNotDirectInjectCreditsWhenFlagDisabled(t *testing.T) {
|
||||
func TestAntigravityExecute_NoCreditsWithoutConductorFlag(t *testing.T) {
|
||||
resetAntigravityCreditsRetryState()
|
||||
t.Cleanup(resetAntigravityCreditsRetryState)
|
||||
|
||||
@@ -457,10 +276,10 @@ func TestAntigravityExecute_DoesNotDirectInjectCreditsWhenFlagDisabled(t *testin
|
||||
defer server.Close()
|
||||
|
||||
exec := NewAntigravityExecutor(&config.Config{
|
||||
QuotaExceeded: config.QuotaExceeded{AntigravityCredits: false},
|
||||
QuotaExceeded: config.QuotaExceeded{AntigravityCredits: true},
|
||||
})
|
||||
auth := &cliproxyauth.Auth{
|
||||
ID: "auth-flag-disabled",
|
||||
ID: "auth-no-conductor-flag",
|
||||
Attributes: map[string]string{
|
||||
"base_url": server.URL,
|
||||
},
|
||||
@@ -470,10 +289,10 @@ func TestAntigravityExecute_DoesNotDirectInjectCreditsWhenFlagDisabled(t *testin
|
||||
"expired": time.Now().Add(1 * time.Hour).Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
markAntigravityPreferCredits(auth, "gemini-2.5-flash", time.Now(), nil)
|
||||
|
||||
// No conductor credits flag set in context
|
||||
_, err := exec.Execute(context.Background(), auth, cliproxyexecutor.Request{
|
||||
Model: "gemini-2.5-flash",
|
||||
Model: "claude-sonnet-4-6",
|
||||
Payload: []byte(`{"request":{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}}`),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FormatAntigravity,
|
||||
@@ -484,7 +303,156 @@ func TestAntigravityExecute_DoesNotDirectInjectCreditsWhenFlagDisabled(t *testin
|
||||
if len(requestBodies) != 1 {
|
||||
t.Fatalf("request count = %d, want 1", len(requestBodies))
|
||||
}
|
||||
if strings.Contains(requestBodies[0], `"enabledCreditTypes":["GOOGLE_ONE_AI"]`) {
|
||||
t.Fatalf("request unexpectedly used enabledCreditTypes with flag disabled: %s", requestBodies[0])
|
||||
// Should NOT contain credits since conductor didn't request them
|
||||
if strings.Contains(requestBodies[0], `"enabledCreditTypes"`) {
|
||||
t.Fatalf("request should not contain enabledCreditTypes without conductor flag: %s", requestBodies[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestAntigravityAuthHasCredits(t *testing.T) {
|
||||
t.Run("sufficient balance", func(t *testing.T) {
|
||||
resetAntigravityCreditsRetryState()
|
||||
auth := &cliproxyauth.Auth{ID: "test-sufficient"}
|
||||
antigravityCreditsBalanceByAuth.Store("test-sufficient", antigravityCreditsBalance{
|
||||
CreditAmount: 25000,
|
||||
MinCreditAmount: 50,
|
||||
Known: true,
|
||||
})
|
||||
if !antigravityAuthHasCredits(auth) {
|
||||
t.Fatal("antigravityAuthHasCredits() = false, want true")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("insufficient balance", func(t *testing.T) {
|
||||
resetAntigravityCreditsRetryState()
|
||||
auth := &cliproxyauth.Auth{ID: "test-insufficient"}
|
||||
antigravityCreditsBalanceByAuth.Store("test-insufficient", antigravityCreditsBalance{
|
||||
CreditAmount: 30,
|
||||
MinCreditAmount: 50,
|
||||
Known: true,
|
||||
})
|
||||
if antigravityAuthHasCredits(auth) {
|
||||
t.Fatal("antigravityAuthHasCredits() = true, want false")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("no balance stored returns true (optimistic)", func(t *testing.T) {
|
||||
resetAntigravityCreditsRetryState()
|
||||
auth := &cliproxyauth.Auth{ID: "test-no-balance"}
|
||||
if !antigravityAuthHasCredits(auth) {
|
||||
t.Fatal("antigravityAuthHasCredits() = false with no balance stored, want true (optimistic default)")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("nil auth returns false", func(t *testing.T) {
|
||||
if antigravityAuthHasCredits(nil) {
|
||||
t.Fatal("antigravityAuthHasCredits(nil) = true, want false")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("empty ID returns false", func(t *testing.T) {
|
||||
auth := &cliproxyauth.Auth{}
|
||||
if antigravityAuthHasCredits(auth) {
|
||||
t.Fatal("antigravityAuthHasCredits(empty ID) = true, want false")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unknown balance returns false", func(t *testing.T) {
|
||||
resetAntigravityCreditsRetryState()
|
||||
auth := &cliproxyauth.Auth{ID: "test-unknown"}
|
||||
antigravityCreditsBalanceByAuth.Store("test-unknown", antigravityCreditsBalance{
|
||||
Known: false,
|
||||
})
|
||||
if antigravityAuthHasCredits(auth) {
|
||||
t.Fatal("antigravityAuthHasCredits() = true for unknown balance, want false")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type roundTripperFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return f(req)
|
||||
}
|
||||
|
||||
func TestEnsureAccessToken_WarmTokenLoadsCreditsHint(t *testing.T) {
|
||||
resetAntigravityCreditsRetryState()
|
||||
t.Cleanup(resetAntigravityCreditsRetryState)
|
||||
|
||||
exec := NewAntigravityExecutor(&config.Config{
|
||||
QuotaExceeded: config.QuotaExceeded{AntigravityCredits: true},
|
||||
})
|
||||
auth := &cliproxyauth.Auth{
|
||||
ID: "auth-warm-token-credits",
|
||||
Metadata: map[string]any{
|
||||
"access_token": "token",
|
||||
"expired": time.Now().Add(1 * time.Hour).Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
if req.URL.String() != "https://cloudcode-pa.googleapis.com/v1internal:loadCodeAssist" {
|
||||
t.Fatalf("unexpected request url %s", req.URL.String())
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Header: make(http.Header),
|
||||
Body: io.NopCloser(strings.NewReader(`{"paidTier":{"id":"tier-1","availableCredits":[{"creditType":"GOOGLE_ONE_AI","creditAmount":"25000","minimumCreditAmountForUsage":"50"}]}}`)),
|
||||
}, nil
|
||||
}))
|
||||
|
||||
token, updatedAuth, err := exec.ensureAccessToken(ctx, auth)
|
||||
if err != nil {
|
||||
t.Fatalf("ensureAccessToken() error = %v", err)
|
||||
}
|
||||
if token != "token" {
|
||||
t.Fatalf("ensureAccessToken() token = %q, want %q", token, "token")
|
||||
}
|
||||
if updatedAuth != nil {
|
||||
t.Fatalf("ensureAccessToken() updatedAuth = %v, want nil", updatedAuth)
|
||||
}
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for time.Now().Before(deadline) && !cliproxyauth.HasKnownAntigravityCreditsHint(auth.ID) {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
if !cliproxyauth.HasKnownAntigravityCreditsHint(auth.ID) {
|
||||
t.Fatal("expected credits hint to be populated for warm token auth")
|
||||
}
|
||||
hint, ok := cliproxyauth.GetAntigravityCreditsHint(auth.ID)
|
||||
if !ok {
|
||||
t.Fatal("expected credits hint lookup to succeed")
|
||||
}
|
||||
if !hint.Available {
|
||||
t.Fatalf("hint.Available = %v, want true", hint.Available)
|
||||
}
|
||||
if hint.CreditAmount != 25000 || hint.MinCreditAmount != 50 {
|
||||
t.Fatalf("hint amounts = (%v, %v), want (25000, 50)", hint.CreditAmount, hint.MinCreditAmount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMetaFloat(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value any
|
||||
wantVal float64
|
||||
wantOK bool
|
||||
}{
|
||||
{"string", "25000", 25000, true},
|
||||
{"float64", float64(100), 100, true},
|
||||
{"int", int(50), 50, true},
|
||||
{"int64", int64(75), 75, true},
|
||||
{"empty string", "", 0, false},
|
||||
{"invalid string", "abc", 0, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
meta := map[string]any{"key": tt.value}
|
||||
got, ok := parseMetaFloat(meta, "key")
|
||||
if ok != tt.wantOK {
|
||||
t.Fatalf("parseMetaFloat() ok = %v, want %v", ok, tt.wantOK)
|
||||
}
|
||||
if ok && got != tt.wantVal {
|
||||
t.Fatalf("parseMetaFloat() = %f, want %f", got, tt.wantVal)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,69 @@ const (
|
||||
|
||||
var dataTag = []byte("data:")
|
||||
|
||||
// Streamed Codex responses may emit response.output_item.done events while leaving
|
||||
// response.completed.response.output empty. Keep the stream path aligned with the
|
||||
// already-patched non-stream path by reconstructing response.output from those items.
|
||||
func collectCodexOutputItemDone(eventData []byte, outputItemsByIndex map[int64][]byte, outputItemsFallback *[][]byte) {
|
||||
itemResult := gjson.GetBytes(eventData, "item")
|
||||
if !itemResult.Exists() || itemResult.Type != gjson.JSON {
|
||||
return
|
||||
}
|
||||
outputIndexResult := gjson.GetBytes(eventData, "output_index")
|
||||
if outputIndexResult.Exists() {
|
||||
outputItemsByIndex[outputIndexResult.Int()] = []byte(itemResult.Raw)
|
||||
return
|
||||
}
|
||||
*outputItemsFallback = append(*outputItemsFallback, []byte(itemResult.Raw))
|
||||
}
|
||||
|
||||
func patchCodexCompletedOutput(eventData []byte, outputItemsByIndex map[int64][]byte, outputItemsFallback [][]byte) []byte {
|
||||
outputResult := gjson.GetBytes(eventData, "response.output")
|
||||
shouldPatchOutput := (!outputResult.Exists() || !outputResult.IsArray() || len(outputResult.Array()) == 0) && (len(outputItemsByIndex) > 0 || len(outputItemsFallback) > 0)
|
||||
if !shouldPatchOutput {
|
||||
return eventData
|
||||
}
|
||||
|
||||
indexes := make([]int64, 0, len(outputItemsByIndex))
|
||||
for idx := range outputItemsByIndex {
|
||||
indexes = append(indexes, idx)
|
||||
}
|
||||
sort.Slice(indexes, func(i, j int) bool {
|
||||
return indexes[i] < indexes[j]
|
||||
})
|
||||
|
||||
items := make([][]byte, 0, len(outputItemsByIndex)+len(outputItemsFallback))
|
||||
for _, idx := range indexes {
|
||||
items = append(items, outputItemsByIndex[idx])
|
||||
}
|
||||
items = append(items, outputItemsFallback...)
|
||||
|
||||
outputArray := []byte("[]")
|
||||
if len(items) > 0 {
|
||||
var buf bytes.Buffer
|
||||
totalLen := 2
|
||||
for _, item := range items {
|
||||
totalLen += len(item)
|
||||
}
|
||||
if len(items) > 1 {
|
||||
totalLen += len(items) - 1
|
||||
}
|
||||
buf.Grow(totalLen)
|
||||
buf.WriteByte('[')
|
||||
for i, item := range items {
|
||||
if i > 0 {
|
||||
buf.WriteByte(',')
|
||||
}
|
||||
buf.Write(item)
|
||||
}
|
||||
buf.WriteByte(']')
|
||||
outputArray = buf.Bytes()
|
||||
}
|
||||
|
||||
completedDataPatched, _ := sjson.SetRawBytes(eventData, "response.output", outputArray)
|
||||
return completedDataPatched
|
||||
}
|
||||
|
||||
// CodexExecutor is a stateless executor for Codex (OpenAI Responses API entrypoint).
|
||||
// If api_key is unavailable on auth, it falls back to legacy via ClientAdapter.
|
||||
type CodexExecutor struct {
|
||||
@@ -117,6 +180,7 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re
|
||||
body, _ = sjson.DeleteBytes(body, "safety_identifier")
|
||||
body, _ = sjson.DeleteBytes(body, "stream_options")
|
||||
body = normalizeCodexInstructions(body)
|
||||
body = ensureImageGenerationTool(body, baseModel)
|
||||
|
||||
url := strings.TrimSuffix(baseURL, "/") + "/responses"
|
||||
httpReq, err := e.cacheHelper(ctx, from, url, req, body)
|
||||
@@ -263,6 +327,7 @@ func (e *CodexExecutor) executeCompact(ctx context.Context, auth *cliproxyauth.A
|
||||
body, _ = sjson.SetBytes(body, "model", baseModel)
|
||||
body, _ = sjson.DeleteBytes(body, "stream")
|
||||
body = normalizeCodexInstructions(body)
|
||||
body = ensureImageGenerationTool(body, baseModel)
|
||||
|
||||
url := strings.TrimSuffix(baseURL, "/") + "/responses/compact"
|
||||
httpReq, err := e.cacheHelper(ctx, from, url, req, body)
|
||||
@@ -357,6 +422,7 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
|
||||
body, _ = sjson.DeleteBytes(body, "stream_options")
|
||||
body, _ = sjson.SetBytes(body, "model", baseModel)
|
||||
body = normalizeCodexInstructions(body)
|
||||
body = ensureImageGenerationTool(body, baseModel)
|
||||
|
||||
url := strings.TrimSuffix(baseURL, "/") + "/responses"
|
||||
httpReq, err := e.cacheHelper(ctx, from, url, req, body)
|
||||
@@ -414,20 +480,28 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
|
||||
scanner := bufio.NewScanner(httpResp.Body)
|
||||
scanner.Buffer(nil, 52_428_800) // 50MB
|
||||
var param any
|
||||
outputItemsByIndex := make(map[int64][]byte)
|
||||
var outputItemsFallback [][]byte
|
||||
for scanner.Scan() {
|
||||
line := scanner.Bytes()
|
||||
helps.AppendAPIResponseChunk(ctx, e.cfg, line)
|
||||
translatedLine := bytes.Clone(line)
|
||||
|
||||
if bytes.HasPrefix(line, dataTag) {
|
||||
data := bytes.TrimSpace(line[5:])
|
||||
if gjson.GetBytes(data, "type").String() == "response.completed" {
|
||||
switch gjson.GetBytes(data, "type").String() {
|
||||
case "response.output_item.done":
|
||||
collectCodexOutputItemDone(data, outputItemsByIndex, &outputItemsFallback)
|
||||
case "response.completed":
|
||||
if detail, ok := helps.ParseCodexUsage(data); ok {
|
||||
reporter.Publish(ctx, detail)
|
||||
}
|
||||
data = patchCodexCompletedOutput(data, outputItemsByIndex, outputItemsFallback)
|
||||
translatedLine = append([]byte("data: "), data...)
|
||||
}
|
||||
}
|
||||
|
||||
chunks := sdktranslator.TranslateStream(ctx, to, from, req.Model, originalPayload, body, bytes.Clone(line), ¶m)
|
||||
chunks := sdktranslator.TranslateStream(ctx, to, from, req.Model, originalPayload, body, translatedLine, ¶m)
|
||||
for i := range chunks {
|
||||
out <- cliproxyexecutor.StreamChunk{Payload: chunks[i]}
|
||||
}
|
||||
@@ -750,6 +824,28 @@ func normalizeCodexInstructions(body []byte) []byte {
|
||||
return body
|
||||
}
|
||||
|
||||
var imageGenToolJSON = []byte(`{"type":"image_generation","output_format":"png"}`)
|
||||
var imageGenToolArrayJSON = []byte(`[{"type":"image_generation","output_format":"png"}]`)
|
||||
|
||||
func ensureImageGenerationTool(body []byte, baseModel string) []byte {
|
||||
if strings.HasSuffix(baseModel, "spark") {
|
||||
return body
|
||||
}
|
||||
|
||||
tools := gjson.GetBytes(body, "tools")
|
||||
if !tools.Exists() || !tools.IsArray() {
|
||||
body, _ = sjson.SetRawBytes(body, "tools", imageGenToolArrayJSON)
|
||||
return body
|
||||
}
|
||||
for _, t := range tools.Array() {
|
||||
if t.Get("type").String() == "image_generation" {
|
||||
return body
|
||||
}
|
||||
}
|
||||
body, _ = sjson.SetRawBytes(body, "tools.-1", imageGenToolJSON)
|
||||
return body
|
||||
}
|
||||
|
||||
func isCodexModelCapacityError(errorBody []byte) bool {
|
||||
if len(errorBody) == 0 {
|
||||
return false
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func TestEnsureImageGenerationTool_NoTools(t *testing.T) {
|
||||
body := []byte(`{"model":"gpt-5.4","input":"draw a cat"}`)
|
||||
result := ensureImageGenerationTool(body, "gpt-5.4")
|
||||
|
||||
tools := gjson.GetBytes(result, "tools")
|
||||
if !tools.IsArray() {
|
||||
t.Fatalf("expected tools array, got %v", tools.Type)
|
||||
}
|
||||
arr := tools.Array()
|
||||
if len(arr) != 1 {
|
||||
t.Fatalf("expected 1 tool, got %d", len(arr))
|
||||
}
|
||||
if arr[0].Get("type").String() != "image_generation" {
|
||||
t.Fatalf("expected type=image_generation, got %s", arr[0].Get("type").String())
|
||||
}
|
||||
if arr[0].Get("output_format").String() != "png" {
|
||||
t.Fatalf("expected output_format=png, got %s", arr[0].Get("output_format").String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureImageGenerationTool_ExistingToolsWithoutImageGen(t *testing.T) {
|
||||
body := []byte(`{"model":"gpt-5.4","tools":[{"type":"function","name":"get_weather","parameters":{}}]}`)
|
||||
result := ensureImageGenerationTool(body, "gpt-5.4")
|
||||
|
||||
tools := gjson.GetBytes(result, "tools")
|
||||
arr := tools.Array()
|
||||
if len(arr) != 2 {
|
||||
t.Fatalf("expected 2 tools, got %d", len(arr))
|
||||
}
|
||||
if arr[0].Get("type").String() != "function" {
|
||||
t.Fatalf("expected first tool type=function, got %s", arr[0].Get("type").String())
|
||||
}
|
||||
if arr[1].Get("type").String() != "image_generation" {
|
||||
t.Fatalf("expected second tool type=image_generation, got %s", arr[1].Get("type").String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureImageGenerationTool_AlreadyPresent(t *testing.T) {
|
||||
body := []byte(`{"model":"gpt-5.4","tools":[{"type":"image_generation","output_format":"webp"},{"type":"function","name":"f1"}]}`)
|
||||
result := ensureImageGenerationTool(body, "gpt-5.4")
|
||||
|
||||
tools := gjson.GetBytes(result, "tools")
|
||||
arr := tools.Array()
|
||||
if len(arr) != 2 {
|
||||
t.Fatalf("expected 2 tools (no duplicate), got %d", len(arr))
|
||||
}
|
||||
if arr[0].Get("output_format").String() != "webp" {
|
||||
t.Fatalf("expected original output_format=webp preserved, got %s", arr[0].Get("output_format").String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureImageGenerationTool_EmptyToolsArray(t *testing.T) {
|
||||
body := []byte(`{"model":"gpt-5.4","tools":[]}`)
|
||||
result := ensureImageGenerationTool(body, "gpt-5.4")
|
||||
|
||||
tools := gjson.GetBytes(result, "tools")
|
||||
arr := tools.Array()
|
||||
if len(arr) != 1 {
|
||||
t.Fatalf("expected 1 tool, got %d", len(arr))
|
||||
}
|
||||
if arr[0].Get("type").String() != "image_generation" {
|
||||
t.Fatalf("expected type=image_generation, got %s", arr[0].Get("type").String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureImageGenerationTool_WebSearchAndImageGen(t *testing.T) {
|
||||
body := []byte(`{"model":"gpt-5.4","tools":[{"type":"web_search"}]}`)
|
||||
result := ensureImageGenerationTool(body, "gpt-5.4")
|
||||
|
||||
tools := gjson.GetBytes(result, "tools")
|
||||
arr := tools.Array()
|
||||
if len(arr) != 2 {
|
||||
t.Fatalf("expected 2 tools, got %d", len(arr))
|
||||
}
|
||||
if arr[0].Get("type").String() != "web_search" {
|
||||
t.Fatalf("expected first tool type=web_search, got %s", arr[0].Get("type").String())
|
||||
}
|
||||
if arr[1].Get("type").String() != "image_generation" {
|
||||
t.Fatalf("expected second tool type=image_generation, got %s", arr[1].Get("type").String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureImageGenerationTool_GPT53CodexSparkDoesNotInjectTool(t *testing.T) {
|
||||
body := []byte(`{"model":"gpt-5.3-codex-spark","input":"draw a cat"}`)
|
||||
result := ensureImageGenerationTool(body, "gpt-5.3-codex-spark")
|
||||
|
||||
if string(result) != string(body) {
|
||||
t.Fatalf("expected body to be unchanged, got %s", string(result))
|
||||
}
|
||||
if gjson.GetBytes(result, "tools").Exists() {
|
||||
t.Fatalf("expected no tools for gpt-5.3-codex-spark, got %s", gjson.GetBytes(result, "tools").Raw)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -44,3 +45,53 @@ func TestCodexExecutorExecute_EmptyStreamCompletionOutputUsesOutputItemDone(t *t
|
||||
t.Fatalf("choices.0.message.content = %q, want %q; payload=%s", gotContent, "ok", string(resp.Payload))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCodexExecutorExecuteStream_EmptyStreamCompletionOutputUsesOutputItemDone(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
_, _ = w.Write([]byte("data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"output_text\",\"text\":\"ok\"}]},\"output_index\":0}\n"))
|
||||
_, _ = w.Write([]byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_1\",\"object\":\"response\",\"created_at\":1775555723,\"status\":\"completed\",\"model\":\"gpt-5.4-mini-2026-03-17\",\"output\":[],\"usage\":{\"input_tokens\":8,\"output_tokens\":28,\"total_tokens\":36}}}\n\n"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
executor := NewCodexExecutor(&config.Config{})
|
||||
auth := &cliproxyauth.Auth{Attributes: map[string]string{
|
||||
"base_url": server.URL,
|
||||
"api_key": "test",
|
||||
}}
|
||||
|
||||
result, err := executor.ExecuteStream(context.Background(), auth, cliproxyexecutor.Request{
|
||||
Model: "gpt-5.4-mini",
|
||||
Payload: []byte(`{"model":"gpt-5.4-mini","input":"Say ok"}`),
|
||||
}, cliproxyexecutor.Options{
|
||||
SourceFormat: sdktranslator.FromString("openai-response"),
|
||||
Stream: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("ExecuteStream error: %v", err)
|
||||
}
|
||||
|
||||
var completed []byte
|
||||
for chunk := range result.Chunks {
|
||||
if chunk.Err != nil {
|
||||
t.Fatalf("stream chunk error: %v", chunk.Err)
|
||||
}
|
||||
payload := bytes.TrimSpace(chunk.Payload)
|
||||
if !bytes.HasPrefix(payload, []byte("data:")) {
|
||||
continue
|
||||
}
|
||||
data := bytes.TrimSpace(payload[5:])
|
||||
if gjson.GetBytes(data, "type").String() == "response.completed" {
|
||||
completed = append([]byte(nil), data...)
|
||||
}
|
||||
}
|
||||
|
||||
if len(completed) == 0 {
|
||||
t.Fatal("missing response.completed chunk")
|
||||
}
|
||||
|
||||
gotContent := gjson.GetBytes(completed, "response.output.0.content.0.text").String()
|
||||
if gotContent != "ok" {
|
||||
t.Fatalf("response.output[0].content[0].text = %q, want %q; completed=%s", gotContent, "ok", string(completed))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -898,7 +898,14 @@ func parseRetryDelay(errorBody []byte) (*time.Duration, error) {
|
||||
if matches := re.FindStringSubmatch(message); len(matches) > 1 {
|
||||
seconds, err := strconv.Atoi(matches[1])
|
||||
if err == nil {
|
||||
return new(time.Duration(seconds) * time.Second), nil
|
||||
duration := time.Duration(seconds) * time.Second
|
||||
return &duration, nil
|
||||
}
|
||||
}
|
||||
reHuman := regexp.MustCompile(`after\s+((?:\d+h)?(?:\d+m)?(?:\d+s)?)\.?`)
|
||||
if matches := reHuman.FindStringSubmatch(strings.ToLower(message)); len(matches) > 1 {
|
||||
if duration, err := time.ParseDuration(matches[1]); err == nil && duration > 0 {
|
||||
return &duration, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ const (
|
||||
apiRequestKey = "API_REQUEST"
|
||||
apiResponseKey = "API_RESPONSE"
|
||||
apiWebsocketTimelineKey = "API_WEBSOCKET_TIMELINE"
|
||||
creditsUsedKey = "__antigravity_credits_used__"
|
||||
)
|
||||
|
||||
// UpstreamRequestLog captures the outbound upstream request details for logging.
|
||||
@@ -568,3 +569,24 @@ func LogWithRequestID(ctx context.Context) *log.Entry {
|
||||
}
|
||||
return log.WithField("request_id", requestID)
|
||||
}
|
||||
|
||||
// MarkCreditsUsed flags the request as having used AI credits for billing.
|
||||
func MarkCreditsUsed(ctx context.Context) {
|
||||
ginCtx := ginContextFrom(ctx)
|
||||
if ginCtx != nil {
|
||||
ginCtx.Set(creditsUsedKey, true)
|
||||
}
|
||||
}
|
||||
|
||||
// CreditsUsed returns true if the request used AI credits.
|
||||
func CreditsUsed(ctx context.Context) bool {
|
||||
ginCtx := ginContextFrom(ctx)
|
||||
if ginCtx != nil {
|
||||
if val, exists := ginCtx.Get(creditsUsedKey); exists {
|
||||
if b, ok := val.(bool); ok {
|
||||
return b
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -31,6 +31,23 @@ func NewProxyAwareHTTPClient(ctx context.Context, cfg *config.Config, auth *clip
|
||||
proxyURL = strings.TrimSpace(cfg.ProxyURL)
|
||||
}
|
||||
|
||||
var ctxTransport http.RoundTripper
|
||||
if ctx != nil {
|
||||
if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
|
||||
ctxTransport = rt
|
||||
}
|
||||
}
|
||||
|
||||
// Context-bound round trippers are request-scoped and must not be served
|
||||
// from or written into the shared empty-proxy cache.
|
||||
if proxyURL == "" && ctxTransport != nil {
|
||||
httpClient := &http.Client{Transport: ctxTransport}
|
||||
if timeout > 0 {
|
||||
httpClient.Timeout = timeout
|
||||
}
|
||||
return httpClient
|
||||
}
|
||||
|
||||
cacheKey := proxyURL
|
||||
|
||||
httpClientCacheMutex.RLock()
|
||||
@@ -63,8 +80,8 @@ func NewProxyAwareHTTPClient(ctx context.Context, cfg *config.Config, auth *clip
|
||||
log.Debugf("failed to setup proxy from URL: %s, falling back to context transport", proxyURL)
|
||||
}
|
||||
|
||||
if rt, ok := ctx.Value("cliproxy.roundtripper").(http.RoundTripper); ok && rt != nil {
|
||||
httpClient.Transport = rt
|
||||
if ctxTransport != nil {
|
||||
httpClient.Transport = ctxTransport
|
||||
}
|
||||
|
||||
if proxyURL == "" {
|
||||
|
||||
@@ -47,6 +47,14 @@ func applyCustomHeaders(r *http.Request, headers map[string]string) {
|
||||
if k == "" || v == "" {
|
||||
continue
|
||||
}
|
||||
// net/http reads Host from req.Host (not req.Header) when writing
|
||||
// a real request, so we must mirror it there. Some callers pass
|
||||
// synthetic requests (e.g. &http.Request{Header: ...}) and only
|
||||
// consume r.Header afterwards, so keep the value in the header
|
||||
// map too.
|
||||
if http.CanonicalHeaderKey(k) == "Host" {
|
||||
r.Host = v
|
||||
}
|
||||
r.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -796,6 +796,13 @@ func (h *BaseAPIHandler) getRequestDetails(modelName string) (providers []string
|
||||
parsed := thinking.ParseSuffix(resolvedModelName)
|
||||
baseModel := strings.TrimSpace(parsed.ModelName)
|
||||
|
||||
if strings.EqualFold(baseModel, "gpt-image-2") {
|
||||
return nil, "", &interfaces.ErrorMessage{
|
||||
StatusCode: http.StatusServiceUnavailable,
|
||||
Error: fmt.Errorf("model %s is only supported on /v1/images/generations and /v1/images/edits", baseModel),
|
||||
}
|
||||
}
|
||||
|
||||
providers = util.GetProviderName(baseModel)
|
||||
// Fallback: if baseModel has no provider but differs from resolvedModelName,
|
||||
// try using the full model name. This handles edge cases where custom models
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -116,3 +118,22 @@ func TestGetRequestDetails_PreservesSuffix(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRequestDetails_ImageModelReturns503(t *testing.T) {
|
||||
handler := NewBaseAPIHandlers(&sdkconfig.SDKConfig{}, coreauth.NewManager(nil, nil, nil))
|
||||
|
||||
_, _, errMsg := handler.getRequestDetails("gpt-image-2")
|
||||
if errMsg == nil {
|
||||
t.Fatalf("expected error for gpt-image-2, got nil")
|
||||
}
|
||||
if errMsg.StatusCode != http.StatusServiceUnavailable {
|
||||
t.Fatalf("unexpected status code: got %d want %d", errMsg.StatusCode, http.StatusServiceUnavailable)
|
||||
}
|
||||
if errMsg.Error == nil {
|
||||
t.Fatalf("expected error message, got nil")
|
||||
}
|
||||
msg := errMsg.Error.Error()
|
||||
if !strings.Contains(msg, "/v1/images/generations") || !strings.Contains(msg, "/v1/images/edits") {
|
||||
t.Fatalf("unexpected error message: %q", msg)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,896 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultImagesMainModel = "gpt-5.4-mini"
|
||||
defaultImagesToolModel = "gpt-image-2"
|
||||
)
|
||||
|
||||
type imageCallResult struct {
|
||||
Result string
|
||||
RevisedPrompt string
|
||||
OutputFormat string
|
||||
Size string
|
||||
Background string
|
||||
Quality string
|
||||
}
|
||||
|
||||
type sseFrameAccumulator struct {
|
||||
pending []byte
|
||||
}
|
||||
|
||||
func (a *sseFrameAccumulator) AddChunk(chunk []byte) [][]byte {
|
||||
if len(chunk) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if responsesSSENeedsLineBreak(a.pending, chunk) {
|
||||
a.pending = append(a.pending, '\n')
|
||||
}
|
||||
a.pending = append(a.pending, chunk...)
|
||||
|
||||
var frames [][]byte
|
||||
for {
|
||||
frameLen := responsesSSEFrameLen(a.pending)
|
||||
if frameLen == 0 {
|
||||
break
|
||||
}
|
||||
frames = append(frames, a.pending[:frameLen])
|
||||
copy(a.pending, a.pending[frameLen:])
|
||||
a.pending = a.pending[:len(a.pending)-frameLen]
|
||||
}
|
||||
|
||||
if len(bytes.TrimSpace(a.pending)) == 0 {
|
||||
a.pending = a.pending[:0]
|
||||
return frames
|
||||
}
|
||||
if len(a.pending) == 0 || !responsesSSECanEmitWithoutDelimiter(a.pending) {
|
||||
return frames
|
||||
}
|
||||
frames = append(frames, a.pending)
|
||||
a.pending = a.pending[:0]
|
||||
return frames
|
||||
}
|
||||
|
||||
func (a *sseFrameAccumulator) Flush() [][]byte {
|
||||
if len(a.pending) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var frames [][]byte
|
||||
for {
|
||||
frameLen := responsesSSEFrameLen(a.pending)
|
||||
if frameLen == 0 {
|
||||
break
|
||||
}
|
||||
frames = append(frames, a.pending[:frameLen])
|
||||
copy(a.pending, a.pending[frameLen:])
|
||||
a.pending = a.pending[:len(a.pending)-frameLen]
|
||||
}
|
||||
|
||||
if len(bytes.TrimSpace(a.pending)) == 0 {
|
||||
a.pending = nil
|
||||
return frames
|
||||
}
|
||||
if responsesSSECanEmitWithoutDelimiter(a.pending) {
|
||||
frames = append(frames, a.pending)
|
||||
}
|
||||
a.pending = nil
|
||||
return frames
|
||||
}
|
||||
|
||||
func mimeTypeFromOutputFormat(outputFormat string) string {
|
||||
if outputFormat == "" {
|
||||
return "image/png"
|
||||
}
|
||||
if strings.Contains(outputFormat, "/") {
|
||||
return outputFormat
|
||||
}
|
||||
switch strings.ToLower(strings.TrimSpace(outputFormat)) {
|
||||
case "png":
|
||||
return "image/png"
|
||||
case "jpg", "jpeg":
|
||||
return "image/jpeg"
|
||||
case "webp":
|
||||
return "image/webp"
|
||||
default:
|
||||
return "image/png"
|
||||
}
|
||||
}
|
||||
|
||||
func multipartFileToDataURL(fileHeader *multipart.FileHeader) (string, error) {
|
||||
if fileHeader == nil {
|
||||
return "", fmt.Errorf("upload file is nil")
|
||||
}
|
||||
f, err := fileHeader.Open()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("open upload file failed: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
if errClose := f.Close(); errClose != nil {
|
||||
log.Errorf("openai images: close upload file error: %v", errClose)
|
||||
}
|
||||
}()
|
||||
|
||||
data, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read upload file failed: %w", err)
|
||||
}
|
||||
|
||||
mediaType := strings.TrimSpace(fileHeader.Header.Get("Content-Type"))
|
||||
if mediaType == "" {
|
||||
mediaType = http.DetectContentType(data)
|
||||
}
|
||||
|
||||
b64 := base64.StdEncoding.EncodeToString(data)
|
||||
return "data:" + mediaType + ";base64," + b64, nil
|
||||
}
|
||||
|
||||
func parseIntField(raw string, fallback int64) int64 {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return fallback
|
||||
}
|
||||
v, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func parseBoolField(raw string, fallback bool) bool {
|
||||
raw = strings.TrimSpace(strings.ToLower(raw))
|
||||
if raw == "" {
|
||||
return fallback
|
||||
}
|
||||
switch raw {
|
||||
case "1", "true", "yes", "on":
|
||||
return true
|
||||
case "0", "false", "no", "off":
|
||||
return false
|
||||
default:
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
|
||||
func (h *OpenAIAPIHandler) ImagesGenerations(c *gin.Context) {
|
||||
rawJSON, err := c.GetRawData()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: fmt.Sprintf("Invalid request: %v", err),
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
if !json.Valid(rawJSON) {
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: "Invalid request: body must be valid JSON",
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
prompt := strings.TrimSpace(gjson.GetBytes(rawJSON, "prompt").String())
|
||||
if prompt == "" {
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: "Invalid request: prompt is required",
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
imageModel := strings.TrimSpace(gjson.GetBytes(rawJSON, "model").String())
|
||||
if imageModel == "" {
|
||||
imageModel = defaultImagesToolModel
|
||||
}
|
||||
responseFormat := strings.TrimSpace(gjson.GetBytes(rawJSON, "response_format").String())
|
||||
if responseFormat == "" {
|
||||
responseFormat = "b64_json"
|
||||
}
|
||||
stream := gjson.GetBytes(rawJSON, "stream").Bool()
|
||||
|
||||
tool := []byte(`{"type":"image_generation","action":"generate"}`)
|
||||
tool, _ = sjson.SetBytes(tool, "model", imageModel)
|
||||
|
||||
if v := strings.TrimSpace(gjson.GetBytes(rawJSON, "size").String()); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "size", v)
|
||||
}
|
||||
if v := strings.TrimSpace(gjson.GetBytes(rawJSON, "quality").String()); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "quality", v)
|
||||
}
|
||||
if v := strings.TrimSpace(gjson.GetBytes(rawJSON, "background").String()); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "background", v)
|
||||
}
|
||||
if v := strings.TrimSpace(gjson.GetBytes(rawJSON, "output_format").String()); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "output_format", v)
|
||||
}
|
||||
if v := gjson.GetBytes(rawJSON, "output_compression"); v.Exists() {
|
||||
if v.Type == gjson.Number {
|
||||
tool, _ = sjson.SetBytes(tool, "output_compression", v.Int())
|
||||
}
|
||||
}
|
||||
if v := gjson.GetBytes(rawJSON, "partial_images"); v.Exists() {
|
||||
if v.Type == gjson.Number {
|
||||
tool, _ = sjson.SetBytes(tool, "partial_images", v.Int())
|
||||
}
|
||||
}
|
||||
if v := strings.TrimSpace(gjson.GetBytes(rawJSON, "moderation").String()); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "moderation", v)
|
||||
}
|
||||
|
||||
responsesReq := buildImagesResponsesRequest(prompt, nil, tool)
|
||||
if stream {
|
||||
h.streamImagesFromResponses(c, responsesReq, responseFormat, "image_generation")
|
||||
return
|
||||
}
|
||||
h.collectImagesFromResponses(c, responsesReq, responseFormat)
|
||||
}
|
||||
|
||||
func (h *OpenAIAPIHandler) ImagesEdits(c *gin.Context) {
|
||||
contentType := strings.ToLower(strings.TrimSpace(c.GetHeader("Content-Type")))
|
||||
if strings.HasPrefix(contentType, "application/json") {
|
||||
h.imagesEditsFromJSON(c)
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(contentType, "multipart/form-data") || contentType == "" {
|
||||
h.imagesEditsFromMultipart(c)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: fmt.Sprintf("Invalid request: unsupported Content-Type %q", contentType),
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (h *OpenAIAPIHandler) imagesEditsFromMultipart(c *gin.Context) {
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: fmt.Sprintf("Invalid request: %v", err),
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
prompt := strings.TrimSpace(c.PostForm("prompt"))
|
||||
if prompt == "" {
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: "Invalid request: prompt is required",
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var imageFiles []*multipart.FileHeader
|
||||
if files := form.File["image[]"]; len(files) > 0 {
|
||||
imageFiles = files
|
||||
} else if files := form.File["image"]; len(files) > 0 {
|
||||
imageFiles = files
|
||||
}
|
||||
if len(imageFiles) == 0 {
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: "Invalid request: image is required",
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
images := make([]string, 0, len(imageFiles))
|
||||
for _, fh := range imageFiles {
|
||||
dataURL, err := multipartFileToDataURL(fh)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: fmt.Sprintf("Invalid request: %v", err),
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
images = append(images, dataURL)
|
||||
}
|
||||
|
||||
var maskDataURL *string
|
||||
if maskFiles := form.File["mask"]; len(maskFiles) > 0 && maskFiles[0] != nil {
|
||||
dataURL, err := multipartFileToDataURL(maskFiles[0])
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: fmt.Sprintf("Invalid request: %v", err),
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
maskDataURL = &dataURL
|
||||
}
|
||||
|
||||
imageModel := strings.TrimSpace(c.PostForm("model"))
|
||||
if imageModel == "" {
|
||||
imageModel = defaultImagesToolModel
|
||||
}
|
||||
responseFormat := strings.TrimSpace(c.PostForm("response_format"))
|
||||
if responseFormat == "" {
|
||||
responseFormat = "b64_json"
|
||||
}
|
||||
stream := parseBoolField(c.PostForm("stream"), false)
|
||||
|
||||
tool := []byte(`{"type":"image_generation","action":"edit"}`)
|
||||
tool, _ = sjson.SetBytes(tool, "model", imageModel)
|
||||
|
||||
if v := strings.TrimSpace(c.PostForm("size")); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "size", v)
|
||||
}
|
||||
if v := strings.TrimSpace(c.PostForm("quality")); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "quality", v)
|
||||
}
|
||||
if v := strings.TrimSpace(c.PostForm("background")); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "background", v)
|
||||
}
|
||||
if v := strings.TrimSpace(c.PostForm("output_format")); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "output_format", v)
|
||||
}
|
||||
if v := strings.TrimSpace(c.PostForm("input_fidelity")); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "input_fidelity", v)
|
||||
}
|
||||
if v := strings.TrimSpace(c.PostForm("moderation")); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "moderation", v)
|
||||
}
|
||||
|
||||
if v := strings.TrimSpace(c.PostForm("output_compression")); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "output_compression", parseIntField(v, 0))
|
||||
}
|
||||
if v := strings.TrimSpace(c.PostForm("partial_images")); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "partial_images", parseIntField(v, 0))
|
||||
}
|
||||
|
||||
if maskDataURL != nil && strings.TrimSpace(*maskDataURL) != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "input_image_mask.image_url", strings.TrimSpace(*maskDataURL))
|
||||
}
|
||||
|
||||
responsesReq := buildImagesResponsesRequest(prompt, images, tool)
|
||||
if stream {
|
||||
h.streamImagesFromResponses(c, responsesReq, responseFormat, "image_edit")
|
||||
return
|
||||
}
|
||||
h.collectImagesFromResponses(c, responsesReq, responseFormat)
|
||||
}
|
||||
|
||||
func (h *OpenAIAPIHandler) imagesEditsFromJSON(c *gin.Context) {
|
||||
rawJSON, err := c.GetRawData()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: fmt.Sprintf("Invalid request: %v", err),
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
if !json.Valid(rawJSON) {
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: "Invalid request: body must be valid JSON",
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
prompt := strings.TrimSpace(gjson.GetBytes(rawJSON, "prompt").String())
|
||||
if prompt == "" {
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: "Invalid request: prompt is required",
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var images []string
|
||||
imagesResult := gjson.GetBytes(rawJSON, "images")
|
||||
if imagesResult.IsArray() {
|
||||
for _, img := range imagesResult.Array() {
|
||||
url := strings.TrimSpace(img.Get("image_url").String())
|
||||
if url == "" {
|
||||
continue
|
||||
}
|
||||
images = append(images, url)
|
||||
}
|
||||
}
|
||||
if len(images) == 0 {
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: "Invalid request: images[].image_url is required (file_id is not supported)",
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var maskDataURL *string
|
||||
if mask := gjson.GetBytes(rawJSON, "mask.image_url"); mask.Exists() {
|
||||
url := strings.TrimSpace(mask.String())
|
||||
if url != "" {
|
||||
maskDataURL = &url
|
||||
}
|
||||
} else if mask := gjson.GetBytes(rawJSON, "mask.file_id"); mask.Exists() {
|
||||
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: "Invalid request: mask.file_id is not supported (use mask.image_url instead)",
|
||||
Type: "invalid_request_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
imageModel := strings.TrimSpace(gjson.GetBytes(rawJSON, "model").String())
|
||||
if imageModel == "" {
|
||||
imageModel = defaultImagesToolModel
|
||||
}
|
||||
responseFormat := strings.TrimSpace(gjson.GetBytes(rawJSON, "response_format").String())
|
||||
if responseFormat == "" {
|
||||
responseFormat = "b64_json"
|
||||
}
|
||||
stream := gjson.GetBytes(rawJSON, "stream").Bool()
|
||||
|
||||
tool := []byte(`{"type":"image_generation","action":"edit"}`)
|
||||
tool, _ = sjson.SetBytes(tool, "model", imageModel)
|
||||
|
||||
for _, field := range []string{"size", "quality", "background", "output_format", "input_fidelity", "moderation"} {
|
||||
if v := strings.TrimSpace(gjson.GetBytes(rawJSON, field).String()); v != "" {
|
||||
tool, _ = sjson.SetBytes(tool, field, v)
|
||||
}
|
||||
}
|
||||
|
||||
for _, field := range []string{"output_compression", "partial_images"} {
|
||||
if v := gjson.GetBytes(rawJSON, field); v.Exists() && v.Type == gjson.Number {
|
||||
tool, _ = sjson.SetBytes(tool, field, v.Int())
|
||||
}
|
||||
}
|
||||
|
||||
if maskDataURL != nil && strings.TrimSpace(*maskDataURL) != "" {
|
||||
tool, _ = sjson.SetBytes(tool, "input_image_mask.image_url", strings.TrimSpace(*maskDataURL))
|
||||
}
|
||||
|
||||
responsesReq := buildImagesResponsesRequest(prompt, images, tool)
|
||||
if stream {
|
||||
h.streamImagesFromResponses(c, responsesReq, responseFormat, "image_edit")
|
||||
return
|
||||
}
|
||||
h.collectImagesFromResponses(c, responsesReq, responseFormat)
|
||||
}
|
||||
|
||||
func buildImagesResponsesRequest(prompt string, images []string, toolJSON []byte) []byte {
|
||||
req := []byte(`{"instructions":"","stream":true,"reasoning":{"effort":"medium","summary":"auto"},"parallel_tool_calls":true,"include":["reasoning.encrypted_content"],"model":"","store":false,"tool_choice":{"type":"image_generation"}}`)
|
||||
req, _ = sjson.SetBytes(req, "model", defaultImagesMainModel)
|
||||
|
||||
input := []byte(`[{"type":"message","role":"user","content":[{"type":"input_text","text":""}]}]`)
|
||||
input, _ = sjson.SetBytes(input, "0.content.0.text", prompt)
|
||||
contentIndex := 1
|
||||
for _, img := range images {
|
||||
if strings.TrimSpace(img) == "" {
|
||||
continue
|
||||
}
|
||||
part := []byte(`{"type":"input_image","image_url":""}`)
|
||||
part, _ = sjson.SetBytes(part, "image_url", img)
|
||||
path := fmt.Sprintf("0.content.%d", contentIndex)
|
||||
input, _ = sjson.SetRawBytes(input, path, part)
|
||||
contentIndex++
|
||||
}
|
||||
req, _ = sjson.SetRawBytes(req, "input", input)
|
||||
|
||||
req, _ = sjson.SetRawBytes(req, "tools", []byte(`[]`))
|
||||
if len(toolJSON) > 0 && json.Valid(toolJSON) {
|
||||
req, _ = sjson.SetRawBytes(req, "tools.-1", toolJSON)
|
||||
}
|
||||
return req
|
||||
}
|
||||
|
||||
func (h *OpenAIAPIHandler) collectImagesFromResponses(c *gin.Context, responsesReq []byte, responseFormat string) {
|
||||
c.Header("Content-Type", "application/json")
|
||||
|
||||
cliCtx, cliCancel := h.GetContextWithCancel(h, c, context.Background())
|
||||
stopKeepAlive := h.StartNonStreamingKeepAlive(c, cliCtx)
|
||||
|
||||
dataChan, upstreamHeaders, errChan := h.ExecuteStreamWithAuthManager(cliCtx, "openai-response", defaultImagesMainModel, responsesReq, "")
|
||||
|
||||
out, errMsg := collectImagesFromResponsesStream(cliCtx, dataChan, errChan, responseFormat)
|
||||
stopKeepAlive()
|
||||
if errMsg != nil {
|
||||
h.WriteErrorResponse(c, errMsg)
|
||||
if errMsg.Error != nil {
|
||||
cliCancel(errMsg.Error)
|
||||
} else {
|
||||
cliCancel(nil)
|
||||
}
|
||||
return
|
||||
}
|
||||
handlers.WriteUpstreamHeaders(c.Writer.Header(), upstreamHeaders)
|
||||
_, _ = c.Writer.Write(out)
|
||||
cliCancel()
|
||||
}
|
||||
|
||||
func collectImagesFromResponsesStream(ctx context.Context, data <-chan []byte, errs <-chan *interfaces.ErrorMessage, responseFormat string) ([]byte, *interfaces.ErrorMessage) {
|
||||
acc := &sseFrameAccumulator{}
|
||||
|
||||
processFrame := func(frame []byte) ([]byte, bool, *interfaces.ErrorMessage) {
|
||||
for _, line := range bytes.Split(frame, []byte("\n")) {
|
||||
trimmed := bytes.TrimSpace(bytes.TrimRight(line, "\r"))
|
||||
if len(trimmed) == 0 {
|
||||
continue
|
||||
}
|
||||
if !bytes.HasPrefix(trimmed, []byte("data:")) {
|
||||
continue
|
||||
}
|
||||
payload := bytes.TrimSpace(trimmed[len("data:"):])
|
||||
if len(payload) == 0 || bytes.Equal(payload, []byte("[DONE]")) {
|
||||
continue
|
||||
}
|
||||
if !json.Valid(payload) {
|
||||
return nil, false, &interfaces.ErrorMessage{StatusCode: http.StatusBadGateway, Error: fmt.Errorf("invalid SSE data JSON")}
|
||||
}
|
||||
|
||||
if gjson.GetBytes(payload, "type").String() != "response.completed" {
|
||||
continue
|
||||
}
|
||||
|
||||
results, createdAt, usageRaw, firstMeta, err := extractImagesFromResponsesCompleted(payload)
|
||||
if err != nil {
|
||||
return nil, false, &interfaces.ErrorMessage{StatusCode: http.StatusBadGateway, Error: err}
|
||||
}
|
||||
if len(results) == 0 {
|
||||
return nil, false, &interfaces.ErrorMessage{StatusCode: http.StatusBadGateway, Error: fmt.Errorf("upstream did not return image output")}
|
||||
}
|
||||
out, err := buildImagesAPIResponse(results, createdAt, usageRaw, firstMeta, responseFormat)
|
||||
if err != nil {
|
||||
return nil, false, &interfaces.ErrorMessage{StatusCode: http.StatusInternalServerError, Error: err}
|
||||
}
|
||||
return out, true, nil
|
||||
}
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, &interfaces.ErrorMessage{StatusCode: http.StatusRequestTimeout, Error: ctx.Err()}
|
||||
case errMsg, ok := <-errs:
|
||||
if ok && errMsg != nil {
|
||||
return nil, errMsg
|
||||
}
|
||||
errs = nil
|
||||
case chunk, ok := <-data:
|
||||
if !ok {
|
||||
for _, frame := range acc.Flush() {
|
||||
if out, done, errMsg := processFrame(frame); errMsg != nil {
|
||||
return nil, errMsg
|
||||
} else if done {
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
return nil, &interfaces.ErrorMessage{StatusCode: http.StatusBadGateway, Error: fmt.Errorf("stream disconnected before completion")}
|
||||
}
|
||||
for _, frame := range acc.AddChunk(chunk) {
|
||||
if out, done, errMsg := processFrame(frame); errMsg != nil {
|
||||
return nil, errMsg
|
||||
} else if done {
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func extractImagesFromResponsesCompleted(payload []byte) (results []imageCallResult, createdAt int64, usageRaw []byte, firstMeta imageCallResult, err error) {
|
||||
if gjson.GetBytes(payload, "type").String() != "response.completed" {
|
||||
return nil, 0, nil, imageCallResult{}, fmt.Errorf("unexpected event type")
|
||||
}
|
||||
|
||||
createdAt = gjson.GetBytes(payload, "response.created_at").Int()
|
||||
if createdAt <= 0 {
|
||||
createdAt = time.Now().Unix()
|
||||
}
|
||||
|
||||
output := gjson.GetBytes(payload, "response.output")
|
||||
if output.IsArray() {
|
||||
for _, item := range output.Array() {
|
||||
if item.Get("type").String() != "image_generation_call" {
|
||||
continue
|
||||
}
|
||||
res := strings.TrimSpace(item.Get("result").String())
|
||||
if res == "" {
|
||||
continue
|
||||
}
|
||||
entry := imageCallResult{
|
||||
Result: res,
|
||||
RevisedPrompt: strings.TrimSpace(item.Get("revised_prompt").String()),
|
||||
OutputFormat: strings.TrimSpace(item.Get("output_format").String()),
|
||||
Size: strings.TrimSpace(item.Get("size").String()),
|
||||
Background: strings.TrimSpace(item.Get("background").String()),
|
||||
Quality: strings.TrimSpace(item.Get("quality").String()),
|
||||
}
|
||||
if len(results) == 0 {
|
||||
firstMeta = entry
|
||||
}
|
||||
results = append(results, entry)
|
||||
}
|
||||
}
|
||||
|
||||
if usage := gjson.GetBytes(payload, "response.tool_usage.image_gen"); usage.Exists() && usage.IsObject() {
|
||||
usageRaw = []byte(usage.Raw)
|
||||
}
|
||||
|
||||
return results, createdAt, usageRaw, firstMeta, nil
|
||||
}
|
||||
|
||||
func buildImagesAPIResponse(results []imageCallResult, createdAt int64, usageRaw []byte, firstMeta imageCallResult, responseFormat string) ([]byte, error) {
|
||||
out := []byte(`{"created":0,"data":[]}`)
|
||||
out, _ = sjson.SetBytes(out, "created", createdAt)
|
||||
|
||||
responseFormat = strings.ToLower(strings.TrimSpace(responseFormat))
|
||||
if responseFormat == "" {
|
||||
responseFormat = "b64_json"
|
||||
}
|
||||
|
||||
for _, img := range results {
|
||||
item := []byte(`{}`)
|
||||
if responseFormat == "url" {
|
||||
mt := mimeTypeFromOutputFormat(img.OutputFormat)
|
||||
item, _ = sjson.SetBytes(item, "url", "data:"+mt+";base64,"+img.Result)
|
||||
} else {
|
||||
item, _ = sjson.SetBytes(item, "b64_json", img.Result)
|
||||
}
|
||||
if img.RevisedPrompt != "" {
|
||||
item, _ = sjson.SetBytes(item, "revised_prompt", img.RevisedPrompt)
|
||||
}
|
||||
out, _ = sjson.SetRawBytes(out, "data.-1", item)
|
||||
}
|
||||
|
||||
if firstMeta.Background != "" {
|
||||
out, _ = sjson.SetBytes(out, "background", firstMeta.Background)
|
||||
}
|
||||
if firstMeta.OutputFormat != "" {
|
||||
out, _ = sjson.SetBytes(out, "output_format", firstMeta.OutputFormat)
|
||||
}
|
||||
if firstMeta.Quality != "" {
|
||||
out, _ = sjson.SetBytes(out, "quality", firstMeta.Quality)
|
||||
}
|
||||
if firstMeta.Size != "" {
|
||||
out, _ = sjson.SetBytes(out, "size", firstMeta.Size)
|
||||
}
|
||||
|
||||
if len(usageRaw) > 0 && json.Valid(usageRaw) {
|
||||
out, _ = sjson.SetRawBytes(out, "usage", usageRaw)
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (h *OpenAIAPIHandler) streamImagesFromResponses(c *gin.Context, responsesReq []byte, responseFormat string, streamPrefix string) {
|
||||
flusher, ok := c.Writer.(http.Flusher)
|
||||
if !ok {
|
||||
c.JSON(http.StatusInternalServerError, handlers.ErrorResponse{
|
||||
Error: handlers.ErrorDetail{
|
||||
Message: "Streaming not supported",
|
||||
Type: "server_error",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
cliCtx, cliCancel := h.GetContextWithCancel(h, c, context.Background())
|
||||
dataChan, upstreamHeaders, errChan := h.ExecuteStreamWithAuthManager(cliCtx, "openai-response", defaultImagesMainModel, responsesReq, "")
|
||||
|
||||
setSSEHeaders := func() {
|
||||
c.Header("Content-Type", "text/event-stream")
|
||||
c.Header("Cache-Control", "no-cache")
|
||||
c.Header("Connection", "keep-alive")
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
}
|
||||
|
||||
writeEvent := func(eventName string, dataJSON []byte) {
|
||||
if strings.TrimSpace(eventName) != "" {
|
||||
_, _ = fmt.Fprintf(c.Writer, "event: %s\n", eventName)
|
||||
}
|
||||
_, _ = fmt.Fprintf(c.Writer, "data: %s\n\n", string(dataJSON))
|
||||
flusher.Flush()
|
||||
}
|
||||
|
||||
// Peek for first chunk/error so we can still return a JSON error body.
|
||||
for {
|
||||
select {
|
||||
case <-c.Request.Context().Done():
|
||||
cliCancel(c.Request.Context().Err())
|
||||
return
|
||||
case errMsg, ok := <-errChan:
|
||||
if !ok {
|
||||
errChan = nil
|
||||
continue
|
||||
}
|
||||
h.WriteErrorResponse(c, errMsg)
|
||||
if errMsg != nil {
|
||||
cliCancel(errMsg.Error)
|
||||
} else {
|
||||
cliCancel(nil)
|
||||
}
|
||||
return
|
||||
case chunk, ok := <-dataChan:
|
||||
if !ok {
|
||||
setSSEHeaders()
|
||||
handlers.WriteUpstreamHeaders(c.Writer.Header(), upstreamHeaders)
|
||||
_, _ = c.Writer.Write([]byte("\n"))
|
||||
flusher.Flush()
|
||||
cliCancel(nil)
|
||||
return
|
||||
}
|
||||
|
||||
setSSEHeaders()
|
||||
handlers.WriteUpstreamHeaders(c.Writer.Header(), upstreamHeaders)
|
||||
|
||||
h.forwardImagesStream(cliCtx, c, flusher, func(err error) { cliCancel(err) }, dataChan, errChan, chunk, responseFormat, streamPrefix, writeEvent)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *OpenAIAPIHandler) forwardImagesStream(ctx context.Context, c *gin.Context, flusher http.Flusher, cancel func(error), data <-chan []byte, errs <-chan *interfaces.ErrorMessage, firstChunk []byte, responseFormat string, streamPrefix string, writeEvent func(string, []byte)) {
|
||||
acc := &sseFrameAccumulator{}
|
||||
|
||||
responseFormat = strings.ToLower(strings.TrimSpace(responseFormat))
|
||||
if responseFormat == "" {
|
||||
responseFormat = "b64_json"
|
||||
}
|
||||
|
||||
emitError := func(errMsg *interfaces.ErrorMessage) {
|
||||
if errMsg == nil {
|
||||
return
|
||||
}
|
||||
status := http.StatusInternalServerError
|
||||
if errMsg.StatusCode > 0 {
|
||||
status = errMsg.StatusCode
|
||||
}
|
||||
errText := http.StatusText(status)
|
||||
if errMsg.Error != nil && strings.TrimSpace(errMsg.Error.Error()) != "" {
|
||||
errText = errMsg.Error.Error()
|
||||
}
|
||||
body := handlers.BuildErrorResponseBody(status, errText)
|
||||
writeEvent("error", body)
|
||||
}
|
||||
|
||||
processFrame := func(frame []byte) (done bool) {
|
||||
for _, line := range bytes.Split(frame, []byte("\n")) {
|
||||
trimmed := bytes.TrimSpace(bytes.TrimRight(line, "\r"))
|
||||
if len(trimmed) == 0 || !bytes.HasPrefix(trimmed, []byte("data:")) {
|
||||
continue
|
||||
}
|
||||
payload := bytes.TrimSpace(trimmed[len("data:"):])
|
||||
if len(payload) == 0 || bytes.Equal(payload, []byte("[DONE]")) || !json.Valid(payload) {
|
||||
continue
|
||||
}
|
||||
|
||||
switch gjson.GetBytes(payload, "type").String() {
|
||||
case "response.image_generation_call.partial_image":
|
||||
b64 := strings.TrimSpace(gjson.GetBytes(payload, "partial_image_b64").String())
|
||||
if b64 == "" {
|
||||
continue
|
||||
}
|
||||
outputFormat := strings.TrimSpace(gjson.GetBytes(payload, "output_format").String())
|
||||
index := gjson.GetBytes(payload, "partial_image_index").Int()
|
||||
eventName := streamPrefix + ".partial_image"
|
||||
data := []byte(`{"type":"","partial_image_index":0}`)
|
||||
data, _ = sjson.SetBytes(data, "type", eventName)
|
||||
data, _ = sjson.SetBytes(data, "partial_image_index", index)
|
||||
if responseFormat == "url" {
|
||||
mt := mimeTypeFromOutputFormat(outputFormat)
|
||||
data, _ = sjson.SetBytes(data, "url", "data:"+mt+";base64,"+b64)
|
||||
} else {
|
||||
data, _ = sjson.SetBytes(data, "b64_json", b64)
|
||||
}
|
||||
writeEvent(eventName, data)
|
||||
case "response.completed":
|
||||
results, _, usageRaw, _, err := extractImagesFromResponsesCompleted(payload)
|
||||
if err != nil {
|
||||
emitError(&interfaces.ErrorMessage{StatusCode: http.StatusBadGateway, Error: err})
|
||||
return true
|
||||
}
|
||||
if len(results) == 0 {
|
||||
emitError(&interfaces.ErrorMessage{StatusCode: http.StatusBadGateway, Error: fmt.Errorf("upstream did not return image output")})
|
||||
return true
|
||||
}
|
||||
eventName := streamPrefix + ".completed"
|
||||
for _, img := range results {
|
||||
data := []byte(`{"type":""}`)
|
||||
data, _ = sjson.SetBytes(data, "type", eventName)
|
||||
if responseFormat == "url" {
|
||||
mt := mimeTypeFromOutputFormat(img.OutputFormat)
|
||||
data, _ = sjson.SetBytes(data, "url", "data:"+mt+";base64,"+img.Result)
|
||||
} else {
|
||||
data, _ = sjson.SetBytes(data, "b64_json", img.Result)
|
||||
}
|
||||
if len(usageRaw) > 0 && json.Valid(usageRaw) {
|
||||
data, _ = sjson.SetRawBytes(data, "usage", usageRaw)
|
||||
}
|
||||
writeEvent(eventName, data)
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
for _, frame := range acc.AddChunk(firstChunk) {
|
||||
if processFrame(frame) {
|
||||
cancel(nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-c.Request.Context().Done():
|
||||
cancel(c.Request.Context().Err())
|
||||
return
|
||||
case errMsg, ok := <-errs:
|
||||
if ok && errMsg != nil {
|
||||
emitError(errMsg)
|
||||
cancel(errMsg.Error)
|
||||
return
|
||||
}
|
||||
errs = nil
|
||||
case chunk, ok := <-data:
|
||||
if !ok {
|
||||
for _, frame := range acc.Flush() {
|
||||
if processFrame(frame) {
|
||||
cancel(nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
cancel(nil)
|
||||
return
|
||||
}
|
||||
for _, frame := range acc.AddChunk(chunk) {
|
||||
if processFrame(frame) {
|
||||
cancel(nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type antigravityUseCreditsContextKey struct{}
|
||||
|
||||
// WithAntigravityCredits returns a child context that signals the executor to
|
||||
// inject enabledCreditTypes into the request payload.
|
||||
func WithAntigravityCredits(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, antigravityUseCreditsContextKey{}, true)
|
||||
}
|
||||
|
||||
// AntigravityCreditsRequested reports whether the context carries the credits flag.
|
||||
func AntigravityCreditsRequested(ctx context.Context) bool {
|
||||
if ctx == nil {
|
||||
return false
|
||||
}
|
||||
v, _ := ctx.Value(antigravityUseCreditsContextKey{}).(bool)
|
||||
return v
|
||||
}
|
||||
|
||||
// AntigravityCreditsHint stores the latest known AI credits state for one auth.
|
||||
type AntigravityCreditsHint struct {
|
||||
Known bool
|
||||
Available bool
|
||||
CreditAmount float64
|
||||
MinCreditAmount float64
|
||||
PaidTierID string
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
var antigravityCreditsHintByAuth sync.Map
|
||||
|
||||
// SetAntigravityCreditsHint updates the latest known AI credits state for an auth.
|
||||
func SetAntigravityCreditsHint(authID string, hint AntigravityCreditsHint) {
|
||||
authID = strings.TrimSpace(authID)
|
||||
if authID == "" {
|
||||
return
|
||||
}
|
||||
if hint.UpdatedAt.IsZero() {
|
||||
hint.UpdatedAt = time.Now()
|
||||
}
|
||||
antigravityCreditsHintByAuth.Store(authID, hint)
|
||||
}
|
||||
|
||||
// GetAntigravityCreditsHint returns the latest known AI credits state for an auth.
|
||||
func GetAntigravityCreditsHint(authID string) (AntigravityCreditsHint, bool) {
|
||||
authID = strings.TrimSpace(authID)
|
||||
if authID == "" {
|
||||
return AntigravityCreditsHint{}, false
|
||||
}
|
||||
value, ok := antigravityCreditsHintByAuth.Load(authID)
|
||||
if !ok {
|
||||
return AntigravityCreditsHint{}, false
|
||||
}
|
||||
hint, ok := value.(AntigravityCreditsHint)
|
||||
if !ok {
|
||||
antigravityCreditsHintByAuth.Delete(authID)
|
||||
return AntigravityCreditsHint{}, false
|
||||
}
|
||||
return hint, true
|
||||
}
|
||||
|
||||
// HasKnownAntigravityCreditsHint reports whether credits state has been discovered for an auth.
|
||||
func HasKnownAntigravityCreditsHint(authID string) bool {
|
||||
hint, ok := GetAntigravityCreditsHint(authID)
|
||||
return ok && hint.Known
|
||||
}
|
||||
|
||||
func antigravityCreditsAvailableForModel(auth *Auth, model string) bool {
|
||||
if auth == nil {
|
||||
return false
|
||||
}
|
||||
if !strings.EqualFold(strings.TrimSpace(auth.Provider), "antigravity") {
|
||||
return false
|
||||
}
|
||||
if !strings.Contains(strings.ToLower(strings.TrimSpace(model)), "claude") {
|
||||
return false
|
||||
}
|
||||
hint, ok := GetAntigravityCreditsHint(auth.ID)
|
||||
if !ok || !hint.Known {
|
||||
return false
|
||||
}
|
||||
return hint.Available
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestIsAuthBlockedForModel_ClaudeWithCreditsStillBlockedDuringCooldown(t *testing.T) {
|
||||
auth := &Auth{
|
||||
ID: "ag-1",
|
||||
Provider: "antigravity",
|
||||
ModelStates: map[string]*ModelState{
|
||||
"claude-sonnet-4-6": {
|
||||
Unavailable: true,
|
||||
NextRetryAfter: time.Now().Add(10 * time.Minute),
|
||||
Quota: QuotaState{
|
||||
Exceeded: true,
|
||||
NextRecoverAt: time.Now().Add(10 * time.Minute),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
SetAntigravityCreditsHint(auth.ID, AntigravityCreditsHint{
|
||||
Known: true,
|
||||
Available: true,
|
||||
UpdatedAt: time.Now(),
|
||||
})
|
||||
|
||||
blocked, reason, _ := isAuthBlockedForModel(auth, "claude-sonnet-4-6", time.Now())
|
||||
if !blocked || reason != blockReasonCooldown {
|
||||
t.Fatalf("expected auth to be blocked during cooldown even with credits, got blocked=%v reason=%v", blocked, reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAuthBlockedForModel_KeepsGeminiBlockedWithoutCreditsBypass(t *testing.T) {
|
||||
auth := &Auth{
|
||||
ID: "ag-2",
|
||||
Provider: "antigravity",
|
||||
ModelStates: map[string]*ModelState{
|
||||
"gemini-3-flash": {
|
||||
Unavailable: true,
|
||||
NextRetryAfter: time.Now().Add(10 * time.Minute),
|
||||
Quota: QuotaState{
|
||||
Exceeded: true,
|
||||
NextRecoverAt: time.Now().Add(10 * time.Minute),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
SetAntigravityCreditsHint(auth.ID, AntigravityCreditsHint{
|
||||
Known: true,
|
||||
Available: true,
|
||||
UpdatedAt: time.Now(),
|
||||
})
|
||||
|
||||
blocked, reason, _ := isAuthBlockedForModel(auth, "gemini-3-flash", time.Now())
|
||||
if !blocked || reason != blockReasonCooldown {
|
||||
t.Fatalf("expected gemini auth to remain blocked, got blocked=%v reason=%v", blocked, reason)
|
||||
}
|
||||
}
|
||||
@@ -64,8 +64,13 @@ const (
|
||||
refreshMaxConcurrency = 16
|
||||
refreshPendingBackoff = time.Minute
|
||||
refreshFailureBackoff = 5 * time.Minute
|
||||
quotaBackoffBase = time.Second
|
||||
quotaBackoffMax = 30 * time.Minute
|
||||
// refreshIneffectiveBackoff throttles refresh attempts when an executor returns
|
||||
// success but the auth still evaluates as needing refresh (e.g. token expiry
|
||||
// wasn't updated). Without this guard, the auto-refresh loop can tight-loop and
|
||||
// burn CPU at idle.
|
||||
refreshIneffectiveBackoff = 30 * time.Second
|
||||
quotaBackoffBase = time.Second
|
||||
quotaBackoffMax = 30 * time.Minute
|
||||
)
|
||||
|
||||
var quotaCooldownDisabled atomic.Bool
|
||||
@@ -1197,12 +1202,16 @@ func (m *Manager) Execute(ctx context.Context, providers []string, req cliproxye
|
||||
}
|
||||
}
|
||||
if lastErr != nil {
|
||||
if shouldAttemptAntigravityCreditsFallback(m, lastErr, normalized) {
|
||||
if resp, ok := m.tryAntigravityCreditsExecute(ctx, req, opts); ok {
|
||||
return resp, nil
|
||||
}
|
||||
}
|
||||
return cliproxyexecutor.Response{}, lastErr
|
||||
}
|
||||
return cliproxyexecutor.Response{}, &Error{Code: "auth_not_found", Message: "no auth available"}
|
||||
}
|
||||
|
||||
// ExecuteCount performs a non-streaming execution using the configured selector and executor.
|
||||
// It supports multiple providers for the same model and round-robins the starting provider per model.
|
||||
func (m *Manager) ExecuteCount(ctx context.Context, providers []string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
|
||||
normalized := m.normalizeProviders(providers)
|
||||
@@ -1259,6 +1268,11 @@ func (m *Manager) ExecuteStream(ctx context.Context, providers []string, req cli
|
||||
}
|
||||
}
|
||||
if lastErr != nil {
|
||||
if shouldAttemptAntigravityCreditsFallback(m, lastErr, normalized) {
|
||||
if result, ok := m.tryAntigravityCreditsExecuteStream(ctx, req, opts); ok {
|
||||
return result, nil
|
||||
}
|
||||
}
|
||||
return nil, lastErr
|
||||
}
|
||||
return nil, &Error{Code: "auth_not_found", Message: "no auth available"}
|
||||
@@ -2314,7 +2328,8 @@ func retryAfterFromError(err error) *time.Duration {
|
||||
if retryAfter == nil {
|
||||
return nil
|
||||
}
|
||||
return new(*retryAfter)
|
||||
value := *retryAfter
|
||||
return &value
|
||||
}
|
||||
|
||||
func statusCodeFromResult(err *Error) int {
|
||||
@@ -2404,11 +2419,18 @@ func isRequestInvalidError(err error) bool {
|
||||
status := statusCodeFromError(err)
|
||||
switch status {
|
||||
case http.StatusBadRequest:
|
||||
return strings.Contains(err.Error(), "invalid_request_error")
|
||||
msg := err.Error()
|
||||
return strings.Contains(msg, "invalid_request_error") ||
|
||||
strings.Contains(msg, "INVALID_ARGUMENT") ||
|
||||
strings.Contains(msg, "FAILED_PRECONDITION")
|
||||
case http.StatusNotFound:
|
||||
return isRequestScopedNotFoundMessage(err.Error())
|
||||
case http.StatusUnprocessableEntity:
|
||||
return true
|
||||
case http.StatusInternalServerError:
|
||||
msg := err.Error()
|
||||
return strings.Contains(msg, "\"status\":\"UNKNOWN\"") ||
|
||||
strings.Contains(msg, "\"status\": \"UNKNOWN\"")
|
||||
default:
|
||||
return false
|
||||
}
|
||||
@@ -2881,6 +2903,175 @@ func (m *Manager) pickNextMixed(ctx context.Context, providers []string, model s
|
||||
return authCopy, executor, providerKey, nil
|
||||
}
|
||||
|
||||
func (m *Manager) findAllAntigravityCreditsCandidateAuths(routeModel string, opts cliproxyexecutor.Options) []creditsCandidateEntry {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
pinnedAuthID := pinnedAuthIDFromMetadata(opts.Metadata)
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
var known []creditsCandidateEntry
|
||||
var unknown []creditsCandidateEntry
|
||||
for _, auth := range m.auths {
|
||||
if auth == nil || auth.Disabled || auth.Status == StatusDisabled {
|
||||
continue
|
||||
}
|
||||
if pinnedAuthID != "" && auth.ID != pinnedAuthID {
|
||||
continue
|
||||
}
|
||||
if !strings.EqualFold(strings.TrimSpace(auth.Provider), "antigravity") {
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(strings.ToLower(strings.TrimSpace(routeModel)), "claude") {
|
||||
continue
|
||||
}
|
||||
providerKey := strings.TrimSpace(strings.ToLower(auth.Provider))
|
||||
executor, ok := m.executors[providerKey]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
hint, okHint := GetAntigravityCreditsHint(auth.ID)
|
||||
if okHint && hint.Known {
|
||||
if !hint.Available {
|
||||
continue
|
||||
}
|
||||
known = append(known, creditsCandidateEntry{
|
||||
auth: auth.Clone(),
|
||||
executor: executor,
|
||||
provider: providerKey,
|
||||
})
|
||||
continue
|
||||
}
|
||||
unknown = append(unknown, creditsCandidateEntry{
|
||||
auth: auth.Clone(),
|
||||
executor: executor,
|
||||
provider: providerKey,
|
||||
})
|
||||
}
|
||||
sort.Slice(known, func(i, j int) bool {
|
||||
return known[i].auth.ID < known[j].auth.ID
|
||||
})
|
||||
sort.Slice(unknown, func(i, j int) bool {
|
||||
return unknown[i].auth.ID < unknown[j].auth.ID
|
||||
})
|
||||
return append(known, unknown...)
|
||||
}
|
||||
|
||||
type creditsCandidateEntry struct {
|
||||
auth *Auth
|
||||
executor ProviderExecutor
|
||||
provider string
|
||||
}
|
||||
|
||||
func shouldAttemptAntigravityCreditsFallback(m *Manager, lastErr error, providers []string) bool {
|
||||
if m == nil || lastErr == nil {
|
||||
return false
|
||||
}
|
||||
if len(providers) > 0 {
|
||||
hasAntigravity := false
|
||||
for _, p := range providers {
|
||||
if strings.EqualFold(strings.TrimSpace(p), "antigravity") {
|
||||
hasAntigravity = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasAntigravity {
|
||||
return false
|
||||
}
|
||||
}
|
||||
cfg, _ := m.runtimeConfig.Load().(*internalconfig.Config)
|
||||
if cfg == nil || !cfg.QuotaExceeded.AntigravityCredits {
|
||||
return false
|
||||
}
|
||||
status := statusCodeFromError(lastErr)
|
||||
switch status {
|
||||
case http.StatusTooManyRequests, http.StatusServiceUnavailable:
|
||||
return true
|
||||
case 0:
|
||||
var authErr *Error
|
||||
if errors.As(lastErr, &authErr) && authErr != nil {
|
||||
return authErr.Code == "auth_not_found" || authErr.Code == "auth_unavailable" || authErr.Code == "model_cooldown"
|
||||
}
|
||||
var cooldownErr *modelCooldownError
|
||||
if errors.As(lastErr, &cooldownErr) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) tryAntigravityCreditsExecute(ctx context.Context, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, bool) {
|
||||
routeModel := req.Model
|
||||
candidates := m.findAllAntigravityCreditsCandidateAuths(routeModel, opts)
|
||||
for _, c := range candidates {
|
||||
if ctx.Err() != nil {
|
||||
return cliproxyexecutor.Response{}, false
|
||||
}
|
||||
creditsCtx := WithAntigravityCredits(ctx)
|
||||
if rt := m.roundTripperFor(c.auth); rt != nil {
|
||||
creditsCtx = context.WithValue(creditsCtx, roundTripperContextKey{}, rt)
|
||||
creditsCtx = context.WithValue(creditsCtx, "cliproxy.roundtripper", rt)
|
||||
}
|
||||
creditsOpts := ensureRequestedModelMetadata(opts, routeModel)
|
||||
publishSelectedAuthMetadata(creditsOpts.Metadata, c.auth.ID)
|
||||
models := m.executionModelCandidates(c.auth, routeModel)
|
||||
if len(models) == 0 {
|
||||
continue
|
||||
}
|
||||
for _, upstreamModel := range models {
|
||||
resultModel := m.stateModelForExecution(c.auth, routeModel, upstreamModel, len(models) > 1)
|
||||
execReq := req
|
||||
execReq.Model = upstreamModel
|
||||
resp, errExec := c.executor.Execute(creditsCtx, c.auth, execReq, creditsOpts)
|
||||
result := Result{AuthID: c.auth.ID, Provider: c.provider, Model: resultModel, Success: errExec == nil}
|
||||
if errExec != nil {
|
||||
result.Error = &Error{Message: errExec.Error()}
|
||||
if se, ok := errors.AsType[cliproxyexecutor.StatusError](errExec); ok && se != nil {
|
||||
result.Error.HTTPStatus = se.StatusCode()
|
||||
}
|
||||
if ra := retryAfterFromError(errExec); ra != nil {
|
||||
result.RetryAfter = ra
|
||||
}
|
||||
m.MarkResult(creditsCtx, result)
|
||||
continue
|
||||
}
|
||||
m.MarkResult(creditsCtx, result)
|
||||
return resp, true
|
||||
}
|
||||
}
|
||||
return cliproxyexecutor.Response{}, false
|
||||
}
|
||||
|
||||
func (m *Manager) tryAntigravityCreditsExecuteStream(ctx context.Context, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, bool) {
|
||||
routeModel := req.Model
|
||||
candidates := m.findAllAntigravityCreditsCandidateAuths(routeModel, opts)
|
||||
for _, c := range candidates {
|
||||
if ctx.Err() != nil {
|
||||
return nil, false
|
||||
}
|
||||
creditsCtx := WithAntigravityCredits(ctx)
|
||||
if rt := m.roundTripperFor(c.auth); rt != nil {
|
||||
creditsCtx = context.WithValue(creditsCtx, roundTripperContextKey{}, rt)
|
||||
creditsCtx = context.WithValue(creditsCtx, "cliproxy.roundtripper", rt)
|
||||
}
|
||||
creditsOpts := ensureRequestedModelMetadata(opts, routeModel)
|
||||
publishSelectedAuthMetadata(creditsOpts.Metadata, c.auth.ID)
|
||||
models := m.executionModelCandidates(c.auth, routeModel)
|
||||
if len(models) == 0 {
|
||||
continue
|
||||
}
|
||||
result, errStream := m.executeStreamWithModelPool(creditsCtx, c.executor, c.auth, c.provider, req, creditsOpts, routeModel, models, len(models) > 1)
|
||||
if errStream != nil {
|
||||
continue
|
||||
}
|
||||
return result, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (m *Manager) persist(ctx context.Context, auth *Auth) error {
|
||||
if m.store == nil || auth == nil {
|
||||
return nil
|
||||
@@ -3195,14 +3386,15 @@ func (m *Manager) refreshAuth(ctx context.Context, id string) {
|
||||
m.mu.RLock()
|
||||
auth := m.auths[id]
|
||||
var exec ProviderExecutor
|
||||
var cloned *Auth
|
||||
if auth != nil {
|
||||
exec = m.executors[auth.Provider]
|
||||
cloned = auth.Clone()
|
||||
}
|
||||
m.mu.RUnlock()
|
||||
if auth == nil || exec == nil {
|
||||
return
|
||||
}
|
||||
cloned := auth.Clone()
|
||||
updated, err := exec.Refresh(ctx, cloned)
|
||||
if err != nil && errors.Is(err, context.Canceled) {
|
||||
log.Debugf("refresh canceled for %s, %s", auth.Provider, auth.ID)
|
||||
@@ -3240,6 +3432,9 @@ func (m *Manager) refreshAuth(ctx context.Context, id string) {
|
||||
updated.NextRefreshAfter = time.Time{}
|
||||
updated.LastError = nil
|
||||
updated.UpdatedAt = now
|
||||
if m.shouldRefresh(updated, now) {
|
||||
updated.NextRefreshAfter = now.Add(refreshIneffectiveBackoff)
|
||||
}
|
||||
_, _ = m.Update(ctx, updated)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
|
||||
)
|
||||
|
||||
func TestFindAllAntigravityCreditsCandidateAuths_PrefersKnownCreditsThenUnknown(t *testing.T) {
|
||||
m := &Manager{
|
||||
auths: map[string]*Auth{
|
||||
"zz-credits": {ID: "zz-credits", Provider: "antigravity"},
|
||||
"aa-unknown": {ID: "aa-unknown", Provider: "antigravity"},
|
||||
"mm-no": {ID: "mm-no", Provider: "antigravity"},
|
||||
},
|
||||
executors: map[string]ProviderExecutor{
|
||||
"antigravity": schedulerTestExecutor{},
|
||||
},
|
||||
}
|
||||
|
||||
SetAntigravityCreditsHint("zz-credits", AntigravityCreditsHint{
|
||||
Known: true,
|
||||
Available: true,
|
||||
UpdatedAt: time.Now(),
|
||||
})
|
||||
SetAntigravityCreditsHint("mm-no", AntigravityCreditsHint{
|
||||
Known: true,
|
||||
Available: false,
|
||||
UpdatedAt: time.Now(),
|
||||
})
|
||||
|
||||
opts := cliproxyexecutor.Options{}
|
||||
|
||||
candidates := m.findAllAntigravityCreditsCandidateAuths("claude-sonnet-4-6", opts)
|
||||
if len(candidates) != 2 {
|
||||
t.Fatalf("candidates len = %d, want 2", len(candidates))
|
||||
}
|
||||
if candidates[0].auth.ID != "zz-credits" {
|
||||
t.Fatalf("candidates[0].auth.ID = %q, want %q", candidates[0].auth.ID, "zz-credits")
|
||||
}
|
||||
if candidates[1].auth.ID != "aa-unknown" {
|
||||
t.Fatalf("candidates[1].auth.ID = %q, want %q", candidates[1].auth.ID, "aa-unknown")
|
||||
}
|
||||
|
||||
nonClaude := m.findAllAntigravityCreditsCandidateAuths("gemini-3-flash", opts)
|
||||
if len(nonClaude) != 0 {
|
||||
t.Fatalf("nonClaude len = %d, want 0", len(nonClaude))
|
||||
}
|
||||
|
||||
pinnedOpts := cliproxyexecutor.Options{
|
||||
Metadata: map[string]any{cliproxyexecutor.PinnedAuthMetadataKey: "aa-unknown"},
|
||||
}
|
||||
pinned := m.findAllAntigravityCreditsCandidateAuths("claude-sonnet-4-6", pinnedOpts)
|
||||
if len(pinned) != 1 {
|
||||
t.Fatalf("pinned len = %d, want 1", len(pinned))
|
||||
}
|
||||
if pinned[0].auth.ID != "aa-unknown" {
|
||||
t.Fatalf("pinned[0].auth.ID = %q, want %q", pinned[0].auth.ID, "aa-unknown")
|
||||
}
|
||||
}
|
||||
@@ -1410,7 +1410,7 @@ func buildCodexConfigModels(entry *config.CodexKey) []*ModelInfo {
|
||||
if entry == nil {
|
||||
return nil
|
||||
}
|
||||
return buildConfigModels(entry.Models, "openai", "openai")
|
||||
return registry.WithCodexBuiltins(buildConfigModels(entry.Models, "openai", "openai"))
|
||||
}
|
||||
|
||||
func rewriteModelInfoName(name, oldID, newID string) string {
|
||||
|
||||
Reference in New Issue
Block a user