fix(anthropic-auto-compact): handle empty messages at arbitrary indices

- Add messageIndex field to ParsedTokenLimitError type for tracking message position
- Extract message index from 'messages.N' format in error messages using regex
- Update fixEmptyMessages to accept optional messageIndex parameter
- Target specific empty message by index instead of fixing all empty messages
- Apply replaceEmptyTextParts before injectTextPart for better coverage
- Remove experimental flag requirement - non-empty content errors now auto-recover by default
- Fixes issue where compaction could create empty messages at positions other than the last message

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
YeonGyu-Kim
2025-12-20 14:47:42 +09:00
parent 4f24423e44
commit 8406f3d6d7
3 changed files with 76 additions and 22 deletions
@@ -28,6 +28,8 @@ const TOKEN_LIMIT_KEYWORDS = [
"non-empty content",
]
const MESSAGE_INDEX_PATTERN = /messages\.(\d+)/
function extractTokensFromMessage(message: string): { current: number; max: number } | null {
for (const pattern of TOKEN_LIMIT_PATTERNS) {
const match = message.match(pattern)
@@ -40,6 +42,14 @@ function extractTokensFromMessage(message: string): { current: number; max: numb
return null
}
function extractMessageIndex(text: string): number | undefined {
const match = text.match(MESSAGE_INDEX_PATTERN)
if (match) {
return parseInt(match[1], 10)
}
return undefined
}
function isTokenLimitError(text: string): boolean {
const lower = text.toLowerCase()
return TOKEN_LIMIT_KEYWORDS.some((kw) => lower.includes(kw.toLowerCase()))
@@ -52,6 +62,7 @@ export function parseAnthropicTokenLimitError(err: unknown): ParsedTokenLimitErr
currentTokens: 0,
maxTokens: 0,
errorType: "non-empty content",
messageIndex: extractMessageIndex(err),
}
}
if (isTokenLimitError(err)) {
@@ -155,6 +166,7 @@ export function parseAnthropicTokenLimitError(err: unknown): ParsedTokenLimitErr
currentTokens: 0,
maxTokens: 0,
errorType: "non-empty content",
messageIndex: extractMessageIndex(combinedText),
}
}