diff --git a/static/src/components/chat/monitor/MonitorDirector.ts b/static/src/components/chat/monitor/MonitorDirector.ts index 118f2a1..f0ac7c6 100644 --- a/static/src/components/chat/monitor/MonitorDirector.ts +++ b/static/src/components/chat/monitor/MonitorDirector.ts @@ -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 || '';