fix: require antigravity project id
This commit is contained in:
@@ -177,12 +177,15 @@ waitForCallback:
|
||||
if accessToken != "" {
|
||||
fetchedProjectID, errProject := authSvc.FetchProjectID(ctx, accessToken)
|
||||
if errProject != nil {
|
||||
log.Warnf("antigravity: failed to fetch project ID: %v", errProject)
|
||||
return nil, fmt.Errorf("antigravity: failed to fetch project ID: %w", errProject)
|
||||
} else {
|
||||
projectID = fetchedProjectID
|
||||
log.Infof("antigravity: obtained project ID %s", projectID)
|
||||
}
|
||||
}
|
||||
if strings.TrimSpace(projectID) == "" {
|
||||
return nil, fmt.Errorf("antigravity: project ID discovery returned empty project")
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
metadata := map[string]any{
|
||||
|
||||
@@ -44,6 +44,13 @@ type ProviderExecutor interface {
|
||||
HttpRequest(ctx context.Context, auth *Auth, req *http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
// RequestAuthPreparer lets an executor update missing auth metadata immediately
|
||||
// before a request. Manager serializes and persists returned updates.
|
||||
type RequestAuthPreparer interface {
|
||||
ShouldPrepareRequestAuth(auth *Auth) bool
|
||||
PrepareRequestAuth(ctx context.Context, auth *Auth) (*Auth, error)
|
||||
}
|
||||
|
||||
// ExecutionSessionCloser allows executors to release per-session runtime resources.
|
||||
type ExecutionSessionCloser interface {
|
||||
CloseExecutionSession(sessionID string)
|
||||
@@ -177,6 +184,8 @@ type Manager struct {
|
||||
// Auto refresh state
|
||||
refreshCancel context.CancelFunc
|
||||
refreshLoop *authAutoRefreshLoop
|
||||
|
||||
requestPrepareLocks sync.Map
|
||||
}
|
||||
|
||||
// NewManager constructs a manager with optional custom selector and hook.
|
||||
@@ -1328,6 +1337,17 @@ func (m *Manager) executeMixedOnce(ctx context.Context, providers []string, req
|
||||
continue
|
||||
}
|
||||
attempted[auth.ID] = struct{}{}
|
||||
var errPrepare error
|
||||
auth, errPrepare = m.prepareRequestAuth(execCtx, executor, auth)
|
||||
if errPrepare != nil {
|
||||
result := Result{AuthID: auth.ID, Provider: provider, Model: routeModel, Success: false, Error: &Error{Message: errPrepare.Error()}}
|
||||
if se, ok := errors.AsType[cliproxyexecutor.StatusError](errPrepare); ok && se != nil {
|
||||
result.Error.HTTPStatus = se.StatusCode()
|
||||
}
|
||||
m.MarkResult(execCtx, result)
|
||||
lastErr = errPrepare
|
||||
continue
|
||||
}
|
||||
var authErr error
|
||||
for _, upstreamModel := range models {
|
||||
resultModel := m.stateModelForExecution(auth, routeModel, upstreamModel, pooled)
|
||||
@@ -1407,6 +1427,17 @@ func (m *Manager) executeCountMixedOnce(ctx context.Context, providers []string,
|
||||
continue
|
||||
}
|
||||
attempted[auth.ID] = struct{}{}
|
||||
var errPrepare error
|
||||
auth, errPrepare = m.prepareRequestAuth(execCtx, executor, auth)
|
||||
if errPrepare != nil {
|
||||
result := Result{AuthID: auth.ID, Provider: provider, Model: routeModel, Success: false, Error: &Error{Message: errPrepare.Error()}}
|
||||
if se, ok := errors.AsType[cliproxyexecutor.StatusError](errPrepare); ok && se != nil {
|
||||
result.Error.HTTPStatus = se.StatusCode()
|
||||
}
|
||||
m.MarkResult(execCtx, result)
|
||||
lastErr = errPrepare
|
||||
continue
|
||||
}
|
||||
var authErr error
|
||||
for _, upstreamModel := range models {
|
||||
resultModel := m.stateModelForExecution(auth, routeModel, upstreamModel, pooled)
|
||||
@@ -1484,6 +1515,17 @@ func (m *Manager) executeStreamMixedOnce(ctx context.Context, providers []string
|
||||
continue
|
||||
}
|
||||
attempted[auth.ID] = struct{}{}
|
||||
var errPrepare error
|
||||
auth, errPrepare = m.prepareRequestAuth(execCtx, executor, auth)
|
||||
if errPrepare != nil {
|
||||
result := Result{AuthID: auth.ID, Provider: provider, Model: routeModel, Success: false, Error: &Error{Message: errPrepare.Error()}}
|
||||
if se, ok := errors.AsType[cliproxyexecutor.StatusError](errPrepare); ok && se != nil {
|
||||
result.Error.HTTPStatus = se.StatusCode()
|
||||
}
|
||||
m.MarkResult(execCtx, result)
|
||||
lastErr = errPrepare
|
||||
continue
|
||||
}
|
||||
streamResult, errStream := m.executeStreamWithModelPool(execCtx, executor, auth, provider, req, opts, routeModel, models, pooled)
|
||||
if errStream != nil {
|
||||
if errCtx := execCtx.Err(); errCtx != nil {
|
||||
@@ -1538,6 +1580,62 @@ func hasRequestedModelMetadata(meta map[string]any) bool {
|
||||
}
|
||||
}
|
||||
|
||||
type requestAuthPrepareLock struct {
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (m *Manager) prepareRequestAuth(ctx context.Context, executor ProviderExecutor, auth *Auth) (*Auth, error) {
|
||||
if m == nil || executor == nil || auth == nil {
|
||||
return auth, nil
|
||||
}
|
||||
preparer, ok := executor.(RequestAuthPreparer)
|
||||
if !ok || preparer == nil || !preparer.ShouldPrepareRequestAuth(auth) {
|
||||
return auth, nil
|
||||
}
|
||||
|
||||
id := strings.TrimSpace(auth.ID)
|
||||
if id == "" {
|
||||
return preparer.PrepareRequestAuth(ctx, auth.Clone())
|
||||
}
|
||||
|
||||
lockValue, _ := m.requestPrepareLocks.LoadOrStore(id, &requestAuthPrepareLock{})
|
||||
lock, ok := lockValue.(*requestAuthPrepareLock)
|
||||
if !ok || lock == nil {
|
||||
return preparer.PrepareRequestAuth(ctx, auth.Clone())
|
||||
}
|
||||
|
||||
lock.mu.Lock()
|
||||
defer lock.mu.Unlock()
|
||||
|
||||
target := auth.Clone()
|
||||
m.mu.RLock()
|
||||
if current := m.auths[id]; current != nil {
|
||||
target = current.Clone()
|
||||
}
|
||||
m.mu.RUnlock()
|
||||
|
||||
if !preparer.ShouldPrepareRequestAuth(target) {
|
||||
return target, nil
|
||||
}
|
||||
|
||||
updated, errPrepare := preparer.PrepareRequestAuth(ctx, target)
|
||||
if errPrepare != nil {
|
||||
return auth, errPrepare
|
||||
}
|
||||
if updated == nil {
|
||||
return target, nil
|
||||
}
|
||||
|
||||
saved, errUpdate := m.Update(ctx, updated)
|
||||
if errUpdate != nil {
|
||||
return updated, errUpdate
|
||||
}
|
||||
if saved != nil {
|
||||
return saved, nil
|
||||
}
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
func contextWithRequestedModelAlias(ctx context.Context, opts cliproxyexecutor.Options, fallback string) context.Context {
|
||||
alias := requestedModelAliasFromOptions(opts, fallback)
|
||||
return coreusage.WithRequestedModelAlias(ctx, alias)
|
||||
@@ -3131,6 +3229,11 @@ func (m *Manager) tryAntigravityCreditsExecute(ctx context.Context, req cliproxy
|
||||
}
|
||||
creditsOpts := ensureRequestedModelMetadata(opts, routeModel)
|
||||
creditsCtx = contextWithRequestedModelAlias(creditsCtx, creditsOpts, routeModel)
|
||||
preparedAuth, errPrepare := m.prepareRequestAuth(creditsCtx, c.executor, c.auth)
|
||||
if errPrepare != nil {
|
||||
continue
|
||||
}
|
||||
c.auth = preparedAuth
|
||||
publishSelectedAuthMetadata(creditsOpts.Metadata, c.auth.ID)
|
||||
models := m.executionModelCandidates(c.auth, routeModel)
|
||||
if len(models) == 0 {
|
||||
@@ -3173,6 +3276,11 @@ func (m *Manager) tryAntigravityCreditsExecuteStream(ctx context.Context, req cl
|
||||
creditsCtx = context.WithValue(creditsCtx, "cliproxy.roundtripper", rt)
|
||||
}
|
||||
creditsOpts := ensureRequestedModelMetadata(opts, routeModel)
|
||||
preparedAuth, errPrepare := m.prepareRequestAuth(creditsCtx, c.executor, c.auth)
|
||||
if errPrepare != nil {
|
||||
continue
|
||||
}
|
||||
c.auth = preparedAuth
|
||||
publishSelectedAuthMetadata(creditsOpts.Metadata, c.auth.ID)
|
||||
models := m.executionModelCandidates(c.auth, routeModel)
|
||||
if len(models) == 0 {
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
|
||||
)
|
||||
|
||||
type requestPrepareStore struct {
|
||||
saveCount atomic.Int32
|
||||
mu sync.Mutex
|
||||
last *Auth
|
||||
}
|
||||
|
||||
func (s *requestPrepareStore) List(context.Context) ([]*Auth, error) { return nil, nil }
|
||||
|
||||
func (s *requestPrepareStore) Save(_ context.Context, auth *Auth) (string, error) {
|
||||
s.saveCount.Add(1)
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.last = auth.Clone()
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (s *requestPrepareStore) Delete(context.Context, string) error { return nil }
|
||||
|
||||
func (s *requestPrepareStore) lastAuth() *Auth {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.last.Clone()
|
||||
}
|
||||
|
||||
type requestPrepareExecutor struct {
|
||||
prepareCalls atomic.Int32
|
||||
executeCalls atomic.Int32
|
||||
}
|
||||
|
||||
func (e *requestPrepareExecutor) Identifier() string { return "antigravity" }
|
||||
|
||||
func (e *requestPrepareExecutor) ShouldPrepareRequestAuth(auth *Auth) bool {
|
||||
return auth == nil || auth.Metadata == nil || testStringValue(auth.Metadata["project_id"]) == ""
|
||||
}
|
||||
|
||||
func (e *requestPrepareExecutor) PrepareRequestAuth(_ context.Context, auth *Auth) (*Auth, error) {
|
||||
e.prepareCalls.Add(1)
|
||||
updated := auth.Clone()
|
||||
if updated.Metadata == nil {
|
||||
updated.Metadata = make(map[string]any)
|
||||
}
|
||||
updated.Metadata["project_id"] = "prepared-project"
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
func (e *requestPrepareExecutor) Execute(_ context.Context, auth *Auth, _ cliproxyexecutor.Request, _ cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
|
||||
e.executeCalls.Add(1)
|
||||
if got := testStringValue(auth.Metadata["project_id"]); got != "prepared-project" {
|
||||
return cliproxyexecutor.Response{}, &Error{HTTPStatus: http.StatusBadRequest, Message: "missing prepared project"}
|
||||
}
|
||||
return cliproxyexecutor.Response{Payload: []byte("ok")}, nil
|
||||
}
|
||||
|
||||
func (e *requestPrepareExecutor) ExecuteStream(context.Context, *Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) {
|
||||
return nil, &Error{HTTPStatus: http.StatusNotImplemented, Message: "stream not implemented"}
|
||||
}
|
||||
|
||||
func (e *requestPrepareExecutor) Refresh(_ context.Context, auth *Auth) (*Auth, error) {
|
||||
return auth, nil
|
||||
}
|
||||
|
||||
func (e *requestPrepareExecutor) CountTokens(context.Context, *Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
|
||||
return cliproxyexecutor.Response{}, &Error{HTTPStatus: http.StatusNotImplemented, Message: "count not implemented"}
|
||||
}
|
||||
|
||||
func (e *requestPrepareExecutor) HttpRequest(context.Context, *Auth, *http.Request) (*http.Response, error) {
|
||||
return nil, &Error{HTTPStatus: http.StatusNotImplemented, Message: "http not implemented"}
|
||||
}
|
||||
|
||||
func TestManagerExecute_PreparesAndPersistsMissingRequestAuthMetadata(t *testing.T) {
|
||||
const model = "gemini-3.1-pro"
|
||||
store := &requestPrepareStore{}
|
||||
executor := &requestPrepareExecutor{}
|
||||
manager := NewManager(store, nil, nil)
|
||||
manager.RegisterExecutor(executor)
|
||||
|
||||
auth := &Auth{
|
||||
ID: "auth-request-prepare",
|
||||
Provider: "antigravity",
|
||||
Metadata: map[string]any{"access_token": "token"},
|
||||
}
|
||||
if _, errRegister := manager.Register(WithSkipPersist(context.Background()), auth); errRegister != nil {
|
||||
t.Fatalf("register auth: %v", errRegister)
|
||||
}
|
||||
registry.GetGlobalRegistry().RegisterClient(auth.ID, "antigravity", []*registry.ModelInfo{{ID: model}})
|
||||
t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient(auth.ID) })
|
||||
|
||||
resp, errExecute := manager.Execute(context.Background(), []string{"antigravity"}, cliproxyexecutor.Request{Model: model}, cliproxyexecutor.Options{})
|
||||
if errExecute != nil {
|
||||
t.Fatalf("Execute error: %v", errExecute)
|
||||
}
|
||||
if string(resp.Payload) != "ok" {
|
||||
t.Fatalf("payload = %q, want ok", string(resp.Payload))
|
||||
}
|
||||
if got := executor.prepareCalls.Load(); got != 1 {
|
||||
t.Fatalf("prepare calls = %d, want 1", got)
|
||||
}
|
||||
if got := store.saveCount.Load(); got < 1 {
|
||||
t.Fatalf("save count = %d, want at least 1", got)
|
||||
}
|
||||
if got := testStringValue(store.lastAuth().Metadata["project_id"]); got != "prepared-project" {
|
||||
t.Fatalf("persisted project_id = %q, want prepared-project", got)
|
||||
}
|
||||
current, ok := manager.GetByID(auth.ID)
|
||||
if !ok {
|
||||
t.Fatal("expected auth in manager")
|
||||
}
|
||||
if got := testStringValue(current.Metadata["project_id"]); got != "prepared-project" {
|
||||
t.Fatalf("manager project_id = %q, want prepared-project", got)
|
||||
}
|
||||
|
||||
if _, errExecute = manager.Execute(context.Background(), []string{"antigravity"}, cliproxyexecutor.Request{Model: model}, cliproxyexecutor.Options{}); errExecute != nil {
|
||||
t.Fatalf("second Execute error: %v", errExecute)
|
||||
}
|
||||
if got := executor.prepareCalls.Load(); got != 1 {
|
||||
t.Fatalf("prepare calls after second execute = %d, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func testStringValue(value any) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
switch typed := value.(type) {
|
||||
case string:
|
||||
return strings.TrimSpace(typed)
|
||||
case []byte:
|
||||
return strings.TrimSpace(string(typed))
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user