fix: restore messages after redo
Refetches session messages before applying redo Aligns undo/redo navigation with OpenCode behavior Keeps restored messages visible after unrevert
This commit is contained in:
@@ -13,6 +13,11 @@ import { useGlobalSessionsStore } from "@/stores/useGlobalSessionsStore"
|
||||
import { useConfigStore } from "@/stores/useConfigStore"
|
||||
import { registerSessionDirectory } from "./sync-refs"
|
||||
import { isSyntheticPart } from "@/lib/messages/synthetic"
|
||||
import { materializeSessionSnapshots } from "./materialization"
|
||||
import { stripMessageDiffSnapshots } from "./sanitize"
|
||||
|
||||
const MESSAGE_REFETCH_LIMIT = 200
|
||||
const MESSAGE_REFETCH_SKIP_PARTS = new Set(["patch", "step-start", "step-finish"])
|
||||
|
||||
// Reference set by SyncProvider — allows actions to access SDK and stores
|
||||
let _sdk: OpencodeClient | null = null
|
||||
@@ -647,6 +652,26 @@ export async function revertToMessage(sessionId: string, messageId: string): Pro
|
||||
}
|
||||
}
|
||||
|
||||
export async function refetchSessionMessages(sessionId: string): Promise<void> {
|
||||
const store = dirStore()
|
||||
const result = await sdk().session.messages({ sessionID: sessionId, directory: dir(), limit: MESSAGE_REFETCH_LIMIT })
|
||||
const records = (result.data ?? []).filter((record: { info?: { id?: string } }) => !!record?.info?.id)
|
||||
if (records.length === 0) return
|
||||
|
||||
store.setState((state) => {
|
||||
const materialized = materializeSessionSnapshots(
|
||||
state,
|
||||
sessionId,
|
||||
records.map((record: { info: Message; parts?: Part[] }) => ({
|
||||
info: stripMessageDiffSnapshots(record.info),
|
||||
parts: record.parts ?? [],
|
||||
})),
|
||||
{ skipPartTypes: MESSAGE_REFETCH_SKIP_PARTS },
|
||||
)
|
||||
return { message: materialized.message, part: materialized.part }
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Unrevert — restore all previously reverted messages.
|
||||
* Restore all previously reverted messages. Aborts if busy, merges result.
|
||||
@@ -675,6 +700,7 @@ export async function unrevertSession(sessionId: string): Promise<void> {
|
||||
store.setState({ session: sessions })
|
||||
}
|
||||
}
|
||||
await refetchSessionMessages(sessionId)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -46,6 +46,7 @@ import {
|
||||
shareSession as shareSessionAction,
|
||||
unshareSession as unshareSessionAction,
|
||||
optimisticSend,
|
||||
refetchSessionMessages,
|
||||
} from "./session-actions"
|
||||
import { useInputStore, type SyntheticContextPart } from "./input-store"
|
||||
import { useSelectionStore } from "./selection-store"
|
||||
@@ -967,8 +968,7 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({
|
||||
const revertToId = currentSession?.revert?.messageID
|
||||
let targetMessage: typeof messages[number] | undefined
|
||||
if (revertToId) {
|
||||
const revertIndex = userMessages.findIndex((m) => m.id === revertToId)
|
||||
targetMessage = userMessages[revertIndex + 1]
|
||||
targetMessage = [...userMessages].reverse().find((m) => m.id < revertToId)
|
||||
} else {
|
||||
targetMessage = userMessages[userMessages.length - 1]
|
||||
}
|
||||
@@ -996,10 +996,11 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({
|
||||
const revertToId = currentSession?.revert?.messageID
|
||||
if (!revertToId) return
|
||||
|
||||
await refetchSessionMessages(sessionId)
|
||||
|
||||
const messages = getSyncMessages(sessionId)
|
||||
const userMessages = messages.filter((m) => m.role === "user")
|
||||
const revertIndex = userMessages.findIndex((m) => m.id === revertToId)
|
||||
const targetMessage = userMessages[revertIndex - 1]
|
||||
const targetMessage = userMessages.find((m) => m.id > revertToId)
|
||||
|
||||
if (targetMessage) {
|
||||
const targetParts = getSyncParts(targetMessage.id)
|
||||
|
||||
Reference in New Issue
Block a user