mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-04-24 06:40:31 +00:00
Compare commits
37 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3de7a7f0cd | ||
|
|
4adb9eed77 | ||
|
|
b6a0f7a07f | ||
|
|
b2566368f8 | ||
|
|
1b2f907671 | ||
|
|
bda04eed8a | ||
|
|
e0735977b5 | ||
|
|
67985d8226 | ||
|
|
1fb4f2b12e | ||
|
|
f4ba1ab910 | ||
|
|
2662f91082 | ||
|
|
f5967069f2 | ||
|
|
80f5523685 | ||
|
|
c1db2c7d7c | ||
|
|
5e5d8142f9 | ||
|
|
b01619b441 | ||
|
|
109cf3928a | ||
|
|
4794645dec | ||
|
|
f861bd6a94 | ||
|
|
6dbfdd140d | ||
|
|
386ccffed4 | ||
|
|
ffddd1c90a | ||
|
|
8f8dfd081b | ||
|
|
9f1b445c7c | ||
|
|
ae933dfe14 | ||
|
|
5d33d6b8ea | ||
|
|
e124db723b | ||
|
|
05444cf32d | ||
|
|
478aff1189 | ||
|
|
8edbda57cf | ||
|
|
821249a5ed | ||
|
|
2331b9a2e7 | ||
|
|
ee33863b47 | ||
|
|
cd22c849e2 | ||
|
|
f0e73efda2 | ||
|
|
3156109c71 | ||
|
|
6762e081f3 |
@@ -33,6 +33,13 @@ var geminiOAuthScopes = []string{
|
|||||||
"https://www.googleapis.com/auth/userinfo.profile",
|
"https://www.googleapis.com/auth/userinfo.profile",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
antigravityOAuthClientID = "1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com"
|
||||||
|
antigravityOAuthClientSecret = "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"
|
||||||
|
)
|
||||||
|
|
||||||
|
var antigravityOAuthTokenURL = "https://oauth2.googleapis.com/token"
|
||||||
|
|
||||||
type apiCallRequest struct {
|
type apiCallRequest struct {
|
||||||
AuthIndexSnake *string `json:"auth_index"`
|
AuthIndexSnake *string `json:"auth_index"`
|
||||||
AuthIndexCamel *string `json:"authIndex"`
|
AuthIndexCamel *string `json:"authIndex"`
|
||||||
@@ -251,6 +258,10 @@ func (h *Handler) resolveTokenForAuth(ctx context.Context, auth *coreauth.Auth)
|
|||||||
token, errToken := h.refreshGeminiOAuthAccessToken(ctx, auth)
|
token, errToken := h.refreshGeminiOAuthAccessToken(ctx, auth)
|
||||||
return token, errToken
|
return token, errToken
|
||||||
}
|
}
|
||||||
|
if provider == "antigravity" {
|
||||||
|
token, errToken := h.refreshAntigravityOAuthAccessToken(ctx, auth)
|
||||||
|
return token, errToken
|
||||||
|
}
|
||||||
|
|
||||||
return tokenValueForAuth(auth), nil
|
return tokenValueForAuth(auth), nil
|
||||||
}
|
}
|
||||||
@@ -325,6 +336,161 @@ func (h *Handler) refreshGeminiOAuthAccessToken(ctx context.Context, auth *corea
|
|||||||
return strings.TrimSpace(currentToken.AccessToken), nil
|
return strings.TrimSpace(currentToken.AccessToken), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) refreshAntigravityOAuthAccessToken(ctx context.Context, auth *coreauth.Auth) (string, error) {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
|
if auth == nil {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata := auth.Metadata
|
||||||
|
if len(metadata) == 0 {
|
||||||
|
return "", fmt.Errorf("antigravity oauth metadata missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
current := strings.TrimSpace(tokenValueFromMetadata(metadata))
|
||||||
|
if current != "" && !antigravityTokenNeedsRefresh(metadata) {
|
||||||
|
return current, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
refreshToken := stringValue(metadata, "refresh_token")
|
||||||
|
if refreshToken == "" {
|
||||||
|
return "", fmt.Errorf("antigravity refresh token missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenURL := strings.TrimSpace(antigravityOAuthTokenURL)
|
||||||
|
if tokenURL == "" {
|
||||||
|
tokenURL = "https://oauth2.googleapis.com/token"
|
||||||
|
}
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("client_id", antigravityOAuthClientID)
|
||||||
|
form.Set("client_secret", antigravityOAuthClientSecret)
|
||||||
|
form.Set("grant_type", "refresh_token")
|
||||||
|
form.Set("refresh_token", refreshToken)
|
||||||
|
|
||||||
|
req, errReq := http.NewRequestWithContext(ctx, http.MethodPost, tokenURL, strings.NewReader(form.Encode()))
|
||||||
|
if errReq != nil {
|
||||||
|
return "", errReq
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
httpClient := &http.Client{
|
||||||
|
Timeout: defaultAPICallTimeout,
|
||||||
|
Transport: h.apiCallTransport(auth),
|
||||||
|
}
|
||||||
|
resp, errDo := httpClient.Do(req)
|
||||||
|
if errDo != nil {
|
||||||
|
return "", errDo
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if errClose := resp.Body.Close(); errClose != nil {
|
||||||
|
log.Errorf("response body close error: %v", errClose)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
bodyBytes, errRead := io.ReadAll(resp.Body)
|
||||||
|
if errRead != nil {
|
||||||
|
return "", errRead
|
||||||
|
}
|
||||||
|
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
|
||||||
|
return "", fmt.Errorf("antigravity oauth token refresh failed: status %d: %s", resp.StatusCode, strings.TrimSpace(string(bodyBytes)))
|
||||||
|
}
|
||||||
|
|
||||||
|
var tokenResp struct {
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
RefreshToken string `json:"refresh_token"`
|
||||||
|
ExpiresIn int64 `json:"expires_in"`
|
||||||
|
TokenType string `json:"token_type"`
|
||||||
|
}
|
||||||
|
if errUnmarshal := json.Unmarshal(bodyBytes, &tokenResp); errUnmarshal != nil {
|
||||||
|
return "", errUnmarshal
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.TrimSpace(tokenResp.AccessToken) == "" {
|
||||||
|
return "", fmt.Errorf("antigravity oauth token refresh returned empty access_token")
|
||||||
|
}
|
||||||
|
|
||||||
|
if auth.Metadata == nil {
|
||||||
|
auth.Metadata = make(map[string]any)
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
auth.Metadata["access_token"] = strings.TrimSpace(tokenResp.AccessToken)
|
||||||
|
if strings.TrimSpace(tokenResp.RefreshToken) != "" {
|
||||||
|
auth.Metadata["refresh_token"] = strings.TrimSpace(tokenResp.RefreshToken)
|
||||||
|
}
|
||||||
|
if tokenResp.ExpiresIn > 0 {
|
||||||
|
auth.Metadata["expires_in"] = tokenResp.ExpiresIn
|
||||||
|
auth.Metadata["timestamp"] = now.UnixMilli()
|
||||||
|
auth.Metadata["expired"] = now.Add(time.Duration(tokenResp.ExpiresIn) * time.Second).Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
auth.Metadata["type"] = "antigravity"
|
||||||
|
|
||||||
|
if h != nil && h.authManager != nil {
|
||||||
|
auth.LastRefreshedAt = now
|
||||||
|
auth.UpdatedAt = now
|
||||||
|
_, _ = h.authManager.Update(ctx, auth)
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.TrimSpace(tokenResp.AccessToken), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func antigravityTokenNeedsRefresh(metadata map[string]any) bool {
|
||||||
|
// Refresh a bit early to avoid requests racing token expiry.
|
||||||
|
const skew = 30 * time.Second
|
||||||
|
|
||||||
|
if metadata == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if expStr, ok := metadata["expired"].(string); ok {
|
||||||
|
if ts, errParse := time.Parse(time.RFC3339, strings.TrimSpace(expStr)); errParse == nil {
|
||||||
|
return !ts.After(time.Now().Add(skew))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expiresIn := int64Value(metadata["expires_in"])
|
||||||
|
timestampMs := int64Value(metadata["timestamp"])
|
||||||
|
if expiresIn > 0 && timestampMs > 0 {
|
||||||
|
exp := time.UnixMilli(timestampMs).Add(time.Duration(expiresIn) * time.Second)
|
||||||
|
return !exp.After(time.Now().Add(skew))
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func int64Value(raw any) int64 {
|
||||||
|
switch typed := raw.(type) {
|
||||||
|
case int:
|
||||||
|
return int64(typed)
|
||||||
|
case int32:
|
||||||
|
return int64(typed)
|
||||||
|
case int64:
|
||||||
|
return typed
|
||||||
|
case uint:
|
||||||
|
return int64(typed)
|
||||||
|
case uint32:
|
||||||
|
return int64(typed)
|
||||||
|
case uint64:
|
||||||
|
if typed > uint64(^uint64(0)>>1) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return int64(typed)
|
||||||
|
case float32:
|
||||||
|
return int64(typed)
|
||||||
|
case float64:
|
||||||
|
return int64(typed)
|
||||||
|
case json.Number:
|
||||||
|
if i, errParse := typed.Int64(); errParse == nil {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
case string:
|
||||||
|
if s := strings.TrimSpace(typed); s != "" {
|
||||||
|
if i, errParse := json.Number(s).Int64(); errParse == nil {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func geminiOAuthMetadata(auth *coreauth.Auth) (map[string]any, func(map[string]any)) {
|
func geminiOAuthMetadata(auth *coreauth.Auth) (map[string]any, func(map[string]any)) {
|
||||||
if auth == nil {
|
if auth == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|||||||
173
internal/api/handlers/management/api_tools_test.go
Normal file
173
internal/api/handlers/management/api_tools_test.go
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
package management
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
|
||||||
|
)
|
||||||
|
|
||||||
|
type memoryAuthStore struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
items map[string]*coreauth.Auth
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *memoryAuthStore) List(ctx context.Context) ([]*coreauth.Auth, error) {
|
||||||
|
_ = ctx
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
out := make([]*coreauth.Auth, 0, len(s.items))
|
||||||
|
for _, a := range s.items {
|
||||||
|
out = append(out, a.Clone())
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *memoryAuthStore) Save(ctx context.Context, auth *coreauth.Auth) (string, error) {
|
||||||
|
_ = ctx
|
||||||
|
if auth == nil {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
s.mu.Lock()
|
||||||
|
if s.items == nil {
|
||||||
|
s.items = make(map[string]*coreauth.Auth)
|
||||||
|
}
|
||||||
|
s.items[auth.ID] = auth.Clone()
|
||||||
|
s.mu.Unlock()
|
||||||
|
return auth.ID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *memoryAuthStore) Delete(ctx context.Context, id string) error {
|
||||||
|
_ = ctx
|
||||||
|
s.mu.Lock()
|
||||||
|
delete(s.items, id)
|
||||||
|
s.mu.Unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveTokenForAuth_Antigravity_RefreshesExpiredToken(t *testing.T) {
|
||||||
|
var callCount int
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
callCount++
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
t.Fatalf("expected POST, got %s", r.Method)
|
||||||
|
}
|
||||||
|
if ct := r.Header.Get("Content-Type"); !strings.HasPrefix(ct, "application/x-www-form-urlencoded") {
|
||||||
|
t.Fatalf("unexpected content-type: %s", ct)
|
||||||
|
}
|
||||||
|
bodyBytes, _ := io.ReadAll(r.Body)
|
||||||
|
_ = r.Body.Close()
|
||||||
|
values, err := url.ParseQuery(string(bodyBytes))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("parse form: %v", err)
|
||||||
|
}
|
||||||
|
if values.Get("grant_type") != "refresh_token" {
|
||||||
|
t.Fatalf("unexpected grant_type: %s", values.Get("grant_type"))
|
||||||
|
}
|
||||||
|
if values.Get("refresh_token") != "rt" {
|
||||||
|
t.Fatalf("unexpected refresh_token: %s", values.Get("refresh_token"))
|
||||||
|
}
|
||||||
|
if values.Get("client_id") != antigravityOAuthClientID {
|
||||||
|
t.Fatalf("unexpected client_id: %s", values.Get("client_id"))
|
||||||
|
}
|
||||||
|
if values.Get("client_secret") != antigravityOAuthClientSecret {
|
||||||
|
t.Fatalf("unexpected client_secret")
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||||
|
"access_token": "new-token",
|
||||||
|
"refresh_token": "rt2",
|
||||||
|
"expires_in": int64(3600),
|
||||||
|
"token_type": "Bearer",
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
t.Cleanup(srv.Close)
|
||||||
|
|
||||||
|
originalURL := antigravityOAuthTokenURL
|
||||||
|
antigravityOAuthTokenURL = srv.URL
|
||||||
|
t.Cleanup(func() { antigravityOAuthTokenURL = originalURL })
|
||||||
|
|
||||||
|
store := &memoryAuthStore{}
|
||||||
|
manager := coreauth.NewManager(store, nil, nil)
|
||||||
|
|
||||||
|
auth := &coreauth.Auth{
|
||||||
|
ID: "antigravity-test.json",
|
||||||
|
FileName: "antigravity-test.json",
|
||||||
|
Provider: "antigravity",
|
||||||
|
Metadata: map[string]any{
|
||||||
|
"type": "antigravity",
|
||||||
|
"access_token": "old-token",
|
||||||
|
"refresh_token": "rt",
|
||||||
|
"expires_in": int64(3600),
|
||||||
|
"timestamp": time.Now().Add(-2 * time.Hour).UnixMilli(),
|
||||||
|
"expired": time.Now().Add(-1 * time.Hour).Format(time.RFC3339),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if _, err := manager.Register(context.Background(), auth); err != nil {
|
||||||
|
t.Fatalf("register auth: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h := &Handler{authManager: manager}
|
||||||
|
token, err := h.resolveTokenForAuth(context.Background(), auth)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("resolveTokenForAuth: %v", err)
|
||||||
|
}
|
||||||
|
if token != "new-token" {
|
||||||
|
t.Fatalf("expected refreshed token, got %q", token)
|
||||||
|
}
|
||||||
|
if callCount != 1 {
|
||||||
|
t.Fatalf("expected 1 refresh call, got %d", callCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
updated, ok := manager.GetByID(auth.ID)
|
||||||
|
if !ok || updated == nil {
|
||||||
|
t.Fatalf("expected auth in manager after update")
|
||||||
|
}
|
||||||
|
if got := tokenValueFromMetadata(updated.Metadata); got != "new-token" {
|
||||||
|
t.Fatalf("expected manager metadata updated, got %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveTokenForAuth_Antigravity_SkipsRefreshWhenTokenValid(t *testing.T) {
|
||||||
|
var callCount int
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
callCount++
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
}))
|
||||||
|
t.Cleanup(srv.Close)
|
||||||
|
|
||||||
|
originalURL := antigravityOAuthTokenURL
|
||||||
|
antigravityOAuthTokenURL = srv.URL
|
||||||
|
t.Cleanup(func() { antigravityOAuthTokenURL = originalURL })
|
||||||
|
|
||||||
|
auth := &coreauth.Auth{
|
||||||
|
ID: "antigravity-valid.json",
|
||||||
|
FileName: "antigravity-valid.json",
|
||||||
|
Provider: "antigravity",
|
||||||
|
Metadata: map[string]any{
|
||||||
|
"type": "antigravity",
|
||||||
|
"access_token": "ok-token",
|
||||||
|
"expired": time.Now().Add(30 * time.Minute).Format(time.RFC3339),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
h := &Handler{}
|
||||||
|
token, err := h.resolveTokenForAuth(context.Background(), auth)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("resolveTokenForAuth: %v", err)
|
||||||
|
}
|
||||||
|
if token != "ok-token" {
|
||||||
|
t.Fatalf("expected existing token, got %q", token)
|
||||||
|
}
|
||||||
|
if callCount != 0 {
|
||||||
|
t.Fatalf("expected no refresh calls, got %d", callCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -202,6 +202,26 @@ func (h *Handler) PutLoggingToFile(c *gin.Context) {
|
|||||||
h.updateBoolField(c, func(v bool) { h.cfg.LoggingToFile = v })
|
h.updateBoolField(c, func(v bool) { h.cfg.LoggingToFile = v })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LogsMaxTotalSizeMB
|
||||||
|
func (h *Handler) GetLogsMaxTotalSizeMB(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{"logs-max-total-size-mb": h.cfg.LogsMaxTotalSizeMB})
|
||||||
|
}
|
||||||
|
func (h *Handler) PutLogsMaxTotalSizeMB(c *gin.Context) {
|
||||||
|
var body struct {
|
||||||
|
Value *int `json:"value"`
|
||||||
|
}
|
||||||
|
if errBindJSON := c.ShouldBindJSON(&body); errBindJSON != nil || body.Value == nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
value := *body.Value
|
||||||
|
if value < 0 {
|
||||||
|
value = 0
|
||||||
|
}
|
||||||
|
h.cfg.LogsMaxTotalSizeMB = value
|
||||||
|
h.persist(c)
|
||||||
|
}
|
||||||
|
|
||||||
// Request log
|
// Request log
|
||||||
func (h *Handler) GetRequestLog(c *gin.Context) { c.JSON(200, gin.H{"request-log": h.cfg.RequestLog}) }
|
func (h *Handler) GetRequestLog(c *gin.Context) { c.JSON(200, gin.H{"request-log": h.cfg.RequestLog}) }
|
||||||
func (h *Handler) PutRequestLog(c *gin.Context) {
|
func (h *Handler) PutRequestLog(c *gin.Context) {
|
||||||
@@ -232,6 +252,52 @@ func (h *Handler) PutMaxRetryInterval(c *gin.Context) {
|
|||||||
h.updateIntField(c, func(v int) { h.cfg.MaxRetryInterval = v })
|
h.updateIntField(c, func(v int) { h.cfg.MaxRetryInterval = v })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ForceModelPrefix
|
||||||
|
func (h *Handler) GetForceModelPrefix(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{"force-model-prefix": h.cfg.ForceModelPrefix})
|
||||||
|
}
|
||||||
|
func (h *Handler) PutForceModelPrefix(c *gin.Context) {
|
||||||
|
h.updateBoolField(c, func(v bool) { h.cfg.ForceModelPrefix = v })
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeRoutingStrategy(strategy string) (string, bool) {
|
||||||
|
normalized := strings.ToLower(strings.TrimSpace(strategy))
|
||||||
|
switch normalized {
|
||||||
|
case "", "round-robin", "roundrobin", "rr":
|
||||||
|
return "round-robin", true
|
||||||
|
case "fill-first", "fillfirst", "ff":
|
||||||
|
return "fill-first", true
|
||||||
|
default:
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoutingStrategy
|
||||||
|
func (h *Handler) GetRoutingStrategy(c *gin.Context) {
|
||||||
|
strategy, ok := normalizeRoutingStrategy(h.cfg.Routing.Strategy)
|
||||||
|
if !ok {
|
||||||
|
c.JSON(200, gin.H{"strategy": strings.TrimSpace(h.cfg.Routing.Strategy)})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, gin.H{"strategy": strategy})
|
||||||
|
}
|
||||||
|
func (h *Handler) PutRoutingStrategy(c *gin.Context) {
|
||||||
|
var body struct {
|
||||||
|
Value *string `json:"value"`
|
||||||
|
}
|
||||||
|
if errBindJSON := c.ShouldBindJSON(&body); errBindJSON != nil || body.Value == nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
normalized, ok := normalizeRoutingStrategy(*body.Value)
|
||||||
|
if !ok {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid strategy"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.cfg.Routing.Strategy = normalized
|
||||||
|
h.persist(c)
|
||||||
|
}
|
||||||
|
|
||||||
// Proxy URL
|
// Proxy URL
|
||||||
func (h *Handler) GetProxyURL(c *gin.Context) { c.JSON(200, gin.H{"proxy-url": h.cfg.ProxyURL}) }
|
func (h *Handler) GetProxyURL(c *gin.Context) { c.JSON(200, gin.H{"proxy-url": h.cfg.ProxyURL}) }
|
||||||
func (h *Handler) PutProxyURL(c *gin.Context) {
|
func (h *Handler) PutProxyURL(c *gin.Context) {
|
||||||
|
|||||||
@@ -487,6 +487,137 @@ func (h *Handler) DeleteOpenAICompat(c *gin.Context) {
|
|||||||
c.JSON(400, gin.H{"error": "missing name or index"})
|
c.JSON(400, gin.H{"error": "missing name or index"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// vertex-api-key: []VertexCompatKey
|
||||||
|
func (h *Handler) GetVertexCompatKeys(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{"vertex-api-key": h.cfg.VertexCompatAPIKey})
|
||||||
|
}
|
||||||
|
func (h *Handler) PutVertexCompatKeys(c *gin.Context) {
|
||||||
|
data, err := c.GetRawData()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": "failed to read body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var arr []config.VertexCompatKey
|
||||||
|
if err = json.Unmarshal(data, &arr); err != nil {
|
||||||
|
var obj struct {
|
||||||
|
Items []config.VertexCompatKey `json:"items"`
|
||||||
|
}
|
||||||
|
if err2 := json.Unmarshal(data, &obj); err2 != nil || len(obj.Items) == 0 {
|
||||||
|
c.JSON(400, gin.H{"error": "invalid body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
arr = obj.Items
|
||||||
|
}
|
||||||
|
for i := range arr {
|
||||||
|
normalizeVertexCompatKey(&arr[i])
|
||||||
|
}
|
||||||
|
h.cfg.VertexCompatAPIKey = arr
|
||||||
|
h.cfg.SanitizeVertexCompatKeys()
|
||||||
|
h.persist(c)
|
||||||
|
}
|
||||||
|
func (h *Handler) PatchVertexCompatKey(c *gin.Context) {
|
||||||
|
type vertexCompatPatch struct {
|
||||||
|
APIKey *string `json:"api-key"`
|
||||||
|
Prefix *string `json:"prefix"`
|
||||||
|
BaseURL *string `json:"base-url"`
|
||||||
|
ProxyURL *string `json:"proxy-url"`
|
||||||
|
Headers *map[string]string `json:"headers"`
|
||||||
|
Models *[]config.VertexCompatModel `json:"models"`
|
||||||
|
}
|
||||||
|
var body struct {
|
||||||
|
Index *int `json:"index"`
|
||||||
|
Match *string `json:"match"`
|
||||||
|
Value *vertexCompatPatch `json:"value"`
|
||||||
|
}
|
||||||
|
if errBindJSON := c.ShouldBindJSON(&body); errBindJSON != nil || body.Value == nil {
|
||||||
|
c.JSON(400, gin.H{"error": "invalid body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
targetIndex := -1
|
||||||
|
if body.Index != nil && *body.Index >= 0 && *body.Index < len(h.cfg.VertexCompatAPIKey) {
|
||||||
|
targetIndex = *body.Index
|
||||||
|
}
|
||||||
|
if targetIndex == -1 && body.Match != nil {
|
||||||
|
match := strings.TrimSpace(*body.Match)
|
||||||
|
if match != "" {
|
||||||
|
for i := range h.cfg.VertexCompatAPIKey {
|
||||||
|
if h.cfg.VertexCompatAPIKey[i].APIKey == match {
|
||||||
|
targetIndex = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if targetIndex == -1 {
|
||||||
|
c.JSON(404, gin.H{"error": "item not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := h.cfg.VertexCompatAPIKey[targetIndex]
|
||||||
|
if body.Value.APIKey != nil {
|
||||||
|
trimmed := strings.TrimSpace(*body.Value.APIKey)
|
||||||
|
if trimmed == "" {
|
||||||
|
h.cfg.VertexCompatAPIKey = append(h.cfg.VertexCompatAPIKey[:targetIndex], h.cfg.VertexCompatAPIKey[targetIndex+1:]...)
|
||||||
|
h.cfg.SanitizeVertexCompatKeys()
|
||||||
|
h.persist(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
entry.APIKey = trimmed
|
||||||
|
}
|
||||||
|
if body.Value.Prefix != nil {
|
||||||
|
entry.Prefix = strings.TrimSpace(*body.Value.Prefix)
|
||||||
|
}
|
||||||
|
if body.Value.BaseURL != nil {
|
||||||
|
trimmed := strings.TrimSpace(*body.Value.BaseURL)
|
||||||
|
if trimmed == "" {
|
||||||
|
h.cfg.VertexCompatAPIKey = append(h.cfg.VertexCompatAPIKey[:targetIndex], h.cfg.VertexCompatAPIKey[targetIndex+1:]...)
|
||||||
|
h.cfg.SanitizeVertexCompatKeys()
|
||||||
|
h.persist(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
entry.BaseURL = trimmed
|
||||||
|
}
|
||||||
|
if body.Value.ProxyURL != nil {
|
||||||
|
entry.ProxyURL = strings.TrimSpace(*body.Value.ProxyURL)
|
||||||
|
}
|
||||||
|
if body.Value.Headers != nil {
|
||||||
|
entry.Headers = config.NormalizeHeaders(*body.Value.Headers)
|
||||||
|
}
|
||||||
|
if body.Value.Models != nil {
|
||||||
|
entry.Models = append([]config.VertexCompatModel(nil), (*body.Value.Models)...)
|
||||||
|
}
|
||||||
|
normalizeVertexCompatKey(&entry)
|
||||||
|
h.cfg.VertexCompatAPIKey[targetIndex] = entry
|
||||||
|
h.cfg.SanitizeVertexCompatKeys()
|
||||||
|
h.persist(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) DeleteVertexCompatKey(c *gin.Context) {
|
||||||
|
if val := strings.TrimSpace(c.Query("api-key")); val != "" {
|
||||||
|
out := make([]config.VertexCompatKey, 0, len(h.cfg.VertexCompatAPIKey))
|
||||||
|
for _, v := range h.cfg.VertexCompatAPIKey {
|
||||||
|
if v.APIKey != val {
|
||||||
|
out = append(out, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h.cfg.VertexCompatAPIKey = out
|
||||||
|
h.cfg.SanitizeVertexCompatKeys()
|
||||||
|
h.persist(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if idxStr := c.Query("index"); idxStr != "" {
|
||||||
|
var idx int
|
||||||
|
_, errScan := fmt.Sscanf(idxStr, "%d", &idx)
|
||||||
|
if errScan == nil && idx >= 0 && idx < len(h.cfg.VertexCompatAPIKey) {
|
||||||
|
h.cfg.VertexCompatAPIKey = append(h.cfg.VertexCompatAPIKey[:idx], h.cfg.VertexCompatAPIKey[idx+1:]...)
|
||||||
|
h.cfg.SanitizeVertexCompatKeys()
|
||||||
|
h.persist(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.JSON(400, gin.H{"error": "missing api-key or index"})
|
||||||
|
}
|
||||||
|
|
||||||
// oauth-excluded-models: map[string][]string
|
// oauth-excluded-models: map[string][]string
|
||||||
func (h *Handler) GetOAuthExcludedModels(c *gin.Context) {
|
func (h *Handler) GetOAuthExcludedModels(c *gin.Context) {
|
||||||
c.JSON(200, gin.H{"oauth-excluded-models": config.NormalizeOAuthExcludedModels(h.cfg.OAuthExcludedModels)})
|
c.JSON(200, gin.H{"oauth-excluded-models": config.NormalizeOAuthExcludedModels(h.cfg.OAuthExcludedModels)})
|
||||||
@@ -572,6 +703,103 @@ func (h *Handler) DeleteOAuthExcludedModels(c *gin.Context) {
|
|||||||
h.persist(c)
|
h.persist(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// oauth-model-mappings: map[string][]ModelNameMapping
|
||||||
|
func (h *Handler) GetOAuthModelMappings(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{"oauth-model-mappings": sanitizedOAuthModelMappings(h.cfg.OAuthModelMappings)})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) PutOAuthModelMappings(c *gin.Context) {
|
||||||
|
data, err := c.GetRawData()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": "failed to read body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var entries map[string][]config.ModelNameMapping
|
||||||
|
if err = json.Unmarshal(data, &entries); err != nil {
|
||||||
|
var wrapper struct {
|
||||||
|
Items map[string][]config.ModelNameMapping `json:"items"`
|
||||||
|
}
|
||||||
|
if err2 := json.Unmarshal(data, &wrapper); err2 != nil {
|
||||||
|
c.JSON(400, gin.H{"error": "invalid body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
entries = wrapper.Items
|
||||||
|
}
|
||||||
|
h.cfg.OAuthModelMappings = sanitizedOAuthModelMappings(entries)
|
||||||
|
h.persist(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) PatchOAuthModelMappings(c *gin.Context) {
|
||||||
|
var body struct {
|
||||||
|
Provider *string `json:"provider"`
|
||||||
|
Channel *string `json:"channel"`
|
||||||
|
Mappings []config.ModelNameMapping `json:"mappings"`
|
||||||
|
}
|
||||||
|
if errBindJSON := c.ShouldBindJSON(&body); errBindJSON != nil {
|
||||||
|
c.JSON(400, gin.H{"error": "invalid body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
channelRaw := ""
|
||||||
|
if body.Channel != nil {
|
||||||
|
channelRaw = *body.Channel
|
||||||
|
} else if body.Provider != nil {
|
||||||
|
channelRaw = *body.Provider
|
||||||
|
}
|
||||||
|
channel := strings.ToLower(strings.TrimSpace(channelRaw))
|
||||||
|
if channel == "" {
|
||||||
|
c.JSON(400, gin.H{"error": "invalid channel"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
normalizedMap := sanitizedOAuthModelMappings(map[string][]config.ModelNameMapping{channel: body.Mappings})
|
||||||
|
normalized := normalizedMap[channel]
|
||||||
|
if len(normalized) == 0 {
|
||||||
|
if h.cfg.OAuthModelMappings == nil {
|
||||||
|
c.JSON(404, gin.H{"error": "channel not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, ok := h.cfg.OAuthModelMappings[channel]; !ok {
|
||||||
|
c.JSON(404, gin.H{"error": "channel not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
delete(h.cfg.OAuthModelMappings, channel)
|
||||||
|
if len(h.cfg.OAuthModelMappings) == 0 {
|
||||||
|
h.cfg.OAuthModelMappings = nil
|
||||||
|
}
|
||||||
|
h.persist(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if h.cfg.OAuthModelMappings == nil {
|
||||||
|
h.cfg.OAuthModelMappings = make(map[string][]config.ModelNameMapping)
|
||||||
|
}
|
||||||
|
h.cfg.OAuthModelMappings[channel] = normalized
|
||||||
|
h.persist(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) DeleteOAuthModelMappings(c *gin.Context) {
|
||||||
|
channel := strings.ToLower(strings.TrimSpace(c.Query("channel")))
|
||||||
|
if channel == "" {
|
||||||
|
channel = strings.ToLower(strings.TrimSpace(c.Query("provider")))
|
||||||
|
}
|
||||||
|
if channel == "" {
|
||||||
|
c.JSON(400, gin.H{"error": "missing channel"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if h.cfg.OAuthModelMappings == nil {
|
||||||
|
c.JSON(404, gin.H{"error": "channel not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, ok := h.cfg.OAuthModelMappings[channel]; !ok {
|
||||||
|
c.JSON(404, gin.H{"error": "channel not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
delete(h.cfg.OAuthModelMappings, channel)
|
||||||
|
if len(h.cfg.OAuthModelMappings) == 0 {
|
||||||
|
h.cfg.OAuthModelMappings = nil
|
||||||
|
}
|
||||||
|
h.persist(c)
|
||||||
|
}
|
||||||
|
|
||||||
// codex-api-key: []CodexKey
|
// codex-api-key: []CodexKey
|
||||||
func (h *Handler) GetCodexKeys(c *gin.Context) {
|
func (h *Handler) GetCodexKeys(c *gin.Context) {
|
||||||
c.JSON(200, gin.H{"codex-api-key": h.cfg.CodexKey})
|
c.JSON(200, gin.H{"codex-api-key": h.cfg.CodexKey})
|
||||||
@@ -789,6 +1017,53 @@ func normalizeCodexKey(entry *config.CodexKey) {
|
|||||||
entry.Models = normalized
|
entry.Models = normalized
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func normalizeVertexCompatKey(entry *config.VertexCompatKey) {
|
||||||
|
if entry == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
entry.APIKey = strings.TrimSpace(entry.APIKey)
|
||||||
|
entry.Prefix = strings.TrimSpace(entry.Prefix)
|
||||||
|
entry.BaseURL = strings.TrimSpace(entry.BaseURL)
|
||||||
|
entry.ProxyURL = strings.TrimSpace(entry.ProxyURL)
|
||||||
|
entry.Headers = config.NormalizeHeaders(entry.Headers)
|
||||||
|
if len(entry.Models) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
normalized := make([]config.VertexCompatModel, 0, len(entry.Models))
|
||||||
|
for i := range entry.Models {
|
||||||
|
model := entry.Models[i]
|
||||||
|
model.Name = strings.TrimSpace(model.Name)
|
||||||
|
model.Alias = strings.TrimSpace(model.Alias)
|
||||||
|
if model.Name == "" || model.Alias == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
normalized = append(normalized, model)
|
||||||
|
}
|
||||||
|
entry.Models = normalized
|
||||||
|
}
|
||||||
|
|
||||||
|
func sanitizedOAuthModelMappings(entries map[string][]config.ModelNameMapping) map[string][]config.ModelNameMapping {
|
||||||
|
if len(entries) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
copied := make(map[string][]config.ModelNameMapping, len(entries))
|
||||||
|
for channel, mappings := range entries {
|
||||||
|
if len(mappings) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
copied[channel] = append([]config.ModelNameMapping(nil), mappings...)
|
||||||
|
}
|
||||||
|
if len(copied) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
cfg := config.Config{OAuthModelMappings: copied}
|
||||||
|
cfg.SanitizeOAuthModelMappings()
|
||||||
|
if len(cfg.OAuthModelMappings) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return cfg.OAuthModelMappings
|
||||||
|
}
|
||||||
|
|
||||||
// GetAmpCode returns the complete ampcode configuration.
|
// GetAmpCode returns the complete ampcode configuration.
|
||||||
func (h *Handler) GetAmpCode(c *gin.Context) {
|
func (h *Handler) GetAmpCode(c *gin.Context) {
|
||||||
if h == nil || h.cfg == nil {
|
if h == nil || h.cfg == nil {
|
||||||
|
|||||||
@@ -512,6 +512,10 @@ func (s *Server) registerManagementRoutes() {
|
|||||||
mgmt.PUT("/logging-to-file", s.mgmt.PutLoggingToFile)
|
mgmt.PUT("/logging-to-file", s.mgmt.PutLoggingToFile)
|
||||||
mgmt.PATCH("/logging-to-file", s.mgmt.PutLoggingToFile)
|
mgmt.PATCH("/logging-to-file", s.mgmt.PutLoggingToFile)
|
||||||
|
|
||||||
|
mgmt.GET("/logs-max-total-size-mb", s.mgmt.GetLogsMaxTotalSizeMB)
|
||||||
|
mgmt.PUT("/logs-max-total-size-mb", s.mgmt.PutLogsMaxTotalSizeMB)
|
||||||
|
mgmt.PATCH("/logs-max-total-size-mb", s.mgmt.PutLogsMaxTotalSizeMB)
|
||||||
|
|
||||||
mgmt.GET("/usage-statistics-enabled", s.mgmt.GetUsageStatisticsEnabled)
|
mgmt.GET("/usage-statistics-enabled", s.mgmt.GetUsageStatisticsEnabled)
|
||||||
mgmt.PUT("/usage-statistics-enabled", s.mgmt.PutUsageStatisticsEnabled)
|
mgmt.PUT("/usage-statistics-enabled", s.mgmt.PutUsageStatisticsEnabled)
|
||||||
mgmt.PATCH("/usage-statistics-enabled", s.mgmt.PutUsageStatisticsEnabled)
|
mgmt.PATCH("/usage-statistics-enabled", s.mgmt.PutUsageStatisticsEnabled)
|
||||||
@@ -584,6 +588,14 @@ func (s *Server) registerManagementRoutes() {
|
|||||||
mgmt.PUT("/max-retry-interval", s.mgmt.PutMaxRetryInterval)
|
mgmt.PUT("/max-retry-interval", s.mgmt.PutMaxRetryInterval)
|
||||||
mgmt.PATCH("/max-retry-interval", s.mgmt.PutMaxRetryInterval)
|
mgmt.PATCH("/max-retry-interval", s.mgmt.PutMaxRetryInterval)
|
||||||
|
|
||||||
|
mgmt.GET("/force-model-prefix", s.mgmt.GetForceModelPrefix)
|
||||||
|
mgmt.PUT("/force-model-prefix", s.mgmt.PutForceModelPrefix)
|
||||||
|
mgmt.PATCH("/force-model-prefix", s.mgmt.PutForceModelPrefix)
|
||||||
|
|
||||||
|
mgmt.GET("/routing/strategy", s.mgmt.GetRoutingStrategy)
|
||||||
|
mgmt.PUT("/routing/strategy", s.mgmt.PutRoutingStrategy)
|
||||||
|
mgmt.PATCH("/routing/strategy", s.mgmt.PutRoutingStrategy)
|
||||||
|
|
||||||
mgmt.GET("/claude-api-key", s.mgmt.GetClaudeKeys)
|
mgmt.GET("/claude-api-key", s.mgmt.GetClaudeKeys)
|
||||||
mgmt.PUT("/claude-api-key", s.mgmt.PutClaudeKeys)
|
mgmt.PUT("/claude-api-key", s.mgmt.PutClaudeKeys)
|
||||||
mgmt.PATCH("/claude-api-key", s.mgmt.PatchClaudeKey)
|
mgmt.PATCH("/claude-api-key", s.mgmt.PatchClaudeKey)
|
||||||
@@ -599,11 +611,21 @@ func (s *Server) registerManagementRoutes() {
|
|||||||
mgmt.PATCH("/openai-compatibility", s.mgmt.PatchOpenAICompat)
|
mgmt.PATCH("/openai-compatibility", s.mgmt.PatchOpenAICompat)
|
||||||
mgmt.DELETE("/openai-compatibility", s.mgmt.DeleteOpenAICompat)
|
mgmt.DELETE("/openai-compatibility", s.mgmt.DeleteOpenAICompat)
|
||||||
|
|
||||||
|
mgmt.GET("/vertex-api-key", s.mgmt.GetVertexCompatKeys)
|
||||||
|
mgmt.PUT("/vertex-api-key", s.mgmt.PutVertexCompatKeys)
|
||||||
|
mgmt.PATCH("/vertex-api-key", s.mgmt.PatchVertexCompatKey)
|
||||||
|
mgmt.DELETE("/vertex-api-key", s.mgmt.DeleteVertexCompatKey)
|
||||||
|
|
||||||
mgmt.GET("/oauth-excluded-models", s.mgmt.GetOAuthExcludedModels)
|
mgmt.GET("/oauth-excluded-models", s.mgmt.GetOAuthExcludedModels)
|
||||||
mgmt.PUT("/oauth-excluded-models", s.mgmt.PutOAuthExcludedModels)
|
mgmt.PUT("/oauth-excluded-models", s.mgmt.PutOAuthExcludedModels)
|
||||||
mgmt.PATCH("/oauth-excluded-models", s.mgmt.PatchOAuthExcludedModels)
|
mgmt.PATCH("/oauth-excluded-models", s.mgmt.PatchOAuthExcludedModels)
|
||||||
mgmt.DELETE("/oauth-excluded-models", s.mgmt.DeleteOAuthExcludedModels)
|
mgmt.DELETE("/oauth-excluded-models", s.mgmt.DeleteOAuthExcludedModels)
|
||||||
|
|
||||||
|
mgmt.GET("/oauth-model-mappings", s.mgmt.GetOAuthModelMappings)
|
||||||
|
mgmt.PUT("/oauth-model-mappings", s.mgmt.PutOAuthModelMappings)
|
||||||
|
mgmt.PATCH("/oauth-model-mappings", s.mgmt.PatchOAuthModelMappings)
|
||||||
|
mgmt.DELETE("/oauth-model-mappings", s.mgmt.DeleteOAuthModelMappings)
|
||||||
|
|
||||||
mgmt.GET("/auth-files", s.mgmt.ListAuthFiles)
|
mgmt.GET("/auth-files", s.mgmt.ListAuthFiles)
|
||||||
mgmt.GET("/auth-files/models", s.mgmt.GetAuthFileModels)
|
mgmt.GET("/auth-files/models", s.mgmt.GetAuthFileModels)
|
||||||
mgmt.GET("/auth-files/download", s.mgmt.DownloadAuthFile)
|
mgmt.GET("/auth-files/download", s.mgmt.DownloadAuthFile)
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ const (
|
|||||||
defaultAntigravityAgent = "antigravity/1.104.0 darwin/arm64"
|
defaultAntigravityAgent = "antigravity/1.104.0 darwin/arm64"
|
||||||
antigravityAuthType = "antigravity"
|
antigravityAuthType = "antigravity"
|
||||||
refreshSkew = 3000 * time.Second
|
refreshSkew = 3000 * time.Second
|
||||||
tokenRefreshTimeout = 30 * time.Second
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -78,7 +77,7 @@ func (e *AntigravityExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Au
|
|||||||
// Execute performs a non-streaming request to the Antigravity API.
|
// 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) {
|
func (e *AntigravityExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (resp cliproxyexecutor.Response, err error) {
|
||||||
isClaude := strings.Contains(strings.ToLower(req.Model), "claude")
|
isClaude := strings.Contains(strings.ToLower(req.Model), "claude")
|
||||||
if isClaude {
|
if isClaude || strings.Contains(req.Model, "gemini-3-pro") {
|
||||||
return e.executeClaudeNonStream(ctx, auth, req, opts)
|
return e.executeClaudeNonStream(ctx, auth, req, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -951,7 +950,7 @@ func (e *AntigravityExecutor) refreshToken(ctx context.Context, auth *cliproxyau
|
|||||||
httpReq.Header.Set("User-Agent", defaultAntigravityAgent)
|
httpReq.Header.Set("User-Agent", defaultAntigravityAgent)
|
||||||
httpReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
httpReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, tokenRefreshTimeout)
|
httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
|
||||||
httpResp, errDo := httpClient.Do(httpReq)
|
httpResp, errDo := httpClient.Do(httpReq)
|
||||||
if errDo != nil {
|
if errDo != nil {
|
||||||
return auth, errDo
|
return auth, errDo
|
||||||
@@ -1048,6 +1047,19 @@ func (e *AntigravityExecutor) buildRequest(ctx context.Context, auth *cliproxyau
|
|||||||
payload = []byte(strJSON)
|
payload = []byte(strJSON)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.Contains(modelName, "claude") || strings.Contains(modelName, "gemini-3-pro-preview") {
|
||||||
|
systemInstructionPartsResult := gjson.GetBytes(payload, "request.systemInstruction.parts")
|
||||||
|
|
||||||
|
payload, _ = sjson.SetBytes(payload, "request.systemInstruction.role", "user")
|
||||||
|
payload, _ = sjson.SetBytes(payload, "request.systemInstruction.parts.0.text", "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**")
|
||||||
|
|
||||||
|
if systemInstructionPartsResult.Exists() && systemInstructionPartsResult.IsArray() {
|
||||||
|
for _, partResult := range systemInstructionPartsResult.Array() {
|
||||||
|
payload, _ = sjson.SetRawBytes(payload, "request.systemInstruction.parts.-1", []byte(partResult.Raw))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
httpReq, errReq := http.NewRequestWithContext(ctx, http.MethodPost, requestURL.String(), bytes.NewReader(payload))
|
httpReq, errReq := http.NewRequestWithContext(ctx, http.MethodPost, requestURL.String(), bytes.NewReader(payload))
|
||||||
if errReq != nil {
|
if errReq != nil {
|
||||||
return nil, errReq
|
return nil, errReq
|
||||||
@@ -1182,8 +1194,8 @@ func antigravityBaseURLFallbackOrder(auth *cliproxyauth.Auth) []string {
|
|||||||
return []string{base}
|
return []string{base}
|
||||||
}
|
}
|
||||||
return []string{
|
return []string{
|
||||||
antigravityBaseURLDaily,
|
|
||||||
antigravitySandboxBaseURLDaily,
|
antigravitySandboxBaseURLDaily,
|
||||||
|
antigravityBaseURLDaily,
|
||||||
antigravityBaseURLProd,
|
antigravityBaseURLProd,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1211,6 +1223,7 @@ func resolveCustomAntigravityBaseURL(auth *cliproxyauth.Auth) string {
|
|||||||
func geminiToAntigravity(modelName string, payload []byte, projectID string) []byte {
|
func geminiToAntigravity(modelName string, payload []byte, projectID string) []byte {
|
||||||
template, _ := sjson.Set(string(payload), "model", modelName)
|
template, _ := sjson.Set(string(payload), "model", modelName)
|
||||||
template, _ = sjson.Set(template, "userAgent", "antigravity")
|
template, _ = sjson.Set(template, "userAgent", "antigravity")
|
||||||
|
template, _ = sjson.Set(template, "requestType", "agent")
|
||||||
|
|
||||||
// Use real project ID from auth if available, otherwise generate random (legacy fallback)
|
// Use real project ID from auth if available, otherwise generate random (legacy fallback)
|
||||||
if projectID != "" {
|
if projectID != "" {
|
||||||
|
|||||||
@@ -184,7 +184,7 @@ func ConvertOpenAIRequestToAntigravity(modelName string, inputRawJSON []byte, _
|
|||||||
role := m.Get("role").String()
|
role := m.Get("role").String()
|
||||||
content := m.Get("content")
|
content := m.Get("content")
|
||||||
|
|
||||||
if role == "system" && len(arr) > 1 {
|
if (role == "system" || role == "developer") && len(arr) > 1 {
|
||||||
// system -> request.systemInstruction as a user message style
|
// system -> request.systemInstruction as a user message style
|
||||||
if content.Type == gjson.String {
|
if content.Type == gjson.String {
|
||||||
out, _ = sjson.SetBytes(out, "request.systemInstruction.role", "user")
|
out, _ = sjson.SetBytes(out, "request.systemInstruction.role", "user")
|
||||||
@@ -201,7 +201,7 @@ func ConvertOpenAIRequestToAntigravity(modelName string, inputRawJSON []byte, _
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if role == "user" || (role == "system" && len(arr) == 1) {
|
} else if role == "user" || ((role == "system" || role == "developer") && len(arr) == 1) {
|
||||||
// Build single user content node to avoid splitting into multiple contents
|
// Build single user content node to avoid splitting into multiple contents
|
||||||
node := []byte(`{"role":"user","parts":[]}`)
|
node := []byte(`{"role":"user","parts":[]}`)
|
||||||
if content.Type == gjson.String {
|
if content.Type == gjson.String {
|
||||||
@@ -223,6 +223,7 @@ func ConvertOpenAIRequestToAntigravity(modelName string, inputRawJSON []byte, _
|
|||||||
data := pieces[1][7:]
|
data := pieces[1][7:]
|
||||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
||||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
||||||
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".thoughtSignature", geminiCLIFunctionThoughtSignature)
|
||||||
p++
|
p++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -266,6 +267,7 @@ func ConvertOpenAIRequestToAntigravity(modelName string, inputRawJSON []byte, _
|
|||||||
data := pieces[1][7:]
|
data := pieces[1][7:]
|
||||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
||||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
||||||
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".thoughtSignature", geminiCLIFunctionThoughtSignature)
|
||||||
p++
|
p++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -152,7 +152,7 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
|
|||||||
role := m.Get("role").String()
|
role := m.Get("role").String()
|
||||||
content := m.Get("content")
|
content := m.Get("content")
|
||||||
|
|
||||||
if role == "system" && len(arr) > 1 {
|
if (role == "system" || role == "developer") && len(arr) > 1 {
|
||||||
// system -> request.systemInstruction as a user message style
|
// system -> request.systemInstruction as a user message style
|
||||||
if content.Type == gjson.String {
|
if content.Type == gjson.String {
|
||||||
out, _ = sjson.SetBytes(out, "request.systemInstruction.role", "user")
|
out, _ = sjson.SetBytes(out, "request.systemInstruction.role", "user")
|
||||||
@@ -169,7 +169,7 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if role == "user" || (role == "system" && len(arr) == 1) {
|
} else if role == "user" || ((role == "system" || role == "developer") && len(arr) == 1) {
|
||||||
// Build single user content node to avoid splitting into multiple contents
|
// Build single user content node to avoid splitting into multiple contents
|
||||||
node := []byte(`{"role":"user","parts":[]}`)
|
node := []byte(`{"role":"user","parts":[]}`)
|
||||||
if content.Type == gjson.String {
|
if content.Type == gjson.String {
|
||||||
@@ -191,6 +191,7 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
|
|||||||
data := pieces[1][7:]
|
data := pieces[1][7:]
|
||||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
||||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
||||||
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".thoughtSignature", geminiCLIFunctionThoughtSignature)
|
||||||
p++
|
p++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -236,6 +237,7 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
|
|||||||
data := pieces[1][7:]
|
data := pieces[1][7:]
|
||||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
||||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
||||||
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".thoughtSignature", geminiCLIFunctionThoughtSignature)
|
||||||
p++
|
p++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
|
|||||||
role := m.Get("role").String()
|
role := m.Get("role").String()
|
||||||
content := m.Get("content")
|
content := m.Get("content")
|
||||||
|
|
||||||
if role == "system" && len(arr) > 1 {
|
if (role == "system" || role == "developer") && len(arr) > 1 {
|
||||||
// system -> system_instruction as a user message style
|
// system -> system_instruction as a user message style
|
||||||
if content.Type == gjson.String {
|
if content.Type == gjson.String {
|
||||||
out, _ = sjson.SetBytes(out, "system_instruction.role", "user")
|
out, _ = sjson.SetBytes(out, "system_instruction.role", "user")
|
||||||
@@ -187,7 +187,7 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if role == "user" || (role == "system" && len(arr) == 1) {
|
} else if role == "user" || ((role == "system" || role == "developer") && len(arr) == 1) {
|
||||||
// Build single user content node to avoid splitting into multiple contents
|
// Build single user content node to avoid splitting into multiple contents
|
||||||
node := []byte(`{"role":"user","parts":[]}`)
|
node := []byte(`{"role":"user","parts":[]}`)
|
||||||
if content.Type == gjson.String {
|
if content.Type == gjson.String {
|
||||||
@@ -209,6 +209,7 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
|
|||||||
data := pieces[1][7:]
|
data := pieces[1][7:]
|
||||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
||||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
||||||
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".thoughtSignature", geminiFunctionThoughtSignature)
|
||||||
p++
|
p++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -253,6 +254,7 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
|
|||||||
data := pieces[1][7:]
|
data := pieces[1][7:]
|
||||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
||||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
||||||
|
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".thoughtSignature", geminiFunctionThoughtSignature)
|
||||||
p++
|
p++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,10 +71,13 @@ func ApplyGeminiThinkingConfig(body []byte, budget *int, includeThoughts *bool)
|
|||||||
incl = &defaultInclude
|
incl = &defaultInclude
|
||||||
}
|
}
|
||||||
if incl != nil {
|
if incl != nil {
|
||||||
valuePath := "generationConfig.thinkingConfig.include_thoughts"
|
if !gjson.GetBytes(updated, "generationConfig.thinkingConfig.includeThoughts").Exists() &&
|
||||||
rewritten, err := sjson.SetBytes(updated, valuePath, *incl)
|
!gjson.GetBytes(updated, "generationConfig.thinkingConfig.include_thoughts").Exists() {
|
||||||
if err == nil {
|
valuePath := "generationConfig.thinkingConfig.include_thoughts"
|
||||||
updated = rewritten
|
rewritten, err := sjson.SetBytes(updated, valuePath, *incl)
|
||||||
|
if err == nil {
|
||||||
|
updated = rewritten
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return updated
|
return updated
|
||||||
@@ -99,10 +102,13 @@ func ApplyGeminiCLIThinkingConfig(body []byte, budget *int, includeThoughts *boo
|
|||||||
incl = &defaultInclude
|
incl = &defaultInclude
|
||||||
}
|
}
|
||||||
if incl != nil {
|
if incl != nil {
|
||||||
valuePath := "request.generationConfig.thinkingConfig.include_thoughts"
|
if !gjson.GetBytes(updated, "request.generationConfig.thinkingConfig.includeThoughts").Exists() &&
|
||||||
rewritten, err := sjson.SetBytes(updated, valuePath, *incl)
|
!gjson.GetBytes(updated, "request.generationConfig.thinkingConfig.include_thoughts").Exists() {
|
||||||
if err == nil {
|
valuePath := "request.generationConfig.thinkingConfig.include_thoughts"
|
||||||
updated = rewritten
|
rewritten, err := sjson.SetBytes(updated, valuePath, *incl)
|
||||||
|
if err == nil {
|
||||||
|
updated = rewritten
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return updated
|
return updated
|
||||||
@@ -130,15 +136,15 @@ func ApplyGeminiThinkingLevel(body []byte, level string, includeThoughts *bool)
|
|||||||
incl = &defaultInclude
|
incl = &defaultInclude
|
||||||
}
|
}
|
||||||
if incl != nil {
|
if incl != nil {
|
||||||
valuePath := "generationConfig.thinkingConfig.includeThoughts"
|
if !gjson.GetBytes(updated, "generationConfig.thinkingConfig.includeThoughts").Exists() &&
|
||||||
rewritten, err := sjson.SetBytes(updated, valuePath, *incl)
|
!gjson.GetBytes(updated, "generationConfig.thinkingConfig.include_thoughts").Exists() {
|
||||||
if err == nil {
|
valuePath := "generationConfig.thinkingConfig.includeThoughts"
|
||||||
updated = rewritten
|
rewritten, err := sjson.SetBytes(updated, valuePath, *incl)
|
||||||
|
if err == nil {
|
||||||
|
updated = rewritten
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if it := gjson.GetBytes(body, "generationConfig.thinkingConfig.include_thoughts"); it.Exists() {
|
|
||||||
updated, _ = sjson.DeleteBytes(updated, "generationConfig.thinkingConfig.include_thoughts")
|
|
||||||
}
|
|
||||||
if tb := gjson.GetBytes(body, "generationConfig.thinkingConfig.thinkingBudget"); tb.Exists() {
|
if tb := gjson.GetBytes(body, "generationConfig.thinkingConfig.thinkingBudget"); tb.Exists() {
|
||||||
updated, _ = sjson.DeleteBytes(updated, "generationConfig.thinkingConfig.thinkingBudget")
|
updated, _ = sjson.DeleteBytes(updated, "generationConfig.thinkingConfig.thinkingBudget")
|
||||||
}
|
}
|
||||||
@@ -167,15 +173,15 @@ func ApplyGeminiCLIThinkingLevel(body []byte, level string, includeThoughts *boo
|
|||||||
incl = &defaultInclude
|
incl = &defaultInclude
|
||||||
}
|
}
|
||||||
if incl != nil {
|
if incl != nil {
|
||||||
valuePath := "request.generationConfig.thinkingConfig.includeThoughts"
|
if !gjson.GetBytes(updated, "request.generationConfig.thinkingConfig.includeThoughts").Exists() &&
|
||||||
rewritten, err := sjson.SetBytes(updated, valuePath, *incl)
|
!gjson.GetBytes(updated, "request.generationConfig.thinkingConfig.include_thoughts").Exists() {
|
||||||
if err == nil {
|
valuePath := "request.generationConfig.thinkingConfig.includeThoughts"
|
||||||
updated = rewritten
|
rewritten, err := sjson.SetBytes(updated, valuePath, *incl)
|
||||||
|
if err == nil {
|
||||||
|
updated = rewritten
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if it := gjson.GetBytes(body, "request.generationConfig.thinkingConfig.include_thoughts"); it.Exists() {
|
|
||||||
updated, _ = sjson.DeleteBytes(updated, "request.generationConfig.thinkingConfig.include_thoughts")
|
|
||||||
}
|
|
||||||
if tb := gjson.GetBytes(body, "request.generationConfig.thinkingConfig.thinkingBudget"); tb.Exists() {
|
if tb := gjson.GetBytes(body, "request.generationConfig.thinkingConfig.thinkingBudget"); tb.Exists() {
|
||||||
updated, _ = sjson.DeleteBytes(updated, "request.generationConfig.thinkingConfig.thinkingBudget")
|
updated, _ = sjson.DeleteBytes(updated, "request.generationConfig.thinkingConfig.thinkingBudget")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ type ManagementTokenRequester interface {
|
|||||||
RequestIFlowToken(*gin.Context)
|
RequestIFlowToken(*gin.Context)
|
||||||
RequestIFlowCookieToken(*gin.Context)
|
RequestIFlowCookieToken(*gin.Context)
|
||||||
GetAuthStatus(c *gin.Context)
|
GetAuthStatus(c *gin.Context)
|
||||||
|
PostOAuthCallback(c *gin.Context)
|
||||||
}
|
}
|
||||||
|
|
||||||
type managementTokenRequester struct {
|
type managementTokenRequester struct {
|
||||||
@@ -65,3 +66,7 @@ func (m *managementTokenRequester) RequestIFlowCookieToken(c *gin.Context) {
|
|||||||
func (m *managementTokenRequester) GetAuthStatus(c *gin.Context) {
|
func (m *managementTokenRequester) GetAuthStatus(c *gin.Context) {
|
||||||
m.handler.GetAuthStatus(c)
|
m.handler.GetAuthStatus(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *managementTokenRequester) PostOAuthCallback(c *gin.Context) {
|
||||||
|
m.handler.PostOAuthCallback(c)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user