29 lines
757 B
Go
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
|
|
}
|