Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(ai): replace summarized chat history

Arham Wani9fdaf10921a4
diff --git a/electron/ai-edition/chat-compaction.test.ts b/electron/ai-edition/chat-compaction.test.ts
index 63a61f379..e10ee3454 100644
--- a/electron/ai-edition/chat-compaction.test.ts
+++ b/electron/ai-edition/chat-compaction.test.ts
@@ -78,18 +78,19 @@ describe("shouldCompact", () => {
});
describe("applyCompaction", () => {
- it("inserts the summary message at the split point", () => {
+ it("replaces the summarized prefix with the summary message", () => {
const msgs = [
- msg("user", "1", "u1"),
- msg("assistant", "2", "a1"),
+ msg("user", "x".repeat(4_000), "u1"),
+ msg("assistant", "y".repeat(4_000), "a1"),
msg("user", "3", "u2"),
msg("assistant", "4", "a2"),
];
const out = applyCompaction(msgs, 2, "summary text", "2026-02-01T00:00:00.000Z");
- expect(out.length).toBe(5);
- expect(out[2]?.content).toBe("summary text");
- expect(out[0]?.id).toBe("u1");
+ expect(out).toHaveLength(3);
+ expect(out[0]?.content).toBe("summary text");
+ expect(out[1]?.id).toBe("u2");
expect(out.at(-1)?.id).toBe("a2");
+ expect(estimateHistoryTokens(out)).toBeLessThan(estimateHistoryTokens(msgs));
});
});
diff --git a/electron/ai-edition/chat-compaction.ts b/electron/ai-edition/chat-compaction.ts
index e9faf4706..49ccb803a 100644
--- a/electron/ai-edition/chat-compaction.ts
+++ b/electron/ai-edition/chat-compaction.ts
@@ -83,7 +83,6 @@ export function applyCompaction(
summary: string,
summaryAt: string,
): AiEditionChatMessage[] {
- const head = messages.slice(0, splitIndex);
const tail = messages.slice(splitIndex);
const summaryMessage: AiEditionChatMessage = {
id: `summary_${Date.now()}`,
@@ -91,7 +90,7 @@ export function applyCompaction(
content: summary,
createdAt: summaryAt,
};
- return [...head, summaryMessage, ...tail];
+ return [summaryMessage, ...tail];
}
/** System-prompt addendum to ask the LLM to compact its own history. */
diff --git a/electron/ai-edition/chat-service.ts b/electron/ai-edition/chat-service.ts
index 180f8e0bc..668f5fd04 100644
--- a/electron/ai-edition/chat-service.ts
+++ b/electron/ai-edition/chat-service.ts
@@ -700,7 +700,7 @@ async function tryCompactSession(opts: {
summary,
new Date().toISOString(),
);
- const inserted = compacted[splitIndex];
+ const inserted = compacted[0];
session.messages = compacted;
return {
summaryMessageId: inserted?.id ?? null,