fix(anthropic-auto-compact): sanitize empty messages before summarization
Pre-emptively fix empty messages in sessions before running document compression to prevent summarization failures. This prevents accumulation of empty message placeholders that can interfere with context management. 🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
@@ -21,6 +21,8 @@ import {
|
|||||||
} from "../session-recovery/storage";
|
} from "../session-recovery/storage";
|
||||||
import { log } from "../../shared/logger";
|
import { log } from "../../shared/logger";
|
||||||
|
|
||||||
|
const PLACEHOLDER_TEXT = "[user interrupted]";
|
||||||
|
|
||||||
type Client = {
|
type Client = {
|
||||||
session: {
|
session: {
|
||||||
messages: (opts: {
|
messages: (opts: {
|
||||||
@@ -103,6 +105,36 @@ function getOrCreateDcpState(
|
|||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sanitizeEmptyMessagesBeforeSummarize(sessionID: string): number {
|
||||||
|
const emptyMessageIds = findEmptyMessages(sessionID);
|
||||||
|
if (emptyMessageIds.length === 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let fixedCount = 0;
|
||||||
|
for (const messageID of emptyMessageIds) {
|
||||||
|
const replaced = replaceEmptyTextParts(messageID, PLACEHOLDER_TEXT);
|
||||||
|
if (replaced) {
|
||||||
|
fixedCount++;
|
||||||
|
} else {
|
||||||
|
const injected = injectTextPart(sessionID, messageID, PLACEHOLDER_TEXT);
|
||||||
|
if (injected) {
|
||||||
|
fixedCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fixedCount > 0) {
|
||||||
|
log("[auto-compact] pre-summarize sanitization fixed empty messages", {
|
||||||
|
sessionID,
|
||||||
|
fixedCount,
|
||||||
|
totalEmpty: emptyMessageIds.length,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return fixedCount;
|
||||||
|
}
|
||||||
|
|
||||||
async function getLastMessagePair(
|
async function getLastMessagePair(
|
||||||
sessionID: string,
|
sessionID: string,
|
||||||
client: Client,
|
client: Client,
|
||||||
@@ -379,6 +411,8 @@ export async function executeCompact(
|
|||||||
|
|
||||||
if (providerID && modelID) {
|
if (providerID && modelID) {
|
||||||
try {
|
try {
|
||||||
|
sanitizeEmptyMessagesBeforeSummarize(sessionID);
|
||||||
|
|
||||||
await (client as Client).tui
|
await (client as Client).tui
|
||||||
.showToast({
|
.showToast({
|
||||||
body: {
|
body: {
|
||||||
@@ -619,6 +653,8 @@ export async function executeCompact(
|
|||||||
|
|
||||||
if (providerID && modelID) {
|
if (providerID && modelID) {
|
||||||
try {
|
try {
|
||||||
|
sanitizeEmptyMessagesBeforeSummarize(sessionID);
|
||||||
|
|
||||||
await (client as Client).tui
|
await (client as Client).tui
|
||||||
.showToast({
|
.showToast({
|
||||||
body: {
|
body: {
|
||||||
|
|||||||
Reference in New Issue
Block a user