Add previousSummary to before_compact hook event

This commit is contained in:
Mario Zechner 2025-12-24 12:47:12 +01:00
parent 97bbd7a642
commit 403faafdbe
5 changed files with 29 additions and 2 deletions

View file

@ -756,6 +756,15 @@ export class AgentSession {
throw new Error("Already compacted");
}
// Find previous compaction summary if any
let previousSummary: string | undefined;
for (let i = entries.length - 1; i >= 0; i--) {
if (entries[i].type === "compaction") {
previousSummary = (entries[i] as CompactionEntry).summary;
break;
}
}
let compactionEntry: CompactionEntry | undefined;
let fromHook = false;
@ -767,6 +776,7 @@ export class AgentSession {
previousSessionFile: null,
reason: "before_compact",
cutPoint: preparation.cutPoint,
previousSummary,
messagesToSummarize: [...preparation.messagesToSummarize],
messagesToKeep: [...preparation.messagesToKeep],
tokensBefore: preparation.tokensBefore,
@ -907,6 +917,15 @@ export class AgentSession {
return;
}
// Find previous compaction summary if any
let previousSummary: string | undefined;
for (let i = entries.length - 1; i >= 0; i--) {
if (entries[i].type === "compaction") {
previousSummary = (entries[i] as CompactionEntry).summary;
break;
}
}
let compactionEntry: CompactionEntry | undefined;
let fromHook = false;
@ -918,6 +937,7 @@ export class AgentSession {
previousSessionFile: null,
reason: "before_compact",
cutPoint: preparation.cutPoint,
previousSummary,
messagesToSummarize: [...preparation.messagesToSummarize],
messagesToKeep: [...preparation.messagesToKeep],
tokensBefore: preparation.tokensBefore,

View file

@ -130,6 +130,8 @@ export type SessionEvent =
| (SessionEventBase & {
reason: "before_compact";
cutPoint: CutPointResult;
/** Summary from previous compaction, if any. Include this in your summary to preserve context. */
previousSummary?: string;
/** Messages that will be summarized and discarded */
messagesToSummarize: AppMessage[];
/** Messages that will be kept after the summary (recent turns) */