Files
ARTEX/third_party/norma/noaadapter/overflow.go
T
wangdefa f782b612ec build: 内置 norma 模块,构建不再依赖上游仓库
把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到
third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或
改写 tag 都不再影响本仓库构建。

- 已验证 GOPROXY=off 下可完整编译并打出发行包
- go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希)
- 升级方法与授权注意事项见 third_party/README.md
2026-10-20 21:10:41 -07:00

79 lines
2.1 KiB
Go

package noaadapter
import (
"regexp"
"strconv"
"sync"
)
// overflowState remembers what a context-overflow error taught us.
//
// The provider's real window is often smaller than the configured one (a proxy
// trims it, a deployment differs from the published spec). Rather than guessing
// again next turn, the error is mined for the actual number and the mechanical
// valve is armed so the next view is built to fit.
type overflowState struct {
mu sync.Mutex
// learnedWindow is the window size parsed out of an error, 0 if unknown.
learnedWindow int
// armed makes the next view build to the emergency floor.
armed bool
}
func (o *overflowState) arm(configured int) {
o.mu.Lock()
defer o.mu.Unlock()
o.armed = true
if o.learnedWindow == 0 {
o.learnedWindow = configured
}
}
func (o *overflowState) disarm() {
o.mu.Lock()
defer o.mu.Unlock()
o.armed = false
}
// armedFloor is the token ceiling the next view must fit under, or 0 when not
// armed.
func (o *overflowState) armedFloor() int {
o.mu.Lock()
defer o.mu.Unlock()
if !o.armed || o.learnedWindow <= 0 {
return 0
}
return int(float64(o.learnedWindow) * 0.95)
}
// learn records a window size parsed from an error message.
func (o *overflowState) learn(window int) {
if window <= 0 {
return
}
o.mu.Lock()
defer o.mu.Unlock()
o.learnedWindow = window
}
// windowPatterns extract the real context window from an overflow error.
// Providers phrase this several ways; each pattern captures the limit.
var windowPatterns = []*regexp.Regexp{
regexp.MustCompile(`(?i)maximum context length is (\d+)`),
regexp.MustCompile(`(?i)context (?:window|length) (?:of|is) (\d+)`),
regexp.MustCompile(`(?i)max(?:imum)?[ _-]?tokens?[^0-9]{0,20}(\d{4,})`),
regexp.MustCompile(`(?i)limit(?:ed)? to (\d{4,}) tokens`),
}
// ParseOverflowWindow extracts a context window size from an error message.
func ParseOverflowWindow(msg string) int {
for _, re := range windowPatterns {
if m := re.FindStringSubmatch(msg); m != nil {
if n, err := strconv.Atoi(m[1]); err == nil && n > 0 {
return n
}
}
}
return 0
}