calculateTokenWarningState function
Calculate the token warning state given current usage.
Implementation
TokenWarningState calculateTokenWarningState(
int tokenUsage,
String model,
int contextWindow, {
bool autoCompactEnabled = true,
}) {
final autoCompactThreshold = getAutoCompactThreshold(model, contextWindow);
final effectiveWindow = getEffectiveContextWindowSize(model, contextWindow);
final threshold = autoCompactEnabled ? autoCompactThreshold : effectiveWindow;
final percentLeft = max(
0,
((threshold - tokenUsage) / threshold * 100).round(),
);
final warningThreshold = threshold - warningThresholdBufferTokens;
final errorThreshold = threshold - errorThresholdBufferTokens;
final isAboveWarningThreshold = tokenUsage >= warningThreshold;
final isAboveErrorThreshold = tokenUsage >= errorThreshold;
final isAboveAutoCompactThreshold =
autoCompactEnabled && tokenUsage >= autoCompactThreshold;
final defaultBlockingLimit = effectiveWindow - manualCompactBufferTokens;
final isAtBlockingLimit = tokenUsage >= defaultBlockingLimit;
return TokenWarningState(
percentLeft: percentLeft,
isAboveWarningThreshold: isAboveWarningThreshold,
isAboveErrorThreshold: isAboveErrorThreshold,
isAboveAutoCompactThreshold: isAboveAutoCompactThreshold,
isAtBlockingLimit: isAtBlockingLimit,
);
}