fix: guard memory empty snapshot replay

This commit is contained in:
JOJO 2025-12-15 00:10:00 +08:00
parent 9750b0b8f1
commit 8c304c113f

View File

@ -1384,11 +1384,11 @@ export class MonitorDirector implements MonitorDriver {
}
}
private async ensureMemoryWindowVisible(options: { initialEntries?: string[]; memoryType?: string } = {}) {
const { initialEntries = [], memoryType = 'main' } = options;
private async ensureMemoryWindowVisible(options: { initialEntries?: string[]; snapshotProvided?: boolean; memoryType?: string } = {}) {
const { initialEntries, snapshotProvided = false, memoryType = 'main' } = options;
const hydrate = async () => {
if (initialEntries.length) {
this.renderMemoryEntries(initialEntries);
if (snapshotProvided) {
this.renderMemoryEntries(initialEntries || []);
return;
}
if (!this.getMemoryItems().length) {
@ -1397,7 +1397,7 @@ export class MonitorDirector implements MonitorDriver {
};
if (this.isWindowVisible(this.elements.memoryWindow)) {
this.showWindow(this.elements.memoryWindow);
if (!this.getMemoryItems().length || initialEntries.length) {
if (!this.getMemoryItems().length || snapshotProvided) {
await hydrate();
}
return;
@ -1413,14 +1413,14 @@ export class MonitorDirector implements MonitorDriver {
return Array.from(this.elements.memoryList.children) as HTMLElement[];
}
private extractMemorySnapshotEntries(payload: any, stage: 'before' | 'after' = 'before'): string[] {
private extractMemorySnapshotEntries(payload: any, stage: 'before' | 'after' = 'before'): { entries: string[]; provided: boolean } {
const key = stage === 'after' ? 'monitor_snapshot_after' : 'monitor_snapshot';
const snapshot = payload?.[key];
const entries = snapshot?.entries;
if (Array.isArray(entries)) {
return entries.map(entry => String(entry ?? ''));
return { entries: entries.map(entry => String(entry ?? '')), provided: true };
}
return [];
return { entries: [], provided: !!snapshot };
}
private getMemoryItemByIndex(index: number): HTMLElement | null {
@ -2794,10 +2794,10 @@ export class MonitorDirector implements MonitorDriver {
this.sceneHandlers.memoryUpdate = async (payload, runtime) => {
this.applySceneStatus(runtime, 'memoryUpdate', '正在同步记忆');
const initialEntries = this.extractMemorySnapshotEntries(payload, 'before');
const { entries: initialEntries, provided: snapshotProvided } = this.extractMemorySnapshotEntries(payload, 'before');
const memoryType =
(payload?.arguments?.memory_type || payload?.result?.memory_type || 'main').toString().toLowerCase();
await this.ensureMemoryWindowVisible({ initialEntries, memoryType });
await this.ensureMemoryWindowVisible({ initialEntries, snapshotProvided, memoryType });
const op = (payload?.arguments?.operation || payload?.result?.operation || 'append').toLowerCase();
const content = payload?.arguments?.content || payload?.result?.content || '';