dsh-foxy-memory 0.4.6 → 0.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/client.js CHANGED
@@ -73,6 +73,13 @@ window.__ModuleLoader__.load({
73
73
  const [presets, setPresets] = useState([])
74
74
  const [presetId, setPresetId] = useState('')
75
75
  const [presetBusy, setPresetBusy] = useState(false)
76
+ const [syncBusy, setSyncBusy] = useState(false)
77
+
78
+ const sortOldest = (list) => [...list].sort((a, b) => {
79
+ const left = Date.parse(a.createdAt || a.updatedAt || 0)
80
+ const right = Date.parse(b.createdAt || b.updatedAt || 0)
81
+ return (Number.isFinite(left) ? left : 0) - (Number.isFinite(right) ? right : 0)
82
+ })
76
83
 
77
84
  const load = useCallback(async (silent = false) => {
78
85
  if (!silent) setLoading(true)
@@ -90,7 +97,7 @@ window.__ModuleLoader__.load({
90
97
  setStats((current) => result.stats || current)
91
98
  } else {
92
99
  const result = await request(`/list?${params.toString()}`)
93
- setItems(result.items || [])
100
+ setItems(sortOldest(result.items || []))
94
101
  setStats(result.stats || {})
95
102
  }
96
103
  } catch (cause) {
@@ -156,6 +163,21 @@ window.__ModuleLoader__.load({
156
163
  }
157
164
  }
158
165
 
166
+ const syncNow = async () => {
167
+ setSyncBusy(true)
168
+ setError('')
169
+ setNotice('')
170
+ try {
171
+ const data = await request('/sync', { method: 'POST' })
172
+ setNotice(`已同步 Koishi 记忆:${data.total ?? 0} 条`)
173
+ await load()
174
+ } catch (cause) {
175
+ setError(cause instanceof Error ? cause.message : String(cause))
176
+ } finally {
177
+ setSyncBusy(false)
178
+ }
179
+ }
180
+
159
181
  const archive = async (item) => {
160
182
  if (!item?.id) return
161
183
  if (!window.confirm('归档这条记忆?归档后默认列表不再显示。')) return
@@ -193,10 +215,12 @@ window.__ModuleLoader__.load({
193
215
  h('span', { className: 'fm-sync' }, stats.syncedAt
194
216
  ? `已同步 ${formatTime(stats.syncedAt)}`
195
217
  : stats.provider === 'livingmemory' ? (loading ? '同步中…' : '等待同步') : '本地记忆'),
218
+ h('button', { type: 'button', className: 'fm-button', onClick: syncNow, disabled: syncBusy }, syncBusy ? '同步中…' : '立即同步'),
196
219
  h('button', { type: 'button', className: 'fm-button', onClick: () => void load(), disabled: loading }, loading ? '刷新中…' : '刷新'),
197
220
  ),
198
221
  ),
199
222
  error ? h('div', { className: 'fm-alert fm-alert-error' }, error) : null,
223
+ !error && stats.lastSyncError ? h('div', { className: 'fm-alert fm-alert-error' }, `同步失败:${stats.lastSyncError}`) : null,
200
224
  notice ? h('div', { className: 'fm-alert' }, notice) : null,
201
225
  h(
202
226
  'section',
package/lib/index.js CHANGED
@@ -353,6 +353,7 @@ export function apply(ctx, config = {}) {
353
353
 
354
354
  // Koishi 侧返回的分面统计按全集计算,仅用于展示来源平台数量。
355
355
  let bridgeSummary = { total: 0, platforms: {}, syncedAt: 0 }
356
+ let lastSyncError = ''
356
357
 
357
358
  function bridgePlatforms(value) {
358
359
  if (!value || typeof value !== 'object' || Array.isArray(value)) return {}
@@ -373,9 +374,20 @@ export function apply(ctx, config = {}) {
373
374
  platforms: Object.keys(platforms).length ? platforms : bridgeSummary.platforms,
374
375
  syncedAt: Date.now(),
375
376
  }
377
+ lastSyncError = ''
376
378
  return bridgeSummary
377
379
  }
378
380
 
381
+ async function syncNow() {
382
+ try {
383
+ const summary = await syncBridge()
384
+ return { ok: true, ...summary, provider: 'livingmemory' }
385
+ } catch (error) {
386
+ lastSyncError = error instanceof Error ? error.message : String(error)
387
+ return { ok: false, error: lastSyncError, provider: 'livingmemory' }
388
+ }
389
+ }
390
+
379
391
  async function bridgeListPage(input = {}) {
380
392
  const status = input.status === '*' ? 'all' : input.status || 'active'
381
393
  const result = await bridgePost('/list', {
@@ -545,13 +557,16 @@ export function apply(ctx, config = {}) {
545
557
  async linkPreset(presetId) {
546
558
  return await persistBridgePreset(presetId)
547
559
  },
560
+ async sync() {
561
+ return await syncNow()
562
+ },
548
563
  archive,
549
564
  stats() {
550
565
  const pending = [...pendingBySession.values()].reduce((sum, list) => sum + list.length, 0)
551
566
  const synced = bridgeSummary.syncedAt
552
567
  ? { total: bridgeSummary.total, platforms: bridgeSummary.platforms, syncedAt: bridgeSummary.syncedAt }
553
568
  : {}
554
- return { ...stats(), ...synced, provider: 'livingmemory', bridgeUrl, bridgePresetId, pending }
569
+ return { ...stats(), ...synced, provider: 'livingmemory', bridgeUrl, bridgePresetId, pending, lastSyncError }
555
570
  },
556
571
  })
557
572
  : null
@@ -600,6 +615,14 @@ export function apply(ctx, config = {}) {
600
615
  json({ ok: true, stats: typeof active.stats === 'function' ? active.stats() : {} }),
601
616
  )
602
617
 
618
+ route('/api/dsh-foxy-memory/sync', ['POST'], async () => {
619
+ if (typeof active.sync !== 'function') {
620
+ return json({ ok: false, error: '当前 Provider 不支持手动同步,请切换到 livingmemory Bridge' }, 400)
621
+ }
622
+ const result = await active.sync()
623
+ return json(result.ok ? { ok: true, ...result } : { ok: false, error: result.error }, result.ok ? 200 : 502)
624
+ })
625
+
603
626
  route('/api/dsh-foxy-memory/presets', ['GET'], async () => {
604
627
  if (typeof active.listPresets !== 'function') {
605
628
  return json({ ok: false, error: '当前 Provider 不支持 Koishi 预设列表,请切换到 livingmemory Bridge' }, 400)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-foxy-memory",
3
- "version": "0.4.6",
3
+ "version": "0.4.8",
4
4
  "description": "Memory subplugin for dsh-foxy: local JSONL archive plus optional cross-platform Foxy Memory API, recall and search.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -45,3 +45,4 @@
45
45
  ],
46
46
  "license": "MIT"
47
47
  }
48
+