From cb2e6ea73253a67574d92fac123d0bd118b6e918 Mon Sep 17 00:00:00 2001 From: soleil1996 Date: Wed, 18 Nov 2026 17:30:37 +0800 Subject: [PATCH 1/2] fix(adapter): skip accounts whose context window cannot fit the request --- internal/adapter/stream.go | 26 ++++++++++- internal/balancer/balancer.go | 29 ++++++++++++ internal/balancer/context_window_test.go | 59 ++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 internal/balancer/context_window_test.go diff --git a/internal/adapter/stream.go b/internal/adapter/stream.go index 302144d..907feb2 100644 --- a/internal/adapter/stream.go +++ b/internal/adapter/stream.go @@ -48,6 +48,8 @@ func (e adapterEmitError) Unwrap() error { return e.err } type accountContextError struct { err *gemini.ProtocolError + // tokens 是这次请求需要的输入 token 数,账号池据此排除上下文窗口同样装不下请求的其他账号 + tokens int } func (e *accountContextError) Error() string { return e.err.Error() } @@ -541,6 +543,7 @@ func runGenerationAttempts( if rebuildable && sessionKey != "" && !newSession && (isAccountContextError(err) || errors.Is(err, gemini.ErrGuestAttachments) || retryableAccountError(err) && !visibleOutput()) { trace.AttemptFailed(accountID, upstreamStatus(err), err, true) excludedAccountIDs[accountID] = struct{}{} + excludeNarrowAccounts(trace, pool, excludedAccountIDs, accountID, err) if (gemini.TransientError(err) || isOutputMismatchError(err)) && !visibleOutput() && regenerated < regenerations { regenerated++ regenerating = true @@ -554,6 +557,7 @@ func runGenerationAttempts( } trace.AttemptFailed(accountID, upstreamStatus(err), err, true) excludedAccountIDs[accountID] = struct{}{} + excludeNarrowAccounts(trace, pool, excludedAccountIDs, accountID, err) continue } if (gemini.TransientError(err) || isOutputMismatchError(err)) && (sessionKey == "" || newSession) && regenerated < regenerations && !visibleOutput() { @@ -667,7 +671,7 @@ func runGenerationWithClient( } promptTokens := contextTokens + tokencount.Content(prompt) + mediaTokens(files) if contextWindow := client.ContextWindow(); contextWindow > 0 && promptTokens > contextWindow { - return generationResult{}, &accountContextError{err: &gemini.ProtocolError{ + return generationResult{}, &accountContextError{tokens: promptTokens, err: &gemini.ProtocolError{ HTTPStatus: 413, Message: fmt.Sprintf("账号上下文窗口为 %d tokens,当前请求需要 %d tokens", contextWindow, promptTokens), }} @@ -964,6 +968,26 @@ func isAccountContextError(err error) bool { return errors.As(err, &contextErr) } +// accountContextTokens 返回账号上下文窗口不足时这次请求需要的输入 token 数 +func accountContextTokens(err error) int { + var contextErr *accountContextError + if errors.As(err, &contextErr) { + return contextErr.tokens + } + return 0 +} + +// excludeNarrowAccounts 在账号因上下文窗口不足拒绝请求时把上下文窗口同样装不下这次请求的账号与访客会话一并排除,避免向它们重复上传附件并徒劳发起请求 +func excludeNarrowAccounts(trace *logging.Request, pool *balancer.AccountPool, excludedAccountIDs map[string]struct{}, accountID string, err error) { + tokens := accountContextTokens(err) + if tokens <= 0 { + return + } + if added := pool.ExcludeNarrowContext(excludedAccountIDs, tokens); len(added) > 0 { + trace.Progress("WARN", accountID, "排除上下文窗口装不下请求的账号", "需要", tokens, "账号", strings.Join(added, ",")) + } +} + // publicModelID 返回响应中的模型 ID:别名取映射目标,改名前的旧 ID 取当前 ID,gemini-default 在各就绪账号(没有账号时为访客会话)的默认模型相同时取该模型 func publicModelID(pool *balancer.AccountPool, requested string) string { id := gemini.CanonicalModelID(config.MapModel(requested)) diff --git a/internal/balancer/balancer.go b/internal/balancer/balancer.go index e0cdfae..d25a803 100644 --- a/internal/balancer/balancer.go +++ b/internal/balancer/balancer.go @@ -1147,6 +1147,35 @@ func errorText(err error) string { return logging.SanitizeError(err.Error()) } +// ExcludeNarrowContext 把上下文窗口装不下 tokens 的账号与访客会话加入排除集,返回新增的账号 ID:账号因上下文窗口不足拒绝请求后,调用方据此不再向同样装不下这次请求的其他账号重复上传附件并徒劳发起请求,窗口未知(0)的账号不排除 +func (p *AccountPool) ExcludeNarrowContext(excludedAccountIDs map[string]struct{}, tokens int) []string { + if tokens <= 0 { + return nil + } + p.mu.Lock() + defer p.mu.Unlock() + var added []string + for _, entry := range p.entries { + if entry.Client == nil { + continue + } + if _, excluded := excludedAccountIDs[entry.AccountID]; excluded { + continue + } + if !contextTooNarrow(entry.Client.ContextWindow(), tokens) { + continue + } + excludedAccountIDs[entry.AccountID] = struct{}{} + added = append(added, entry.AccountID) + } + return added +} + +// contextTooNarrow 报告账号的上下文窗口是否装不下 tokens,窗口未知(0)时按未知处理 +func contextTooNarrow(window, tokens int) bool { + return window > 0 && tokens > 0 && window < tokens +} + // nextAvailable 优先在账号中选择,没有可调度的账号时使用访客会话 func (p *AccountPool) nextAvailable(now time.Time, modelID string, excludedAccountIDs map[string]struct{}) *accountEntry { if entry := p.nextFrom(now, modelID, excludedAccountIDs, false); entry != nil { diff --git a/internal/balancer/context_window_test.go b/internal/balancer/context_window_test.go new file mode 100644 index 0000000..17d8628 --- /dev/null +++ b/internal/balancer/context_window_test.go @@ -0,0 +1,59 @@ +package balancer + +import ( + "testing" + "time" +) + +// TestContextTooNarrow 验证上下文窗口与请求 token 数的比较:窗口小于请求时为真,两者相等时仍可装下,窗口未知(0)时按未知处理 +func TestContextTooNarrow(t *testing.T) { + cases := []struct { + window int + tokens int + narrow bool + }{ + {32768, 56508, true}, + {32768, 32768, false}, + {128 * 1024, 56508, false}, + {0, 56508, false}, + {32768, 0, false}, + } + for _, tc := range cases { + if got := contextTooNarrow(tc.window, tc.tokens); got != tc.narrow { + t.Fatalf("窗口 %d、请求 %d tokens:%v,应为 %v", tc.window, tc.tokens, got, tc.narrow) + } + } +} + +// TestExcludeNarrowContextKeepsUnknownWindow 验证排除装不下请求的账号时的边界:请求 token 数不为正时不改动,窗口未知的账号不排除,已排除的账号不重复加入 +func TestExcludeNarrowContextKeepsUnknownWindow(t *testing.T) { + pool := NewAccountPool(2*time.Minute, time.Minute) + pool.Register("account", AccountInfo{}, false) + pool.Register("guest", AccountInfo{Source: SourceGuest}, false) + pool.MarkReady("account") + pool.MarkReady("guest") + excluded := map[string]struct{}{"account": {}} + if added := pool.ExcludeNarrowContext(excluded, 0); len(added) != 0 { + t.Fatalf("请求 token 数为 0 时不应排除账号,实际排除 %v", added) + } + if added := pool.ExcludeNarrowContext(excluded, 56508); len(added) != 0 { + t.Fatalf("窗口未知的账号不应排除,实际排除 %v", added) + } + if len(excluded) != 1 { + t.Fatalf("排除集被改动:%v", excluded) + } +} + +// TestExcludeNarrowContextReportsAddedAccounts 验证排除装不下请求的账号时返回新增的账号 ID 并写入排除集,账号池没有排除集之外的改动 +func TestExcludeNarrowContextReportsAddedAccounts(t *testing.T) { + pool := NewAccountPool(2*time.Minute, time.Minute) + pool.Register("guest", AccountInfo{Source: SourceGuest}, false) + pool.MarkReady("guest") + excluded := map[string]struct{}{"other": {}} + if added := pool.ExcludeNarrowContext(excluded, 1); len(added) != 0 { + t.Fatalf("窗口未知的访客会话不应排除:%v", added) + } + if _, ok := excluded["other"]; !ok || len(excluded) != 1 { + t.Fatalf("排除集应只含原有的账号:%v", excluded) + } +} From f59effbce5fa79e34d91a48a5a74bd489a78d040 Mon Sep 17 00:00:00 2001 From: Mag1cFall Date: Sat, 21 Nov 2026 11:07:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E7=AA=97=E5=8F=A3=E8=A3=85=E4=B8=8D?= =?UTF-8?q?=E4=B8=8B=E8=AF=B7=E6=B1=82=E6=97=B6=E9=80=89=E5=8F=B7=E8=B7=B3?= =?UTF-8?q?=E8=BF=87=E7=AA=84=E7=AA=97=E5=8F=A3=E8=B4=A6=E5=8F=B7=E5=B9=B6?= =?UTF-8?q?=E5=9B=9E=E5=88=B0=E5=8E=9F=E8=B4=A6=E5=8F=B7=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/adapter/stream.go | 33 ++++++------- internal/balancer/apikey.go | 20 ++++++-- internal/balancer/balancer.go | 29 ------------ internal/balancer/context_window_test.go | 59 ------------------------ 4 files changed, 34 insertions(+), 107 deletions(-) delete mode 100644 internal/balancer/context_window_test.go diff --git a/internal/adapter/stream.go b/internal/adapter/stream.go index 907feb2..c1c1bf0 100644 --- a/internal/adapter/stream.go +++ b/internal/adapter/stream.go @@ -9,6 +9,7 @@ import ( "regexp" "slices" "strings" + "sync/atomic" "time" "unicode" "unicode/utf8" @@ -48,7 +49,7 @@ func (e adapterEmitError) Unwrap() error { return e.err } type accountContextError struct { err *gemini.ProtocolError - // tokens 是这次请求需要的输入 token 数,账号池据此排除上下文窗口同样装不下请求的其他账号 + // tokens 是新会话尝试需要的输入 token 数,续接已有会话时为 0 tokens int } @@ -435,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) @@ -480,6 +484,7 @@ func runGenerationAttempts( clear(excludedAccountIDs) continue } + attemptRegenerating := regenerating regenerating = false if client == nil { if sessionKey != "" && !newSession && accountID != "" { @@ -523,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 } @@ -543,7 +552,6 @@ func runGenerationAttempts( if rebuildable && sessionKey != "" && !newSession && (isAccountContextError(err) || errors.Is(err, gemini.ErrGuestAttachments) || retryableAccountError(err) && !visibleOutput()) { trace.AttemptFailed(accountID, upstreamStatus(err), err, true) excludedAccountIDs[accountID] = struct{}{} - excludeNarrowAccounts(trace, pool, excludedAccountIDs, accountID, err) if (gemini.TransientError(err) || isOutputMismatchError(err)) && !visibleOutput() && regenerated < regenerations { regenerated++ regenerating = true @@ -557,7 +565,7 @@ func runGenerationAttempts( } trace.AttemptFailed(accountID, upstreamStatus(err), err, true) excludedAccountIDs[accountID] = struct{}{} - excludeNarrowAccounts(trace, pool, excludedAccountIDs, accountID, err) + regenerating = attemptRegenerating continue } if (gemini.TransientError(err) || isOutputMismatchError(err)) && (sessionKey == "" || newSession) && regenerated < regenerations && !visibleOutput() { @@ -671,10 +679,14 @@ func runGenerationWithClient( } promptTokens := contextTokens + tokencount.Content(prompt) + mediaTokens(files) if contextWindow := client.ContextWindow(); contextWindow > 0 && promptTokens > contextWindow { - return generationResult{}, &accountContextError{tokens: promptTokens, 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 { @@ -968,7 +980,7 @@ func isAccountContextError(err error) bool { return errors.As(err, &contextErr) } -// accountContextTokens 返回账号上下文窗口不足时这次请求需要的输入 token 数 +// accountContextTokens 返回新会话尝试因账号上下文窗口不足失败时需要的输入 token 数,其余错误为 0 func accountContextTokens(err error) int { var contextErr *accountContextError if errors.As(err, &contextErr) { @@ -977,17 +989,6 @@ func accountContextTokens(err error) int { return 0 } -// excludeNarrowAccounts 在账号因上下文窗口不足拒绝请求时把上下文窗口同样装不下这次请求的账号与访客会话一并排除,避免向它们重复上传附件并徒劳发起请求 -func excludeNarrowAccounts(trace *logging.Request, pool *balancer.AccountPool, excludedAccountIDs map[string]struct{}, accountID string, err error) { - tokens := accountContextTokens(err) - if tokens <= 0 { - return - } - if added := pool.ExcludeNarrowContext(excludedAccountIDs, tokens); len(added) > 0 { - trace.Progress("WARN", accountID, "排除上下文窗口装不下请求的账号", "需要", tokens, "账号", strings.Join(added, ",")) - } -} - // publicModelID 返回响应中的模型 ID:别名取映射目标,改名前的旧 ID 取当前 ID,gemini-default 在各就绪账号(没有账号时为访客会话)的默认模型相同时取该模型 func publicModelID(pool *balancer.AccountPool, requested string) string { id := gemini.CanonicalModelID(config.MapModel(requested)) diff --git a/internal/balancer/apikey.go b/internal/balancer/apikey.go index f5c1a33..d2d03d0 100644 --- a/internal/balancer/apikey.go +++ b/internal/balancer/apikey.go @@ -5,6 +5,7 @@ import ( "context" "maps" "slices" + "sync/atomic" "time" "github.com/Mag1cFall/Gemini-Web2API/internal/gemini" @@ -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() @@ -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() @@ -46,7 +55,11 @@ 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) @@ -54,7 +67,8 @@ func (p *AccountPool) scopeExcluded(ctx context.Context, modelID string, exclude 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{}{} } } diff --git a/internal/balancer/balancer.go b/internal/balancer/balancer.go index d25a803..e0cdfae 100644 --- a/internal/balancer/balancer.go +++ b/internal/balancer/balancer.go @@ -1147,35 +1147,6 @@ func errorText(err error) string { return logging.SanitizeError(err.Error()) } -// ExcludeNarrowContext 把上下文窗口装不下 tokens 的账号与访客会话加入排除集,返回新增的账号 ID:账号因上下文窗口不足拒绝请求后,调用方据此不再向同样装不下这次请求的其他账号重复上传附件并徒劳发起请求,窗口未知(0)的账号不排除 -func (p *AccountPool) ExcludeNarrowContext(excludedAccountIDs map[string]struct{}, tokens int) []string { - if tokens <= 0 { - return nil - } - p.mu.Lock() - defer p.mu.Unlock() - var added []string - for _, entry := range p.entries { - if entry.Client == nil { - continue - } - if _, excluded := excludedAccountIDs[entry.AccountID]; excluded { - continue - } - if !contextTooNarrow(entry.Client.ContextWindow(), tokens) { - continue - } - excludedAccountIDs[entry.AccountID] = struct{}{} - added = append(added, entry.AccountID) - } - return added -} - -// contextTooNarrow 报告账号的上下文窗口是否装不下 tokens,窗口未知(0)时按未知处理 -func contextTooNarrow(window, tokens int) bool { - return window > 0 && tokens > 0 && window < tokens -} - // nextAvailable 优先在账号中选择,没有可调度的账号时使用访客会话 func (p *AccountPool) nextAvailable(now time.Time, modelID string, excludedAccountIDs map[string]struct{}) *accountEntry { if entry := p.nextFrom(now, modelID, excludedAccountIDs, false); entry != nil { diff --git a/internal/balancer/context_window_test.go b/internal/balancer/context_window_test.go deleted file mode 100644 index 17d8628..0000000 --- a/internal/balancer/context_window_test.go +++ /dev/null @@ -1,59 +0,0 @@ -package balancer - -import ( - "testing" - "time" -) - -// TestContextTooNarrow 验证上下文窗口与请求 token 数的比较:窗口小于请求时为真,两者相等时仍可装下,窗口未知(0)时按未知处理 -func TestContextTooNarrow(t *testing.T) { - cases := []struct { - window int - tokens int - narrow bool - }{ - {32768, 56508, true}, - {32768, 32768, false}, - {128 * 1024, 56508, false}, - {0, 56508, false}, - {32768, 0, false}, - } - for _, tc := range cases { - if got := contextTooNarrow(tc.window, tc.tokens); got != tc.narrow { - t.Fatalf("窗口 %d、请求 %d tokens:%v,应为 %v", tc.window, tc.tokens, got, tc.narrow) - } - } -} - -// TestExcludeNarrowContextKeepsUnknownWindow 验证排除装不下请求的账号时的边界:请求 token 数不为正时不改动,窗口未知的账号不排除,已排除的账号不重复加入 -func TestExcludeNarrowContextKeepsUnknownWindow(t *testing.T) { - pool := NewAccountPool(2*time.Minute, time.Minute) - pool.Register("account", AccountInfo{}, false) - pool.Register("guest", AccountInfo{Source: SourceGuest}, false) - pool.MarkReady("account") - pool.MarkReady("guest") - excluded := map[string]struct{}{"account": {}} - if added := pool.ExcludeNarrowContext(excluded, 0); len(added) != 0 { - t.Fatalf("请求 token 数为 0 时不应排除账号,实际排除 %v", added) - } - if added := pool.ExcludeNarrowContext(excluded, 56508); len(added) != 0 { - t.Fatalf("窗口未知的账号不应排除,实际排除 %v", added) - } - if len(excluded) != 1 { - t.Fatalf("排除集被改动:%v", excluded) - } -} - -// TestExcludeNarrowContextReportsAddedAccounts 验证排除装不下请求的账号时返回新增的账号 ID 并写入排除集,账号池没有排除集之外的改动 -func TestExcludeNarrowContextReportsAddedAccounts(t *testing.T) { - pool := NewAccountPool(2*time.Minute, time.Minute) - pool.Register("guest", AccountInfo{Source: SourceGuest}, false) - pool.MarkReady("guest") - excluded := map[string]struct{}{"other": {}} - if added := pool.ExcludeNarrowContext(excluded, 1); len(added) != 0 { - t.Fatalf("窗口未知的访客会话不应排除:%v", added) - } - if _, ok := excluded["other"]; !ok || len(excluded) != 1 { - t.Fatalf("排除集应只含原有的账号:%v", excluded) - } -}