


























@@ -9,6 +9,10 @@ import {
99getCompactionProvider,
1010type CompactionProvider,
1111} from "../../plugins/compaction-provider.js";
12+import {
13+buildHistoryPrunePlanWithWorker,
14+computeAdaptiveChunkRatioWithWorker,
15+} from "../compaction-planning-worker.js";
1216import {
1317hasMeaningfulConversationContent,
1418isRealConversationMessage,
@@ -19,9 +23,7 @@ import {
1923SAFETY_MARGIN,
2024SUMMARIZATION_OVERHEAD_TOKENS,
2125computeAdaptiveChunkRatio,
22-estimateMessagesTokens,
2326isOversizedForSummary,
24-pruneHistoryForContextShare,
2527resolveContextWindowTokens,
2628summarizeInStages,
2729} from "../compaction.js";
@@ -1071,19 +1073,18 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
10711073let droppedSummary: string | undefined;
1072107410731075if (tokensBefore !== undefined) {
1074-const summarizableTokens =
1075-estimateMessagesTokens(messagesToSummarize) + estimateMessagesTokens(turnPrefixMessages);
1076-const newContentTokens = Math.max(0, Math.floor(tokensBefore - summarizableTokens));
1077-// Apply SAFETY_MARGIN so token underestimates don't trigger unnecessary pruning
1078-const maxHistoryTokens = Math.floor(contextWindowTokens * maxHistoryShare * SAFETY_MARGIN);
1079-1080-if (newContentTokens > maxHistoryTokens) {
1081-const pruned = pruneHistoryForContextShare({
1082-messages: messagesToSummarize,
1083-maxContextTokens: contextWindowTokens,
1084- maxHistoryShare,
1085-parts: 2,
1086-});
1076+const prunePlan = await buildHistoryPrunePlanWithWorker({
1077+ messagesToSummarize,
1078+ turnPrefixMessages,
1079+ tokensBefore,
1080+ contextWindowTokens,
1081+ maxHistoryShare,
1082+parts: 2,
1083+ signal,
1084+});
1085+const { newContentTokens, maxHistoryTokens, pruned } = prunePlan;
1086+1087+if (newContentTokens > maxHistoryTokens && pruned) {
10871088if (pruned.droppedChunks > 0) {
10881089const newContentRatio = (newContentTokens / contextWindowTokens) * 100;
10891090log.warn(
@@ -1097,10 +1098,11 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
10971098// Summarize dropped messages so context isn't lost
10981099if (pruned.droppedMessagesList.length > 0) {
10991100try {
1100-const droppedChunkRatio = computeAdaptiveChunkRatio(
1101-pruned.droppedMessagesList,
1102-contextWindowTokens,
1103-);
1101+const droppedChunkRatio = await computeAdaptiveChunkRatioWithWorker({
1102+messages: pruned.droppedMessagesList,
1103+contextWindow: contextWindowTokens,
1104+ signal,
1105+});
11041106const droppedMaxChunkTokens = Math.max(
110511071,
11061108Math.floor(contextWindowTokens * droppedChunkRatio) -
@@ -1155,7 +1157,11 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
11551157// the summarization prompt, system prompt, previous summary, and reasoning budget
11561158// that generateSummary adds on top of the serialized conversation chunk.
11571159const allMessages = [...messagesToSummarize, ...turnPrefixMessages];
1158-const adaptiveRatio = computeAdaptiveChunkRatio(allMessages, contextWindowTokens);
1160+const adaptiveRatio = await computeAdaptiveChunkRatioWithWorker({
1161+messages: allMessages,
1162+contextWindow: contextWindowTokens,
1163+ signal,
1164+});
11591165const maxChunkTokens = Math.max(
116011661,
11611167Math.floor(contextWindowTokens * adaptiveRatio) - SUMMARIZATION_OVERHEAD_TOKENS,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。