mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 16:04:01 +00:00
A free-tier 400 (context_length_exceeded, invalid_request_error) was treated as retryable: the gateway walked the second anonymous lane, then the keyed tier, and the client received whatever the last tier answered. With the documented placeholder in zen_keys, a 941 KB request that only needed compaction came back as "401 Invalid credential". - classify request-level rejections (400/422 with invalid_request_error, context_length_exceeded, maximum context length, string_above_max_length or prompt is too long) and end the request after the first attempt on both the anonymous and keyed lanes, forwarding the original status and body; 401/403/429 stay retryable because they can be key-specific; - read the body once with peekResponseBody so classification does not consume what the client eventually receives; - drop documented placeholder keys (sk-your-zen-key, change-this-local-key) during config normalization: they never authenticate, and keeping them turns every free-tier fallback into a 401 that hides the real error. Verified end to end by replaying the recorded request against the built binary with the original configuration: 400 with the upstream message, one upstream attempt, an anonymous key_id, and zen/go pools of 0. Co-Authored-By: Claude Code <noreply@anthropic.com>
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package config
|
|
|
|
import "strings"
|
|
|
|
// placeholderKeys are the example values the shipped documentation uses. They
|
|
// are not secrets and no upstream accepts them: a configuration that still
|
|
// carries one has simply not been filled in yet.
|
|
var placeholderKeys = []string{
|
|
"sk-your-zen-key",
|
|
"change-this-local-key",
|
|
}
|
|
|
|
// IsPlaceholderKey reports whether key is a documented placeholder rather than
|
|
// a real credential. Comparison is case-insensitive and ignores surrounding
|
|
// whitespace.
|
|
func IsPlaceholderKey(key string) bool {
|
|
trimmed := strings.ToLower(strings.TrimSpace(key))
|
|
for _, placeholder := range placeholderKeys {
|
|
if trimmed == placeholder {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// DropPlaceholderKeys removes placeholder credentials from the key pools. A
|
|
// placeholder can never authenticate, so keeping it only turns every fallback
|
|
// that reaches the keyed tiers into a 401 — which then hides the real error
|
|
// (the cause of the failover) from the client. Callers still see the configured
|
|
// list through the management API; the pools simply behave as if the tier was
|
|
// left empty.
|
|
func DropPlaceholderKeys(cfg Config) Config {
|
|
drop := func(keys []string) []string {
|
|
if len(keys) == 0 {
|
|
return keys
|
|
}
|
|
kept := keys[:0]
|
|
for _, key := range keys {
|
|
if IsPlaceholderKey(key) {
|
|
continue
|
|
}
|
|
kept = append(kept, key)
|
|
}
|
|
return kept
|
|
}
|
|
cfg.ZenKeys = drop(cfg.ZenKeys)
|
|
cfg.GoKeys = drop(cfg.GoKeys)
|
|
return cfg
|
|
}
|