@@ -3,7 +3,7 @@ import { computed, ref, watch } from 'vue'
33import { useI18n } from ' vue-i18n'
44import { apiFetch } from ' @/shared/api'
55import { store } from ' @/shared/store'
6- import { useToast , useConfirm , SButton , SModal , SBadge , SSelect , STabBar , STab } from ' sbot-ui'
6+ import { useToast , useConfirm , SButton , SModal , SBadge , SInput , SSelect , STabBar , STab } from ' sbot-ui'
77
88interface MemorySummary {
99 slug: string
@@ -29,6 +29,18 @@ interface MemoryJob {
2929 updatedAt: number
3030}
3131
32+ interface MemoryHistoryEntry {
33+ hash: string
34+ shortHash: string
35+ committedAt: number
36+ message: string
37+ }
38+
39+ interface MemoryHistoryDiff extends MemoryHistoryEntry {
40+ diff: string
41+ restorable: boolean
42+ }
43+
3244const { t } = useI18n ()
3345const { show } = useToast ()
3446const { confirm } = useConfirm ()
@@ -39,18 +51,25 @@ const labelOverride = ref('')
3951const workspaceScopes = ref <WorkspaceScope []>([])
4052const selectedWorkPath = ref (' ' )
4153
42- const tab = ref <' memories' | ' jobs' >(' memories' )
54+ const tab = ref <' memories' | ' history ' | ' jobs' >(' memories' )
4355const loading = ref (false )
4456const jobsLoading = ref (false )
57+ const historyLoading = ref (false )
58+ const diffLoading = ref (false )
4559const bodyLoading = ref (false )
4660const consolidating = ref (false )
4761const reconciling = ref (false )
4862const deleting = ref (false )
63+ const restoring = ref (false )
4964const retryingJobId = ref <number | null >(null )
5065const deletingJobId = ref <number | null >(null )
5166
5267const rows = ref <MemorySummary []>([])
5368const jobs = ref <MemoryJob []>([])
69+ const history = ref <MemoryHistoryEntry []>([])
70+ const historySlug = ref (' ' )
71+ const selectedHistoryHash = ref (' ' )
72+ const selectedHistory = ref <MemoryHistoryDiff | null >(null )
5473const selectedKey = ref (' ' )
5574const selectedBody = ref (' ' )
5675
@@ -72,6 +91,9 @@ async function openByMemoryId(id: string | null | undefined, label?: string) {
7291 tab .value = ' memories'
7392 visible .value = true
7493 selectedWorkPath .value = ' '
94+ historySlug .value = ' '
95+ selectedHistoryHash .value = ' '
96+ selectedHistory .value = null
7597 if (memoryId .value ) {
7698 await loadScopes ()
7799 await loadMemories ()
@@ -86,12 +108,15 @@ async function loadScopes() {
86108async function changeScope() {
87109 selectedKey .value = ' '
88110 selectedBody .value = ' '
111+ selectedHistoryHash .value = ' '
112+ selectedHistory .value = null
89113 await refreshCurrentTab ()
90114}
91115
92116async function refreshCurrentTab() {
93117 if (! memoryId .value ) return
94118 if (tab .value === ' memories' ) await loadMemories ()
119+ else if (tab .value === ' history' ) await loadHistory ()
95120 else await loadJobs ()
96121}
97122
@@ -127,6 +152,66 @@ async function loadJobs() {
127152 }
128153}
129154
155+ async function loadHistory() {
156+ if (! memoryId .value ) return
157+ historyLoading .value = true
158+ try {
159+ const query = [viewQuery .value , ' limit=50' ]
160+ const slug = historySlug .value .trim ()
161+ if (slug ) query .push (` slug=${encodeURIComponent (slug )} ` )
162+ const res = await apiFetch (` /api/memories/${encodeURIComponent (memoryId .value )}/history?${query .join (' &' )} ` )
163+ const list = (res .data ?.history || []) as MemoryHistoryEntry []
164+ history .value = list
165+ const next = list .find (item => item .hash === selectedHistoryHash .value ) || list [0 ]
166+ selectedHistoryHash .value = next ?.hash || ' '
167+ if (next ) await loadHistoryDiff (next )
168+ else selectedHistory .value = null
169+ } catch (e : any ) {
170+ show (e .message , ' error' )
171+ } finally {
172+ historyLoading .value = false
173+ }
174+ }
175+
176+ async function loadHistoryDiff(item : MemoryHistoryEntry ) {
177+ if (! memoryId .value ) return
178+ selectedHistoryHash .value = item .hash
179+ diffLoading .value = true
180+ try {
181+ const query = [viewQuery .value ]
182+ const slug = historySlug .value .trim ()
183+ if (slug ) query .push (` slug=${encodeURIComponent (slug )} ` )
184+ const res = await apiFetch (` /api/memories/${encodeURIComponent (memoryId .value )}/history/${item .hash }?${query .join (' &' )} ` )
185+ selectedHistory .value = res .data ?.history as MemoryHistoryDiff
186+ } catch (e : any ) {
187+ selectedHistory .value = null
188+ show (e .message , ' error' )
189+ } finally {
190+ diffLoading .value = false
191+ }
192+ }
193+
194+ async function restoreHistoryVersion() {
195+ const item = selectedHistory .value
196+ const slug = historySlug .value .trim ()
197+ if (! memoryId .value || ! item || ! slug || restoring .value ) return
198+ if (! await confirm (t (' memory_profiles.confirm_restore_memory' , { slug , commit: item .shortHash }), { danger: true })) return
199+ restoring .value = true
200+ try {
201+ await apiFetch (
202+ ` /api/memories/${encodeURIComponent (memoryId .value )}/history/${item .hash }/restore/${encodeURIComponent (slug )}?${viewQuery .value } ` ,
203+ ' POST' ,
204+ {},
205+ )
206+ show (t (' memory_profiles.restore_memory_done' ))
207+ await loadHistory ()
208+ } catch (e : any ) {
209+ show (e .message , ' error' )
210+ } finally {
211+ restoring .value = false
212+ }
213+ }
214+
130215async function selectMemory(memory : MemorySummary ) {
131216 const key = rowKey (memory )
132217 if (selectedKey .value === key && selectedBody .value ) return
@@ -149,6 +234,13 @@ async function loadBody(memory: MemorySummary) {
149234 }
150235}
151236
237+ function openMemoryHistory(memory : MemorySummary ) {
238+ historySlug .value = memory .slug
239+ selectedHistoryHash .value = ' '
240+ selectedHistory .value = null
241+ tab .value = ' history'
242+ }
243+
152244async function runConsolidate() {
153245 if (! memoryId .value || consolidating .value ) return
154246 consolidating .value = true
@@ -259,6 +351,7 @@ function jobVariant(status: string): 'success' | 'info' | 'warning' | 'danger' |
259351watch (tab , value => {
260352 if (! visible .value || ! memoryId .value ) return
261353 if (value === ' memories' ) loadMemories ()
354+ else if (value === ' history' ) loadHistory ()
262355 else loadJobs ()
263356})
264357
@@ -270,6 +363,7 @@ defineExpose({ openByMemoryId })
270363 <div class =" memory-viewer" >
271364 <STabBar v-model =" tab " class="memory-tabs">
272365 <STab name="memories">{{ t('memory_profiles.viewer_memories') }}</STab >
366+ <STab name="history">{{ t('memory_profiles.viewer_history') }}</STab >
273367 <STab name="jobs">{{ t('memory_profiles.viewer_jobs') }}</STab >
274368 </STabBar >
275369
@@ -329,6 +423,9 @@ defineExpose({ openByMemoryId })
329423 <SBadge :variant =" kindVariant (selected .kind )" size="sm">{{ selected.kind }}</SBadge >
330424 <SBadge variant="neutral" size="sm">{{ t('memory_profiles.evidence') }} {{ selected.evidenceCount }}</SBadge >
331425 <SBadge variant="neutral" size="sm">{{ t('memory_profiles.read_count') }} {{ selected.readCount }}</SBadge >
426+ <SButton type="outline" size="sm" @click =" openMemoryHistory (selected )" >
427+ {{ t('memory_profiles.view_memory_history') }}
428+ </SButton >
332429 <SButton type="danger" size="sm" :loading =" deleting " @click =" deleteMemory (selected )" >
333430 {{ t('memory_profiles.delete_memory') }}
334431 </SButton >
@@ -343,6 +440,73 @@ defineExpose({ openByMemoryId })
343440 </div >
344441 </div >
345442
443+ <div v-else-if =" tab === 'history'" class =" memory-tab-pane" >
444+ <div class =" memory-tab-toolbar memory-history-toolbar" >
445+ <SSelect v-model =" selectedWorkPath " size="sm" @change =" changeScope " >
446+ <option value =" " >{{ t('memory_profiles.scope_global') }}</option >
447+ <option v-for =" scope in workspaceScopes" :key =" scope.key" :value =" scope.path" >
448+ {{ t('memory_profiles.scope_workspace_context', { path: scope.path }) }}
449+ </option >
450+ </SSelect >
451+ <div class =" memory-actions memory-history-filter" >
452+ <SInput
453+ v-model =" historySlug "
454+ size="sm"
455+ :placeholder =" t (' memory_profiles.history_slug_placeholder' )"
456+ @keyup .enter =" loadHistory "
457+ />
458+ <SButton type="outline" size="sm" :loading =" historyLoading " @click =" loadHistory " >
459+ {{ t('memory_profiles.view_history') }}
460+ </SButton >
461+ </div >
462+ </div >
463+
464+ <div class =" memory-pane memory-history-pane" >
465+ <aside class =" memory-list memory-history-list" >
466+ <div v-if =" historyLoading" class =" memory-empty" >{{ t('memory_profiles.loading') }}</div >
467+ <div v-else-if =" history.length === 0" class =" memory-empty" >{{ t('memory_profiles.no_history') }}</div >
468+ <button
469+ v-for =" item in history"
470+ v-else
471+ :key =" item.hash"
472+ class =" memory-row memory-history-row"
473+ :class =" { active: item.hash === selectedHistoryHash }"
474+ @click =" loadHistoryDiff(item)"
475+ >
476+ <div class =" memory-row-head" >
477+ <code class =" memory-history-hash" >{{ item.shortHash }}</code >
478+ <span class =" memory-history-time" >{{ fmtTime(item.committedAt) }}</span >
479+ </div >
480+ <div class =" memory-row-title" >{{ item.message }}</div >
481+ </button >
482+ </aside >
483+
484+ <section class =" memory-detail" >
485+ <div v-if =" selectedHistory" class =" memory-detail-head" >
486+ <div >
487+ <div class =" memory-detail-title" >{{ selectedHistory.message }}</div >
488+ <div class =" memory-detail-slug" >{{ selectedHistory.hash }}</div >
489+ </div >
490+ <SButton
491+ v-if =" historySlug .trim () && selectedHistory .restorable "
492+ type="outline"
493+ size="sm"
494+ :loading =" restoring "
495+ @click =" restoreHistoryVersion "
496+ >
497+ {{ t('memory_profiles.restore_memory') }}
498+ </SButton >
499+ </div >
500+ <div v-if =" selectedHistory" class =" memory-detail-meta memory-history-meta" >
501+ <span >{{ t('memory_profiles.committed_at') }}: {{ fmtTime(selectedHistory.committedAt) }}</span >
502+ <span v-if =" !historySlug.trim()" >{{ t('memory_profiles.restore_requires_slug') }}</span >
503+ <span v-else-if =" !selectedHistory.restorable" >{{ t('memory_profiles.version_deleted') }}</span >
504+ </div >
505+ <pre class =" memory-body memory-diff" >{{ diffLoading ? t('memory_profiles.loading') : (selectedHistory?.diff || t('memory_profiles.no_diff')) }}</pre >
506+ </section >
507+ </div >
508+ </div >
509+
346510 <div v-else class =" memory-tab-pane" >
347511 <div class =" memory-tab-toolbar" >
348512 <SSelect v-model =" selectedWorkPath " size="sm" @change =" changeScope " >
@@ -433,6 +597,15 @@ defineExpose({ openByMemoryId })
433597 white-space : nowrap ;
434598}
435599
600+ .memory-history-filter {
601+ flex : 1 ;
602+ justify-content : flex-end ;
603+ }
604+
605+ .memory-history-filter :deep(.s-input ) {
606+ width : min (320px , 100% );
607+ }
608+
436609.memory-pane {
437610 flex : 1 ;
438611 min-height : 0 ;
@@ -491,11 +664,21 @@ defineExpose({ openByMemoryId })
491664
492665.memory-row-slug ,
493666.memory-detail-slug ,
667+ .memory-history-hash ,
494668.memory-job-grid code {
495669 font-family : var (--sui-font-mono );
496670 font-size : var (--sui-fs-xs );
497671}
498672
673+ .memory-history-time {
674+ color : var (--sui-fg-subtle );
675+ font-size : var (--sui-fs-xs );
676+ }
677+
678+ .memory-history-hash {
679+ color : var (--sui-color-primary );
680+ }
681+
499682/* title 是唯一标签,最长 150 字符——列表里裁到两行,全文在右侧详情区 */
500683.memory-row-title {
501684 font-weight : 600 ;
@@ -558,6 +741,15 @@ defineExpose({ openByMemoryId })
558741 line-height : 1.5 ;
559742}
560743
744+ .memory-diff {
745+ white-space : pre-wrap ;
746+ tab-size : 2 ;
747+ }
748+
749+ .memory-history-meta {
750+ grid-template-columns : minmax (0 , 1fr ) minmax (0 , 1fr );
751+ }
752+
561753.memory-jobs {
562754 flex : 1 ;
563755 min-height : 0 ;
@@ -605,6 +797,14 @@ defineExpose({ openByMemoryId })
605797 align-items : stretch ;
606798 }
607799
800+ .memory-history-filter {
801+ width : 100% ;
802+ }
803+
804+ .memory-history-filter :deep(.s-input ) {
805+ width : 100% ;
806+ }
807+
608808 .memory-pane {
609809 grid-template-columns : 1fr ;
610810 }
0 commit comments