Improve before_compact hook: add messagesToKeep, replace apiKey with resolveApiKey

This commit is contained in:
Mario Zechner 2025-12-24 12:41:22 +01:00
parent e4283294c8
commit d9a542763a
8 changed files with 51 additions and 24 deletions

View file

@ -767,11 +767,11 @@ export class AgentSession {
previousSessionFile: null,
reason: "before_compact",
cutPoint: preparation.cutPoint,
messagesToSummarize: preparation.messagesToSummarize,
messagesToSummarize: [...preparation.messagesToSummarize],
messagesToKeep: [...preparation.messagesToKeep],
tokensBefore: preparation.tokensBefore,
customInstructions,
model: this.model,
apiKey,
resolveApiKey: this._resolveApiKey,
})) as SessionEventResult | undefined;
@ -918,11 +918,11 @@ export class AgentSession {
previousSessionFile: null,
reason: "before_compact",
cutPoint: preparation.cutPoint,
messagesToSummarize: preparation.messagesToSummarize,
messagesToSummarize: [...preparation.messagesToSummarize],
messagesToKeep: [...preparation.messagesToKeep],
tokensBefore: preparation.tokensBefore,
customInstructions: undefined,
model: this.model,
apiKey,
resolveApiKey: this._resolveApiKey,
})) as SessionEventResult | undefined;

View file

@ -327,7 +327,10 @@ export async function generateSummary(
export interface CompactionPreparation {
cutPoint: CutPointResult;
/** Messages that will be summarized and discarded */
messagesToSummarize: AppMessage[];
/** Messages that will be kept after the summary (recent turns) */
messagesToKeep: AppMessage[];
tokensBefore: number;
boundaryStart: number;
}
@ -353,6 +356,8 @@ export function prepareCompaction(entries: SessionEntry[], settings: CompactionS
const cutPoint = findCutPoint(entries, boundaryStart, boundaryEnd, settings.keepRecentTokens);
const historyEnd = cutPoint.isSplitTurn ? cutPoint.turnStartIndex : cutPoint.firstKeptEntryIndex;
// Messages to summarize (will be discarded after summary)
const messagesToSummarize: AppMessage[] = [];
for (let i = boundaryStart; i < historyEnd; i++) {
const entry = entries[i];
@ -361,7 +366,16 @@ export function prepareCompaction(entries: SessionEntry[], settings: CompactionS
}
}
return { cutPoint, messagesToSummarize, tokensBefore, boundaryStart };
// Messages to keep (recent turns, kept after summary)
const messagesToKeep: AppMessage[] = [];
for (let i = cutPoint.firstKeptEntryIndex; i < boundaryEnd; i++) {
const entry = entries[i];
if (entry.type === "message") {
messagesToKeep.push(entry.message);
}
}
return { cutPoint, messagesToSummarize, messagesToKeep, tokensBefore, boundaryStart };
}
// ============================================================================

View file

@ -130,11 +130,13 @@ export type SessionEvent =
| (SessionEventBase & {
reason: "before_compact";
cutPoint: CutPointResult;
/** Messages that will be summarized and discarded */
messagesToSummarize: AppMessage[];
/** Messages that will be kept after the summary (recent turns) */
messagesToKeep: AppMessage[];
tokensBefore: number;
customInstructions?: string;
model: Model<any>;
apiKey: string;
/** Resolve API key for any model (checks settings, OAuth, env vars) */
resolveApiKey: (model: Model<any>) => Promise<string | undefined>;
})