Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion internal/adapter/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"regexp"
"slices"
"strings"
"sync/atomic"
"time"
"unicode"
"unicode/utf8"
Expand Down Expand Up @@ -48,6 +49,8 @@ func (e adapterEmitError) Unwrap() error { return e.err }

type accountContextError struct {
err *gemini.ProtocolError
// tokens 是新会话尝试需要的输入 token 数,续接已有会话时为 0
tokens int
}

func (e *accountContextError) Error() string { return e.err.Error() }
Expand Down Expand Up @@ -433,6 +436,9 @@ func runGenerationAttempts(
}

excludedAccountIDs := make(map[string]struct{})
// promptTokens 是新会话尝试因窗口不足失败时需要的输入 token 数,之后选号跳过上下文窗口装不下它的账号
var promptTokens atomic.Int64
ctx = balancer.ContextWithPromptTokens(ctx, &promptTokens)
availableAccounts := pool.Size()
if availableAccounts == 0 {
return generationResult{}, "", noAccountError(ctx, pool, modelName)
Expand Down Expand Up @@ -478,6 +484,7 @@ func runGenerationAttempts(
clear(excludedAccountIDs)
continue
}
attemptRegenerating := regenerating
regenerating = false
if client == nil {
if sessionKey != "" && !newSession && accountID != "" {
Expand Down Expand Up @@ -521,6 +528,10 @@ func runGenerationAttempts(
if lastErr == nil || !isAccountContextError(err) {
lastErr, lastAccountID = err, accountID
}
if tokens := accountContextTokens(err); tokens > int(promptTokens.Load()) {
promptTokens.Store(int64(tokens))
trace.Progress("WARN", accountID, "排除上下文窗口装不下请求的账号", "需要", tokens)
}
if ctx.Err() != nil {
return generationResult{}, accountID, err
}
Expand Down Expand Up @@ -554,6 +565,7 @@ func runGenerationAttempts(
}
trace.AttemptFailed(accountID, upstreamStatus(err), err, true)
excludedAccountIDs[accountID] = struct{}{}
regenerating = attemptRegenerating
continue
}
if (gemini.TransientError(err) || isOutputMismatchError(err)) && (sessionKey == "" || newSession) && regenerated < regenerations && !visibleOutput() {
Expand Down Expand Up @@ -667,10 +679,14 @@ func runGenerationWithClient(
}
promptTokens := contextTokens + tokencount.Content(prompt) + mediaTokens(files)
if contextWindow := client.ContextWindow(); contextWindow > 0 && promptTokens > contextWindow {
return generationResult{}, &accountContextError{err: &gemini.ProtocolError{
contextErr := &accountContextError{err: &gemini.ProtocolError{
HTTPStatus: 413,
Message: fmt.Sprintf("账号上下文窗口为 %d tokens,当前请求需要 %d tokens", contextWindow, promptTokens),
}}
if !continuation {
contextErr.tokens = promptTokens
}
return generationResult{}, contextErr
}
feature := gemini.Feature(0)
if media.requireImage {
Expand Down Expand Up @@ -964,6 +980,15 @@ func isAccountContextError(err error) bool {
return errors.As(err, &contextErr)
}

// accountContextTokens 返回新会话尝试因账号上下文窗口不足失败时需要的输入 token 数,其余错误为 0
func accountContextTokens(err error) int {
var contextErr *accountContextError
if errors.As(err, &contextErr) {
return contextErr.tokens
}
return 0
}

// publicModelID 返回响应中的模型 ID:别名取映射目标,改名前的旧 ID 取当前 ID,gemini-default 在各就绪账号(没有账号时为访客会话)的默认模型相同时取该模型
func publicModelID(pool *balancer.AccountPool, requested string) string {
id := gemini.CanonicalModelID(config.MapModel(requested))
Expand Down
20 changes: 17 additions & 3 deletions internal/balancer/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"maps"
"slices"
"sync/atomic"
"time"

"github.com/Mag1cFall/Gemini-Web2API/internal/gemini"
Expand All @@ -18,6 +19,14 @@ func ContextWithAPIKey(ctx context.Context, key string) context.Context {
return context.WithValue(ctx, apiKeyContextKey{}, key)
}

// promptTokensContextKey 是请求上下文中请求输入 token 数的键
type promptTokensContextKey struct{}

// ContextWithPromptTokens 返回带有请求输入 token 数的上下文,tokens 为正时账号池不选上下文窗口装不下它的账号与访客会话
func ContextWithPromptTokens(ctx context.Context, tokens *atomic.Int64) context.Context {
return context.WithValue(ctx, promptTokensContextKey{}, tokens)
}

// SetAPIKeyAccounts 设置各 API key 可用的账号 ID,未列出的 key 可用全部账号
func (p *AccountPool) SetAPIKeyAccounts(accounts map[string][]string) {
p.mu.Lock()
Expand All @@ -26,7 +35,7 @@ func (p *AccountPool) SetAPIKeyAccounts(accounts map[string][]string) {
p.notifyLocked()
}

// scopeExcluded 返回并入请求范围之外账号与访客会话的排除集合:请求指定了处理账号时只留该账号,否则按 API key 与目标模型的绑定排除,都没有绑定时原样返回
// scopeExcluded 返回并入请求范围之外账号与访客会话的排除集合:请求指定了处理账号时只留该账号,否则按 API key 与目标模型的绑定和上下文窗口能否装下请求排除,都不排除时原样返回
func (p *AccountPool) scopeExcluded(ctx context.Context, modelID string, excluded map[string]struct{}) map[string]struct{} {
key, _ := ctx.Value(apiKeyContextKey{}).(string)
p.mu.Lock()
Expand All @@ -46,15 +55,20 @@ func (p *AccountPool) scopeExcluded(ctx context.Context, modelID string, exclude
keyScope, keyBound := p.apiKeyAccounts[key]
keyBound = keyBound && key != ""
modelScope, modelBound := p.modelBinding(modelID)
if !keyBound && !modelBound {
var tokens int64
if value, ok := ctx.Value(promptTokensContextKey{}).(*atomic.Int64); ok {
tokens = value.Load()
}
if !keyBound && !modelBound && tokens == 0 {
return excluded
}
result := maps.Clone(excluded)
if result == nil {
result = make(map[string]struct{})
}
for _, entry := range p.entries {
if keyBound && !slices.Contains(keyScope, entry.AccountID) || modelBound && !slices.Contains(modelScope, entry.AccountID) {
narrow := entry.Client != nil && entry.Client.ContextWindow() > 0 && int64(entry.Client.ContextWindow()) < tokens
if keyBound && !slices.Contains(keyScope, entry.AccountID) || modelBound && !slices.Contains(modelScope, entry.AccountID) || narrow {
result[entry.AccountID] = struct{}{}
}
}
Expand Down