dsh-foxy-memory 0.4.5 → 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/README.md +13 -3
- package/lib/client.js +58 -10
- package/lib/index.js +170 -26
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -74,14 +74,21 @@ Provider 为 `livingmemory` 时,设置页的「Koishi 记忆预设」会从 Ko
|
|
|
74
74
|
| `provider` | `livingmemory`(配置 bridgeUrl 时)/ `local` | 记忆写到哪个 provider |
|
|
75
75
|
| `bridgeUrl` | 空 | 新插件地址,如 `http://127.0.0.1:5140/foxy-memory` |
|
|
76
76
|
| `bridgeToken` | 空 | 与新插件 `foxyApiToken` 一致;公网部署必须设置 |
|
|
77
|
-
| `bridgePresetId` | `
|
|
77
|
+
| `bridgePresetId` | `foxy3(Character)` | 读写共用的 Koishi 预设池 |
|
|
78
78
|
| `bridgeTimeoutMs` | `15000` | 桥请求超时 |
|
|
79
79
|
| `batchSize` | `8` | 累积多少条消息后调用 `/ingest` |
|
|
80
80
|
| `flushIntervalMs` | `30000` | 未满一批时的定时冲刷间隔 |
|
|
81
|
+
| `syncIntervalMs` | `60000` | 从桥拉取记忆池总数与平台分布的间隔,最小 5000 |
|
|
81
82
|
| `extractModel` | `deepseek/deepseek-flash` | 桥提取记忆使用的模型 |
|
|
82
83
|
|
|
83
84
|
DSH 侧不再逐条写记忆:`record` 只把消息放进 pending,满 `batchSize` 或到 `flushIntervalMs` 时调用桥的 `/ingest`,由模型提炼成中文第一人称记忆。`stats()` 会显示 `pending`。
|
|
84
85
|
|
|
86
|
+
Provider 为 `livingmemory` 时,读取接口全部绑定 `bridgePresetId`:DSH 的人格 `presetId` 不会传到桥,链接哪个 Koishi 预设,回忆、列表与搜索就使用哪个记忆池。
|
|
87
|
+
|
|
88
|
+
- 带 `query` 的回忆调用桥的 `/foxy-memory/context`,保留语义检索与用户画像;
|
|
89
|
+
- 不带 `query` 的回忆(DSH 每轮会话开始时就是这种)直接取桥里最新的活跃记忆,避免用固定检索词碰运气;
|
|
90
|
+
- 插件启动、定时器、记忆落盘和切换预设后都会拉取一次记忆池统计,`stats().total`、`platforms`、`syncedAt` 反映 Koishi 侧的真实数据,而不是本地 JSONL 条数。
|
|
91
|
+
|
|
85
92
|
Foxy 面板的「记忆桥设置」可以修改这些值,保存到 `$DSH_HOME/foxy/settings.json`;插件启动时读取,保存后尝试热重载。`/api/dsh-foxy/settings` 在 DSH host 内提供。
|
|
86
93
|
|
|
87
94
|
## 记忆浏览界面
|
|
@@ -91,7 +98,10 @@ Foxy 面板的「记忆桥设置」可以修改这些值,保存到 `$DSH_HOME/
|
|
|
91
98
|
- 记忆总数、待写入数、来源平台数和当前 provider;
|
|
92
99
|
- 按平台(QQ / 微博 / B站 / DSH)、状态筛选;
|
|
93
100
|
- 按内容、说话人、关键词搜索;
|
|
94
|
-
-
|
|
101
|
+
- 单条归档,默认只显示活跃记忆;
|
|
102
|
+
- 面板每 15 秒自动拉取一次(页面隐藏或正在输入搜索词时暂停),切回页面时立即刷新;标题旁显示最近同步时间。
|
|
103
|
+
|
|
104
|
+
Bridge 模式下「记忆总数」与「来源平台」来自 Koishi `/foxy-memory/list` 的 `total` 与 `facets`;「全部状态」会映射成 Koishi 的 `status=all`。
|
|
95
105
|
|
|
96
106
|
Host 路由:
|
|
97
107
|
|
|
@@ -103,7 +113,7 @@ POST /api/dsh-foxy-memory/archive {"id":"..."}
|
|
|
103
113
|
POST /api/dsh-foxy-memory/reload
|
|
104
114
|
```
|
|
105
115
|
|
|
106
|
-
`/list` 和 `/search` 在 `provider: "livingmemory"` 时走新记忆插件的 `/foxy-memory/list|search`,失败自动回退本地 JSONL
|
|
116
|
+
`/list` 和 `/search` 在 `provider: "livingmemory"` 时走新记忆插件的 `/foxy-memory/list|search`,失败自动回退本地 JSONL;`/list` 的 `total` 是当前筛选条件下的 Koishi 条数。
|
|
107
117
|
|
|
108
118
|
## 服务
|
|
109
119
|
|
package/lib/client.js
CHANGED
|
@@ -3,7 +3,7 @@ window.__ModuleLoader__.load({
|
|
|
3
3
|
factory: (require) => {
|
|
4
4
|
const React = require('react')
|
|
5
5
|
const h = React.createElement
|
|
6
|
-
const { useCallback, useEffect, useState } = React
|
|
6
|
+
const { useCallback, useEffect, useRef, useState } = React
|
|
7
7
|
|
|
8
8
|
const API = '/api/dsh-foxy-memory'
|
|
9
9
|
const PANEL_ID = 'dsh-foxy-memory'
|
|
@@ -73,9 +73,16 @@ 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)
|
|
76
77
|
|
|
77
|
-
const
|
|
78
|
-
|
|
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
|
+
})
|
|
83
|
+
|
|
84
|
+
const load = useCallback(async (silent = false) => {
|
|
85
|
+
if (!silent) setLoading(true)
|
|
79
86
|
setError('')
|
|
80
87
|
try {
|
|
81
88
|
const params = new URLSearchParams({
|
|
@@ -87,16 +94,16 @@ window.__ModuleLoader__.load({
|
|
|
87
94
|
params.set('q', query.trim())
|
|
88
95
|
const result = await request(`/search?${params.toString()}`)
|
|
89
96
|
setItems(result.items || [])
|
|
90
|
-
setStats(result.stats ||
|
|
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) {
|
|
97
104
|
setError(cause instanceof Error ? cause.message : String(cause))
|
|
98
105
|
} finally {
|
|
99
|
-
setLoading(false)
|
|
106
|
+
if (!silent) setLoading(false)
|
|
100
107
|
}
|
|
101
108
|
}, [platform, query, status])
|
|
102
109
|
|
|
@@ -111,9 +118,29 @@ window.__ModuleLoader__.load({
|
|
|
111
118
|
}
|
|
112
119
|
}, [])
|
|
113
120
|
|
|
121
|
+
const loadRef = useRef(load)
|
|
122
|
+
const queryRef = useRef(query)
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
loadRef.current = load
|
|
125
|
+
queryRef.current = query
|
|
126
|
+
}, [load, query])
|
|
127
|
+
|
|
114
128
|
useEffect(() => {
|
|
115
129
|
void load()
|
|
116
130
|
void loadPresets()
|
|
131
|
+
// 面板常驻时持续拉取 Koishi 新记忆;用户正在搜索或页面隐藏时暂停。
|
|
132
|
+
const timer = window.setInterval(() => {
|
|
133
|
+
if (document.hidden || queryRef.current.trim()) return
|
|
134
|
+
void loadRef.current(true)
|
|
135
|
+
}, 15000)
|
|
136
|
+
const onVisible = () => {
|
|
137
|
+
if (!document.hidden) void loadRef.current(true)
|
|
138
|
+
}
|
|
139
|
+
document.addEventListener('visibilitychange', onVisible)
|
|
140
|
+
return () => {
|
|
141
|
+
window.clearInterval(timer)
|
|
142
|
+
document.removeEventListener('visibilitychange', onVisible)
|
|
143
|
+
}
|
|
117
144
|
}, [])
|
|
118
145
|
|
|
119
146
|
const linkPreset = async () => {
|
|
@@ -136,6 +163,21 @@ window.__ModuleLoader__.load({
|
|
|
136
163
|
}
|
|
137
164
|
}
|
|
138
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
|
+
|
|
139
181
|
const archive = async (item) => {
|
|
140
182
|
if (!item?.id) return
|
|
141
183
|
if (!window.confirm('归档这条记忆?归档后默认列表不再显示。')) return
|
|
@@ -165,15 +207,20 @@ window.__ModuleLoader__.load({
|
|
|
165
207
|
null,
|
|
166
208
|
h('p', { className: 'fm-eyebrow' }, 'FOXY EXTENSION'),
|
|
167
209
|
h('h1', null, 'Foxy Memory'),
|
|
168
|
-
h('p', { className: 'fm-subtitle' }, '跨平台记忆浏览、检索与归档。QQ、微博、B站与 DSH 共用同一套 Foxy
|
|
210
|
+
h('p', { className: 'fm-subtitle' }, '跨平台记忆浏览、检索与归档。QQ、微博、B站与 DSH 共用同一套 Foxy 记忆,每 15 秒自动同步。'),
|
|
169
211
|
),
|
|
170
212
|
h(
|
|
171
213
|
'div',
|
|
172
214
|
{ className: 'fm-header-actions' },
|
|
173
|
-
h('
|
|
215
|
+
h('span', { className: 'fm-sync' }, stats.syncedAt
|
|
216
|
+
? `已同步 ${formatTime(stats.syncedAt)}`
|
|
217
|
+
: stats.provider === 'livingmemory' ? (loading ? '同步中…' : '等待同步') : '本地记忆'),
|
|
218
|
+
h('button', { type: 'button', className: 'fm-button', onClick: syncNow, disabled: syncBusy }, syncBusy ? '同步中…' : '立即同步'),
|
|
219
|
+
h('button', { type: 'button', className: 'fm-button', onClick: () => void load(), disabled: loading }, loading ? '刷新中…' : '刷新'),
|
|
174
220
|
),
|
|
175
221
|
),
|
|
176
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,
|
|
177
224
|
notice ? h('div', { className: 'fm-alert' }, notice) : null,
|
|
178
225
|
h(
|
|
179
226
|
'section',
|
|
@@ -235,7 +282,7 @@ window.__ModuleLoader__.load({
|
|
|
235
282
|
h('option', { value: '*' }, '全部状态'),
|
|
236
283
|
h('option', { value: 'archived' }, '已归档'),
|
|
237
284
|
),
|
|
238
|
-
h('button', { type: 'button', className: 'fm-button', onClick: load, disabled: loading }, '查询'),
|
|
285
|
+
h('button', { type: 'button', className: 'fm-button', onClick: () => void load(), disabled: loading }, '查询'),
|
|
239
286
|
),
|
|
240
287
|
h(
|
|
241
288
|
'section',
|
|
@@ -272,7 +319,8 @@ window.__ModuleLoader__.load({
|
|
|
272
319
|
.fm-eyebrow { margin: 0 0 8px; color: var(--dsw-alias-label-tertiary, #6b7280); font-size: 10px; font-weight: 700; letter-spacing: .16em; }
|
|
273
320
|
.fm-header h1 { margin: 0; font-size: 24px; line-height: 1.25; letter-spacing: -.02em; }
|
|
274
321
|
.fm-subtitle { max-width: 660px; margin: 7px 0 0; color: var(--dsw-alias-label-secondary, #6b7280); font-size: 12px; line-height: 1.65; }
|
|
275
|
-
.fm-header-actions { display: flex; gap: 8px; }
|
|
322
|
+
.fm-header-actions { display: flex; gap: 8px; align-items: center; }
|
|
323
|
+
.fm-sync { color: var(--dsw-alias-label-tertiary, #6b7280); font-size: 11px; white-space: nowrap; }
|
|
276
324
|
.fm-button { border: 1px solid var(--dsw-alias-bg-layer-3, #e5e7eb); background: var(--dsw-alias-bg-layer-2, #f8fafc); color: inherit; border-radius: 7px; padding: 8px 13px; font-size: 12px; cursor: pointer; }
|
|
277
325
|
.fm-button:hover { background: var(--dsw-alias-interactive-bg-hover, #f1f5f9); }
|
|
278
326
|
.fm-button:disabled { opacity: .55; cursor: default; }
|
package/lib/index.js
CHANGED
|
@@ -66,7 +66,7 @@ export function apply(ctx, config = {}) {
|
|
|
66
66
|
const settingsFile = join(process.env.DSH_HOME || join(homedir(), '.dsh'), 'foxy', 'settings.json')
|
|
67
67
|
try {
|
|
68
68
|
const saved = JSON.parse(readFileSync(settingsFile, 'utf8'))
|
|
69
|
-
for (const key of ['provider', 'bridgeUrl', 'bridgeToken', 'bridgePresetId', 'batchSize', 'flushIntervalMs', 'extractModel']) {
|
|
69
|
+
for (const key of ['provider', 'bridgeUrl', 'bridgeToken', 'bridgePresetId', 'batchSize', 'flushIntervalMs', 'syncIntervalMs', 'extractModel']) {
|
|
70
70
|
if (saved[key] !== undefined) config[key] = saved[key]
|
|
71
71
|
}
|
|
72
72
|
} catch {}
|
|
@@ -213,14 +213,19 @@ export function apply(ctx, config = {}) {
|
|
|
213
213
|
return lines.reverse().join('\n')
|
|
214
214
|
}
|
|
215
215
|
|
|
216
|
-
async function
|
|
216
|
+
async function listPage(input = {}) {
|
|
217
217
|
await ready
|
|
218
218
|
const presetId = input.presetId === undefined ? undefined : String(input.presetId)
|
|
219
219
|
const status = input.status === undefined ? 'active' : String(input.status)
|
|
220
220
|
const limit = Math.min(500, Math.max(1, Number(input.limit) || 50))
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
221
|
+
const matched = [...entries.values()].filter(
|
|
222
|
+
(entry) => (presetId === undefined || entry.presetId === presetId) && (status === '*' || entry.status === status),
|
|
223
|
+
)
|
|
224
|
+
return { items: matched.slice(-limit), total: matched.length }
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async function list(input = {}) {
|
|
228
|
+
return (await listPage(input)).items
|
|
224
229
|
}
|
|
225
230
|
|
|
226
231
|
async function search(query, input = {}) {
|
|
@@ -267,6 +272,7 @@ export function apply(ctx, config = {}) {
|
|
|
267
272
|
record,
|
|
268
273
|
context,
|
|
269
274
|
list,
|
|
275
|
+
listPage,
|
|
270
276
|
search,
|
|
271
277
|
archive,
|
|
272
278
|
stats,
|
|
@@ -279,6 +285,7 @@ export function apply(ctx, config = {}) {
|
|
|
279
285
|
|
|
280
286
|
const batchSize = Math.max(1, Number(config.batchSize) || 8)
|
|
281
287
|
const flushIntervalMs = Math.max(1000, Number(config.flushIntervalMs) || 30000)
|
|
288
|
+
const syncIntervalMs = Math.max(5000, Number(config.syncIntervalMs) || 60000)
|
|
282
289
|
const pendingBySession = new Map()
|
|
283
290
|
let flushTimer
|
|
284
291
|
|
|
@@ -306,6 +313,7 @@ export function apply(ctx, config = {}) {
|
|
|
306
313
|
messages,
|
|
307
314
|
})
|
|
308
315
|
pendingBySession.delete(key)
|
|
316
|
+
void syncBridge().catch(() => {})
|
|
309
317
|
ctx.logger?.info?.(`[dsh-foxy-memory] flushed ${messages.length} messages -> ${result.created || 0} memories`)
|
|
310
318
|
return result
|
|
311
319
|
} catch (error) {
|
|
@@ -343,6 +351,100 @@ export function apply(ctx, config = {}) {
|
|
|
343
351
|
return data
|
|
344
352
|
}
|
|
345
353
|
|
|
354
|
+
// Koishi 侧返回的分面统计按全集计算,仅用于展示来源平台数量。
|
|
355
|
+
let bridgeSummary = { total: 0, platforms: {}, syncedAt: 0 }
|
|
356
|
+
let lastSyncError = ''
|
|
357
|
+
|
|
358
|
+
function bridgePlatforms(value) {
|
|
359
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return {}
|
|
360
|
+
return Object.fromEntries(Object.entries(value).filter(([, count]) => Number.isFinite(Number(count))))
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
async function syncBridge() {
|
|
364
|
+
const result = await bridgePost('/list', {
|
|
365
|
+
presetId: bridgePresetId,
|
|
366
|
+
status: 'active',
|
|
367
|
+
page: 1,
|
|
368
|
+
pageSize: 1,
|
|
369
|
+
})
|
|
370
|
+
const total = Number(result.total)
|
|
371
|
+
const platforms = bridgePlatforms(result.facets?.platforms)
|
|
372
|
+
bridgeSummary = {
|
|
373
|
+
total: Number.isFinite(total) ? total : bridgeSummary.total,
|
|
374
|
+
platforms: Object.keys(platforms).length ? platforms : bridgeSummary.platforms,
|
|
375
|
+
syncedAt: Date.now(),
|
|
376
|
+
}
|
|
377
|
+
lastSyncError = ''
|
|
378
|
+
return bridgeSummary
|
|
379
|
+
}
|
|
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
|
+
|
|
391
|
+
async function bridgeListPage(input = {}) {
|
|
392
|
+
const status = input.status === '*' ? 'all' : input.status || 'active'
|
|
393
|
+
const result = await bridgePost('/list', {
|
|
394
|
+
presetId: bridgePresetId,
|
|
395
|
+
platform: input.platform,
|
|
396
|
+
status,
|
|
397
|
+
keyword: input.keyword,
|
|
398
|
+
page: input.page || 1,
|
|
399
|
+
pageSize: input.limit || input.pageSize || 50,
|
|
400
|
+
})
|
|
401
|
+
const items = Array.isArray(result.items) ? result.items : []
|
|
402
|
+
const total = Number(result.total)
|
|
403
|
+
const platforms = bridgePlatforms(result.facets?.platforms)
|
|
404
|
+
bridgeSummary = {
|
|
405
|
+
// 仅无筛选的活跃列表代表整个记忆池的总数。
|
|
406
|
+
total: status === 'active' && !input.platform && !input.keyword && Number(input.page || 1) === 1 && Number.isFinite(total)
|
|
407
|
+
? total
|
|
408
|
+
: bridgeSummary.total,
|
|
409
|
+
platforms: Object.keys(platforms).length ? platforms : bridgeSummary.platforms,
|
|
410
|
+
syncedAt: Date.now(),
|
|
411
|
+
}
|
|
412
|
+
return { items, total: Number.isFinite(total) ? total : items.length }
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function bridgePlatformLabel(value) {
|
|
416
|
+
const platform = String(value || '').toLowerCase()
|
|
417
|
+
if (platform === 'qq') return 'QQ'
|
|
418
|
+
if (platform === 'weibo') return '微博'
|
|
419
|
+
if (platform === 'bilibili') return 'B站'
|
|
420
|
+
if (platform === 'dsh') return 'DSH'
|
|
421
|
+
return value || 'Koishi'
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function bridgeMemoryLine(item) {
|
|
425
|
+
const raw = String(item?.content || item?.summary || '').replace(/\s+/g, ' ').trim()
|
|
426
|
+
const content = clip(raw, maxContentChars)
|
|
427
|
+
if (!content) return ''
|
|
428
|
+
const timestamp = Number(new Date(item?.updatedAt || item?.createdAt))
|
|
429
|
+
const date = Number.isFinite(timestamp) ? new Date(timestamp).toISOString().slice(0, 16).replace('T', ' ') : ''
|
|
430
|
+
const platform = bridgePlatformLabel(item?.platform)
|
|
431
|
+
return `- [${[date, platform].filter(Boolean).join(' ')}] ${content}`
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// 无检索词的回忆取 Koishi 最新记忆;预算超出时优先保留最新条目。
|
|
435
|
+
function formatBridgeMemories(items, limit) {
|
|
436
|
+
const lines = []
|
|
437
|
+
let size = 0
|
|
438
|
+
for (const item of items.slice(0, limit)) {
|
|
439
|
+
const line = bridgeMemoryLine(item)
|
|
440
|
+
if (!line) continue
|
|
441
|
+
if (size + line.length > maxContextChars) break
|
|
442
|
+
lines.push(line)
|
|
443
|
+
size += line.length + 1
|
|
444
|
+
}
|
|
445
|
+
return lines.reverse().join('\n')
|
|
446
|
+
}
|
|
447
|
+
|
|
346
448
|
async function persistBridgePreset(presetId) {
|
|
347
449
|
const value = String(presetId || '').trim()
|
|
348
450
|
if (!value) throw new Error('presetId is required')
|
|
@@ -353,6 +455,7 @@ export function apply(ctx, config = {}) {
|
|
|
353
455
|
saved.bridgePresetId = value
|
|
354
456
|
await mkdir(dirname(settingsFile), { recursive: true })
|
|
355
457
|
await writeFile(settingsFile, `${JSON.stringify(saved, null, 2)}\n`, 'utf8')
|
|
458
|
+
void syncBridge().catch(() => {})
|
|
356
459
|
return { ok: true, presetId: value }
|
|
357
460
|
}
|
|
358
461
|
|
|
@@ -391,39 +494,48 @@ export function apply(ctx, config = {}) {
|
|
|
391
494
|
}
|
|
392
495
|
},
|
|
393
496
|
async context(input = {}) {
|
|
497
|
+
const limit = Math.min(maxContextEntries, Math.max(1, Number(input.limit) || maxContextEntries))
|
|
498
|
+
const query = String(input.query || '').trim()
|
|
394
499
|
try {
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
500
|
+
if (query) {
|
|
501
|
+
const result = await bridgePost('/context', {
|
|
502
|
+
presetId: bridgePresetId,
|
|
503
|
+
sessionId: input.sessionId,
|
|
504
|
+
query,
|
|
505
|
+
limit,
|
|
506
|
+
})
|
|
507
|
+
if (result.text) return result.text
|
|
508
|
+
} else {
|
|
509
|
+
const page = await bridgeListPage({ status: 'active', limit })
|
|
510
|
+
const text = formatBridgeMemories(page.items, limit)
|
|
511
|
+
if (text) return text
|
|
512
|
+
}
|
|
402
513
|
} catch (error) {
|
|
403
514
|
ctx.logger?.warn?.(`[dsh-foxy-memory] bridge context failed, falling back to local: ${error instanceof Error ? error.message : String(error)}`)
|
|
404
515
|
}
|
|
405
|
-
return context(input)
|
|
516
|
+
return context({ ...input, limit })
|
|
406
517
|
},
|
|
407
518
|
async list(input = {}) {
|
|
408
519
|
try {
|
|
409
|
-
|
|
410
|
-
presetId: input.presetId || bridgePresetId,
|
|
411
|
-
platform: input.platform,
|
|
412
|
-
status: input.status || 'active',
|
|
413
|
-
keyword: input.keyword,
|
|
414
|
-
page: input.page || 1,
|
|
415
|
-
pageSize: input.limit || input.pageSize || 50,
|
|
416
|
-
})
|
|
417
|
-
return Array.isArray(result.items) ? result.items : []
|
|
520
|
+
return (await bridgeListPage(input)).items
|
|
418
521
|
} catch (error) {
|
|
419
522
|
ctx.logger?.warn?.(`[dsh-foxy-memory] bridge list failed, falling back to local: ${error instanceof Error ? error.message : String(error)}`)
|
|
420
523
|
return list(input)
|
|
421
524
|
}
|
|
422
525
|
},
|
|
526
|
+
async listPage(input = {}) {
|
|
527
|
+
try {
|
|
528
|
+
return await bridgeListPage(input)
|
|
529
|
+
} catch (error) {
|
|
530
|
+
ctx.logger?.warn?.(`[dsh-foxy-memory] bridge list failed, falling back to local: ${error instanceof Error ? error.message : String(error)}`)
|
|
531
|
+
const items = await list(input)
|
|
532
|
+
return { items, total: items.length }
|
|
533
|
+
}
|
|
534
|
+
},
|
|
423
535
|
async search(query, input = {}) {
|
|
424
536
|
try {
|
|
425
537
|
const result = await bridgePost('/search', {
|
|
426
|
-
presetId:
|
|
538
|
+
presetId: bridgePresetId,
|
|
427
539
|
query,
|
|
428
540
|
platform: input.platform,
|
|
429
541
|
limit: input.limit || 20,
|
|
@@ -445,10 +557,16 @@ export function apply(ctx, config = {}) {
|
|
|
445
557
|
async linkPreset(presetId) {
|
|
446
558
|
return await persistBridgePreset(presetId)
|
|
447
559
|
},
|
|
560
|
+
async sync() {
|
|
561
|
+
return await syncNow()
|
|
562
|
+
},
|
|
448
563
|
archive,
|
|
449
564
|
stats() {
|
|
450
565
|
const pending = [...pendingBySession.values()].reduce((sum, list) => sum + list.length, 0)
|
|
451
|
-
|
|
566
|
+
const synced = bridgeSummary.syncedAt
|
|
567
|
+
? { total: bridgeSummary.total, platforms: bridgeSummary.platforms, syncedAt: bridgeSummary.syncedAt }
|
|
568
|
+
: {}
|
|
569
|
+
return { ...stats(), ...synced, provider: 'livingmemory', bridgeUrl, bridgePresetId, pending, lastSyncError }
|
|
452
570
|
},
|
|
453
571
|
})
|
|
454
572
|
: null
|
|
@@ -462,6 +580,14 @@ export function apply(ctx, config = {}) {
|
|
|
462
580
|
: null
|
|
463
581
|
flushTimer = setInterval(() => { void flushAll() }, flushIntervalMs)
|
|
464
582
|
flushTimer.unref?.()
|
|
583
|
+
let syncTimer
|
|
584
|
+
if (bridgeProvider && active === bridgeProvider) {
|
|
585
|
+
void syncBridge().catch((error) => ctx.logger?.warn?.(`[dsh-foxy-memory] bridge sync failed: ${error instanceof Error ? error.message : String(error)}`))
|
|
586
|
+
syncTimer = setInterval(() => {
|
|
587
|
+
void syncBridge().catch((error) => ctx.logger?.warn?.(`[dsh-foxy-memory] bridge sync failed: ${error instanceof Error ? error.message : String(error)}`))
|
|
588
|
+
}, syncIntervalMs)
|
|
589
|
+
syncTimer.unref?.()
|
|
590
|
+
}
|
|
465
591
|
|
|
466
592
|
if (typeof ctx.provide === 'function') ctx.provide('foxyMemory', active)
|
|
467
593
|
ctx.logger?.info?.(`[dsh-foxy-memory] ready; provider=${active.name}; file=${entriesFile}${bridgeUrl ? `; bridge=${bridgeUrl}` : ''}`)
|
|
@@ -489,6 +615,14 @@ export function apply(ctx, config = {}) {
|
|
|
489
615
|
json({ ok: true, stats: typeof active.stats === 'function' ? active.stats() : {} }),
|
|
490
616
|
)
|
|
491
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
|
+
|
|
492
626
|
route('/api/dsh-foxy-memory/presets', ['GET'], async () => {
|
|
493
627
|
if (typeof active.listPresets !== 'function') {
|
|
494
628
|
return json({ ok: false, error: '当前 Provider 不支持 Koishi 预设列表,请切换到 livingmemory Bridge' }, 400)
|
|
@@ -516,8 +650,17 @@ export function apply(ctx, config = {}) {
|
|
|
516
650
|
page: Number(url.searchParams.get('page')) || 1,
|
|
517
651
|
limit: Number(url.searchParams.get('pageSize')) || 50,
|
|
518
652
|
}
|
|
519
|
-
const
|
|
520
|
-
|
|
653
|
+
const page = typeof active.listPage === 'function'
|
|
654
|
+
? await active.listPage(input)
|
|
655
|
+
: { items: await active.list(input) }
|
|
656
|
+
const items = Array.isArray(page.items) ? page.items : []
|
|
657
|
+
const total = Number(page.total)
|
|
658
|
+
return json({
|
|
659
|
+
ok: true,
|
|
660
|
+
items,
|
|
661
|
+
total: Number.isFinite(total) ? total : items.length,
|
|
662
|
+
stats: typeof active.stats === 'function' ? active.stats() : {},
|
|
663
|
+
})
|
|
521
664
|
})
|
|
522
665
|
|
|
523
666
|
route('/api/dsh-foxy-memory/search', ['GET'], async (request) => {
|
|
@@ -551,6 +694,7 @@ export function apply(ctx, config = {}) {
|
|
|
551
694
|
provider: active,
|
|
552
695
|
dispose: () => {
|
|
553
696
|
clearInterval(flushTimer)
|
|
697
|
+
clearInterval(syncTimer)
|
|
554
698
|
void flushAll()
|
|
555
699
|
disposeLocal()
|
|
556
700
|
disposeBridge?.()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-foxy-memory",
|
|
3
|
-
"version": "0.4.
|
|
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
|
+
|