fix(chat): keep messages chronological across ID rollover

This commit is contained in:
Bohdan Triapitsyn
2026-08-14 16:53:05 +03:00
parent 7cf869d5eb
commit fe1f6130d6
23 changed files with 405 additions and 132 deletions
+4 -5
View File
@@ -1,5 +1,6 @@
import type { Message, Part } from '@opencode-ai/sdk/v2/client';
import type { State } from './types';
import { messagesBefore } from './message-ordering';
type UserMessageHistoryRecord = {
message: Message;
@@ -62,14 +63,12 @@ export const buildUserMessageHistorySnapshot = (
const session = state.session.find((candidate) => candidate.id === sessionID);
const revertMessageID = (session as { revert?: { messageID?: string } } | undefined)?.revert?.messageID;
const records: UserMessageHistoryRecord[] = [];
for (let index = messages.length - 1; index >= 0; index -= 1) {
const message = messages[index];
const visibleMessages = messagesBefore(messages, revertMessageID);
for (let index = visibleMessages.length - 1; index >= 0; index -= 1) {
const message = visibleMessages[index];
if (message.role !== 'user') {
continue;
}
if (revertMessageID && message.id >= revertMessageID) {
continue;
}
records.push({
message,
parts: state.part[message.id] ?? EMPTY_PARTS,