mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 17:01:53 +03:00
fix: update compaction safeguard to respect context window tokens
This commit is contained in:
@@ -81,8 +81,16 @@ export function buildEmbeddedExtensionPaths(params: {
|
|||||||
const paths: string[] = [];
|
const paths: string[] = [];
|
||||||
if (resolveCompactionMode(params.cfg) === "safeguard") {
|
if (resolveCompactionMode(params.cfg) === "safeguard") {
|
||||||
const compactionCfg = params.cfg?.agents?.defaults?.compaction;
|
const compactionCfg = params.cfg?.agents?.defaults?.compaction;
|
||||||
|
const contextWindowInfo = resolveContextWindowInfo({
|
||||||
|
cfg: params.cfg,
|
||||||
|
provider: params.provider,
|
||||||
|
modelId: params.modelId,
|
||||||
|
modelContextWindow: params.model?.contextWindow,
|
||||||
|
defaultTokens: DEFAULT_CONTEXT_TOKENS,
|
||||||
|
});
|
||||||
setCompactionSafeguardRuntime(params.sessionManager, {
|
setCompactionSafeguardRuntime(params.sessionManager, {
|
||||||
maxHistoryShare: compactionCfg?.maxHistoryShare,
|
maxHistoryShare: compactionCfg?.maxHistoryShare,
|
||||||
|
contextWindowTokens: contextWindowInfo.tokens,
|
||||||
});
|
});
|
||||||
paths.push(resolvePiExtensionPath("compaction-safeguard"));
|
paths.push(resolvePiExtensionPath("compaction-safeguard"));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
export type CompactionSafeguardRuntimeValue = {
|
export type CompactionSafeguardRuntimeValue = {
|
||||||
maxHistoryShare?: number;
|
maxHistoryShare?: number;
|
||||||
|
contextWindowTokens?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Session-scoped runtime registry keyed by object identity.
|
// Session-scoped runtime registry keyed by object identity.
|
||||||
|
|||||||
@@ -195,11 +195,15 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const contextWindowTokens = resolveContextWindowTokens(model);
|
const runtime = getCompactionSafeguardRuntime(ctx.sessionManager);
|
||||||
|
const modelContextWindow = resolveContextWindowTokens(model);
|
||||||
|
const contextWindowTokens = Math.min(
|
||||||
|
runtime?.contextWindowTokens ?? modelContextWindow,
|
||||||
|
modelContextWindow,
|
||||||
|
);
|
||||||
const turnPrefixMessages = preparation.turnPrefixMessages ?? [];
|
const turnPrefixMessages = preparation.turnPrefixMessages ?? [];
|
||||||
let messagesToSummarize = preparation.messagesToSummarize;
|
let messagesToSummarize = preparation.messagesToSummarize;
|
||||||
|
|
||||||
const runtime = getCompactionSafeguardRuntime(ctx.sessionManager);
|
|
||||||
const maxHistoryShare = runtime?.maxHistoryShare ?? 0.5;
|
const maxHistoryShare = runtime?.maxHistoryShare ?? 0.5;
|
||||||
|
|
||||||
const tokensBefore =
|
const tokensBefore =
|
||||||
|
|||||||
Reference in New Issue
Block a user