mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 22:11:23 +00:00
fix: context overflow compaction and subagent announce improvements (#11664) (thanks @tyler6204)
* initial commit * feat: implement deriveSessionTotalTokens function and update usage tests * Added deriveSessionTotalTokens function to calculate total tokens based on usage and context tokens. * Updated usage tests to include cases for derived session total tokens. * Refactored session usage calculations in multiple files to utilize the new function for improved accuracy. * fix: restore overflow truncation fallback + changelog/test hardening (#11551) (thanks @tyler6204)
This commit is contained in:
@@ -103,3 +103,34 @@ export function derivePromptTokens(usage?: {
|
||||
const sum = input + cacheRead + cacheWrite;
|
||||
return sum > 0 ? sum : undefined;
|
||||
}
|
||||
|
||||
export function deriveSessionTotalTokens(params: {
|
||||
usage?: {
|
||||
input?: number;
|
||||
total?: number;
|
||||
cacheRead?: number;
|
||||
cacheWrite?: number;
|
||||
};
|
||||
contextTokens?: number;
|
||||
}): number | undefined {
|
||||
const usage = params.usage;
|
||||
if (!usage) {
|
||||
return undefined;
|
||||
}
|
||||
const input = usage.input ?? 0;
|
||||
const promptTokens = derivePromptTokens({
|
||||
input: usage.input,
|
||||
cacheRead: usage.cacheRead,
|
||||
cacheWrite: usage.cacheWrite,
|
||||
});
|
||||
let total = promptTokens ?? usage.total ?? input;
|
||||
if (!(total > 0)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const contextTokens = params.contextTokens;
|
||||
if (typeof contextTokens === "number" && Number.isFinite(contextTokens) && contextTokens > 0) {
|
||||
total = Math.min(total, contextTokens);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user