Merge pull request #3254 from sususu98/fix/antigravity-project-id-onboard
fix: require antigravity project id
This commit is contained in:
@@ -45,6 +45,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)
|
||||
@@ -182,6 +189,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.
|
||||
@@ -1365,6 +1374,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)
|
||||
@@ -1453,6 +1473,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)
|
||||
@@ -1539,6 +1570,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 {
|
||||
@@ -1630,6 +1672,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)
|
||||
ctx = coreusage.WithRequestedModelAlias(ctx, alias)
|
||||
@@ -3667,6 +3765,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 {
|
||||
@@ -3709,6 +3812,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 {
|
||||
|
||||
Reference in New Issue
Block a user