Skip to content

Commit 0317102

Browse files
committed
Merge branch 'fix-empty-message-content'
2 parents ac9ec62 + a22a800 commit 0317102

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

src/hooks/session-recovery/index.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import {
44
findEmptyMessages,
55
findEmptyMessageByIndex,
66
findMessageByIndexNeedingThinking,
7+
findMessagesWithEmptyTextParts,
78
findMessagesWithOrphanThinking,
89
findMessagesWithThinkingBlocks,
910
findMessagesWithThinkingOnly,
1011
injectTextPart,
1112
prependThinkingPart,
1213
readParts,
14+
replaceEmptyTextParts,
1315
stripThinkingParts,
1416
} from "./storage"
1517
import type { MessageData } from "./types"
@@ -222,28 +224,48 @@ async function recoverEmptyContentMessage(
222224
): Promise<boolean> {
223225
const targetIndex = extractMessageIndex(error)
224226
const failedID = failedAssistantMsg.info?.id
227+
let anySuccess = false
228+
229+
const messagesWithEmptyText = findMessagesWithEmptyTextParts(sessionID)
230+
for (const messageID of messagesWithEmptyText) {
231+
if (replaceEmptyTextParts(messageID, PLACEHOLDER_TEXT)) {
232+
anySuccess = true
233+
}
234+
}
225235

226236
const thinkingOnlyIDs = findMessagesWithThinkingOnly(sessionID)
227237
for (const messageID of thinkingOnlyIDs) {
228-
injectTextPart(sessionID, messageID, PLACEHOLDER_TEXT)
238+
if (injectTextPart(sessionID, messageID, PLACEHOLDER_TEXT)) {
239+
anySuccess = true
240+
}
229241
}
230242

231243
if (targetIndex !== null) {
232244
const targetMessageID = findEmptyMessageByIndex(sessionID, targetIndex)
233245
if (targetMessageID) {
234-
return injectTextPart(sessionID, targetMessageID, PLACEHOLDER_TEXT)
246+
if (replaceEmptyTextParts(targetMessageID, PLACEHOLDER_TEXT)) {
247+
return true
248+
}
249+
if (injectTextPart(sessionID, targetMessageID, PLACEHOLDER_TEXT)) {
250+
return true
251+
}
235252
}
236253
}
237254

238255
if (failedID) {
256+
if (replaceEmptyTextParts(failedID, PLACEHOLDER_TEXT)) {
257+
return true
258+
}
239259
if (injectTextPart(sessionID, failedID, PLACEHOLDER_TEXT)) {
240260
return true
241261
}
242262
}
243263

244264
const emptyMessageIDs = findEmptyMessages(sessionID)
245-
let anySuccess = thinkingOnlyIDs.length > 0
246265
for (const messageID of emptyMessageIDs) {
266+
if (replaceEmptyTextParts(messageID, PLACEHOLDER_TEXT)) {
267+
anySuccess = true
268+
}
247269
if (injectTextPart(sessionID, messageID, PLACEHOLDER_TEXT)) {
248270
anySuccess = true
249271
}

src/hooks/session-recovery/storage.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,55 @@ export function stripThinkingParts(messageID: string): boolean {
271271
return anyRemoved
272272
}
273273

274+
export function replaceEmptyTextParts(messageID: string, replacementText: string): boolean {
275+
const partDir = join(PART_STORAGE, messageID)
276+
if (!existsSync(partDir)) return false
277+
278+
let anyReplaced = false
279+
for (const file of readdirSync(partDir)) {
280+
if (!file.endsWith(".json")) continue
281+
try {
282+
const filePath = join(partDir, file)
283+
const content = readFileSync(filePath, "utf-8")
284+
const part = JSON.parse(content) as StoredPart
285+
286+
if (part.type === "text") {
287+
const textPart = part as StoredTextPart
288+
if (!textPart.text?.trim()) {
289+
textPart.text = replacementText
290+
textPart.synthetic = true
291+
writeFileSync(filePath, JSON.stringify(textPart, null, 2))
292+
anyReplaced = true
293+
}
294+
}
295+
} catch {
296+
continue
297+
}
298+
}
299+
300+
return anyReplaced
301+
}
302+
303+
export function findMessagesWithEmptyTextParts(sessionID: string): string[] {
304+
const messages = readMessages(sessionID)
305+
const result: string[] = []
306+
307+
for (const msg of messages) {
308+
const parts = readParts(msg.id)
309+
const hasEmptyTextPart = parts.some((p) => {
310+
if (p.type !== "text") return false
311+
const textPart = p as StoredTextPart
312+
return !textPart.text?.trim()
313+
})
314+
315+
if (hasEmptyTextPart) {
316+
result.push(msg.id)
317+
}
318+
}
319+
320+
return result
321+
}
322+
274323
export function findMessageByIndexNeedingThinking(sessionID: string, targetIndex: number): string | null {
275324
const messages = readMessages(sessionID)
276325

0 commit comments

Comments
 (0)