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
This commit is contained in:
1 parent
a819fa3230
commit
f782b612ec
213 files changed
+37401
-2
No files matched your search
Vendored
+64
@@ -0,0 +1,64 @@
|
||||
package noa
|
||||
|
||||
import "math"
|
||||
|
||||
// TokenCountFn estimates a text's token cost. Injected so the host can supply a
|
||||
// real tokenizer; DefaultCountTokens is the built-in approximation.
|
||||
type TokenCountFn func(string) int
|
||||
|
||||
// DefaultCountTokens approximates tokens as one per CJK rune plus one per four
|
||||
// other runes.
|
||||
//
|
||||
// Deviation from upstream (deliberate): counting is by RUNE, not UTF-16 code
|
||||
// unit. Upstream mirrors JavaScript's String.length so its numbers match the
|
||||
// reference implementation byte for byte; noa has no such requirement, and rune
|
||||
// semantics are the natural Go reading. The only visible difference is that
|
||||
// astral-plane characters (emoji) count once instead of twice.
|
||||
func DefaultCountTokens(text string) int {
|
||||
if text == "" {
|
||||
return 0
|
||||
}
|
||||
cjk, other := 0, 0
|
||||
for _, r := range text {
|
||||
if isCJK(r) {
|
||||
cjk++
|
||||
} else {
|
||||
other++
|
||||
}
|
||||
}
|
||||
return cjk + ceilDiv(other, 4)
|
||||
}
|
||||
|
||||
// EstimateTokensFast skips the CJK scan; used where a rough figure suffices.
|
||||
func EstimateTokensFast(text string) int { return ceilDiv(len([]rune(text)), 4) }
|
||||
|
||||
// CountMessageTokens is the per-message cost under a given counter.
|
||||
func CountMessageTokens(m CoreMessage, count TokenCountFn) int {
|
||||
if count == nil {
|
||||
count = DefaultCountTokens
|
||||
}
|
||||
return count(m.Text)
|
||||
}
|
||||
|
||||
func isCJK(r rune) bool {
|
||||
switch {
|
||||
case r >= 0x4E00 && r <= 0x9FFF: // CJK Unified Ideographs
|
||||
return true
|
||||
case r >= 0x3040 && r <= 0x30FF: // Hiragana + Katakana
|
||||
return true
|
||||
case r >= 0xAC00 && r <= 0xD7AF: // Hangul Syllables
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ceilDiv(n, d int) int {
|
||||
if n <= 0 {
|
||||
return 0
|
||||
}
|
||||
return (n + d - 1) / d
|
||||
}
|
||||
|
||||
// roundHalfUp matches JavaScript's Math.round for the non-negative values used
|
||||
// here (Go's math.Round is half-away-from-zero, which agrees on positives).
|
||||
func roundHalfUp(f float64) int { return int(math.Round(f)) }
|
||||
Reference in new issue
Block a user