nub/internal/tokens/blocks.go
Tom a97013d876 initial commit
- v 0.1.0 siehe CHANGELOG.md
2026-07-25 11:08:01 +02:00

29 lines
757 B
Go

package tokens
import "nub/internal/llm"
// EstimateBlocks summiert die Schätzung über Text, Tool-Input und
// (rekursiv) Tool-Result-Blöcke.
func EstimateBlocks(blocks []llm.Block) int {
total := 0
for _, b := range blocks {
total += Estimate(b.Text)
if len(b.Input) > 0 {
total += Estimate(string(b.Input))
}
if len(b.Result) > 0 {
total += EstimateBlocks(b.Result)
}
}
return total
}
// EstimateMessages schätzt die Tokenkosten eines kompletten Requests
// (System-Blöcke + Message-Historie) — Grundlage für den Compaction-Trigger.
func EstimateMessages(system []llm.Block, messages []llm.Message) int {
total := EstimateBlocks(system)
for _, m := range messages {
total += EstimateBlocks(m.Content)
}
return total
}