dsh-foxy-memory 0.4.4 → 0.4.6
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 +14 -4
- package/lib/client.js +51 -15
- package/lib/index.js +147 -26
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -74,24 +74,34 @@ 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
|
## 记忆浏览界面
|
|
88
95
|
|
|
89
|
-
安装并重启 DSH
|
|
96
|
+
安装并重启 DSH 后,入口在 **Foxy → 记忆**。页面提供:
|
|
90
97
|
|
|
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'
|
|
@@ -74,8 +74,8 @@ window.__ModuleLoader__.load({
|
|
|
74
74
|
const [presetId, setPresetId] = useState('')
|
|
75
75
|
const [presetBusy, setPresetBusy] = useState(false)
|
|
76
76
|
|
|
77
|
-
const load = useCallback(async () => {
|
|
78
|
-
setLoading(true)
|
|
77
|
+
const load = useCallback(async (silent = false) => {
|
|
78
|
+
if (!silent) setLoading(true)
|
|
79
79
|
setError('')
|
|
80
80
|
try {
|
|
81
81
|
const params = new URLSearchParams({
|
|
@@ -87,7 +87,7 @@ window.__ModuleLoader__.load({
|
|
|
87
87
|
params.set('q', query.trim())
|
|
88
88
|
const result = await request(`/search?${params.toString()}`)
|
|
89
89
|
setItems(result.items || [])
|
|
90
|
-
setStats(result.stats ||
|
|
90
|
+
setStats((current) => result.stats || current)
|
|
91
91
|
} else {
|
|
92
92
|
const result = await request(`/list?${params.toString()}`)
|
|
93
93
|
setItems(result.items || [])
|
|
@@ -96,7 +96,7 @@ window.__ModuleLoader__.load({
|
|
|
96
96
|
} catch (cause) {
|
|
97
97
|
setError(cause instanceof Error ? cause.message : String(cause))
|
|
98
98
|
} finally {
|
|
99
|
-
setLoading(false)
|
|
99
|
+
if (!silent) setLoading(false)
|
|
100
100
|
}
|
|
101
101
|
}, [platform, query, status])
|
|
102
102
|
|
|
@@ -111,9 +111,29 @@ window.__ModuleLoader__.load({
|
|
|
111
111
|
}
|
|
112
112
|
}, [])
|
|
113
113
|
|
|
114
|
+
const loadRef = useRef(load)
|
|
115
|
+
const queryRef = useRef(query)
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
loadRef.current = load
|
|
118
|
+
queryRef.current = query
|
|
119
|
+
}, [load, query])
|
|
120
|
+
|
|
114
121
|
useEffect(() => {
|
|
115
122
|
void load()
|
|
116
123
|
void loadPresets()
|
|
124
|
+
// 面板常驻时持续拉取 Koishi 新记忆;用户正在搜索或页面隐藏时暂停。
|
|
125
|
+
const timer = window.setInterval(() => {
|
|
126
|
+
if (document.hidden || queryRef.current.trim()) return
|
|
127
|
+
void loadRef.current(true)
|
|
128
|
+
}, 15000)
|
|
129
|
+
const onVisible = () => {
|
|
130
|
+
if (!document.hidden) void loadRef.current(true)
|
|
131
|
+
}
|
|
132
|
+
document.addEventListener('visibilitychange', onVisible)
|
|
133
|
+
return () => {
|
|
134
|
+
window.clearInterval(timer)
|
|
135
|
+
document.removeEventListener('visibilitychange', onVisible)
|
|
136
|
+
}
|
|
117
137
|
}, [])
|
|
118
138
|
|
|
119
139
|
const linkPreset = async () => {
|
|
@@ -165,12 +185,15 @@ window.__ModuleLoader__.load({
|
|
|
165
185
|
null,
|
|
166
186
|
h('p', { className: 'fm-eyebrow' }, 'FOXY EXTENSION'),
|
|
167
187
|
h('h1', null, 'Foxy Memory'),
|
|
168
|
-
h('p', { className: 'fm-subtitle' }, '跨平台记忆浏览、检索与归档。QQ、微博、B站与 DSH 共用同一套 Foxy
|
|
188
|
+
h('p', { className: 'fm-subtitle' }, '跨平台记忆浏览、检索与归档。QQ、微博、B站与 DSH 共用同一套 Foxy 记忆,每 15 秒自动同步。'),
|
|
169
189
|
),
|
|
170
190
|
h(
|
|
171
191
|
'div',
|
|
172
192
|
{ className: 'fm-header-actions' },
|
|
173
|
-
h('
|
|
193
|
+
h('span', { className: 'fm-sync' }, stats.syncedAt
|
|
194
|
+
? `已同步 ${formatTime(stats.syncedAt)}`
|
|
195
|
+
: stats.provider === 'livingmemory' ? (loading ? '同步中…' : '等待同步') : '本地记忆'),
|
|
196
|
+
h('button', { type: 'button', className: 'fm-button', onClick: () => void load(), disabled: loading }, loading ? '刷新中…' : '刷新'),
|
|
174
197
|
),
|
|
175
198
|
),
|
|
176
199
|
error ? h('div', { className: 'fm-alert fm-alert-error' }, error) : null,
|
|
@@ -235,7 +258,7 @@ window.__ModuleLoader__.load({
|
|
|
235
258
|
h('option', { value: '*' }, '全部状态'),
|
|
236
259
|
h('option', { value: 'archived' }, '已归档'),
|
|
237
260
|
),
|
|
238
|
-
h('button', { type: 'button', className: 'fm-button', onClick: load, disabled: loading }, '查询'),
|
|
261
|
+
h('button', { type: 'button', className: 'fm-button', onClick: () => void load(), disabled: loading }, '查询'),
|
|
239
262
|
),
|
|
240
263
|
h(
|
|
241
264
|
'section',
|
|
@@ -272,7 +295,8 @@ window.__ModuleLoader__.load({
|
|
|
272
295
|
.fm-eyebrow { margin: 0 0 8px; color: var(--dsw-alias-label-tertiary, #6b7280); font-size: 10px; font-weight: 700; letter-spacing: .16em; }
|
|
273
296
|
.fm-header h1 { margin: 0; font-size: 24px; line-height: 1.25; letter-spacing: -.02em; }
|
|
274
297
|
.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; }
|
|
298
|
+
.fm-header-actions { display: flex; gap: 8px; align-items: center; }
|
|
299
|
+
.fm-sync { color: var(--dsw-alias-label-tertiary, #6b7280); font-size: 11px; white-space: nowrap; }
|
|
276
300
|
.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
301
|
.fm-button:hover { background: var(--dsw-alias-interactive-bg-hover, #f1f5f9); }
|
|
278
302
|
.fm-button:disabled { opacity: .55; cursor: default; }
|
|
@@ -304,6 +328,23 @@ window.__ModuleLoader__.load({
|
|
|
304
328
|
const name = 'dsh-foxy-memory'
|
|
305
329
|
const inject = ['slots']
|
|
306
330
|
|
|
331
|
+
function registerConsolePage() {
|
|
332
|
+
const root = window.__FOXY_CONSOLE__ || (window.__FOXY_CONSOLE__ = { pages: [], listeners: new Set() })
|
|
333
|
+
if (!Array.isArray(root.pages)) root.pages = []
|
|
334
|
+
if (!(root.listeners instanceof Set)) root.listeners = new Set()
|
|
335
|
+
const entry = { id: PANEL_ID, label: '记忆', order: 15, Component: Panel }
|
|
336
|
+
const index = root.pages.findIndex((page) => page.id === entry.id)
|
|
337
|
+
if (index >= 0) root.pages[index] = entry
|
|
338
|
+
else root.pages.push(entry)
|
|
339
|
+
for (const listener of root.listeners) {
|
|
340
|
+
try {
|
|
341
|
+
listener()
|
|
342
|
+
} catch {
|
|
343
|
+
// 单个面板的渲染错误不应影响其他标签页
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
307
348
|
function apply(ctx) {
|
|
308
349
|
const style = document.createElement('style')
|
|
309
350
|
style.setAttribute('data-plugin-css', 'dsh-foxy-memory')
|
|
@@ -311,18 +352,13 @@ window.__ModuleLoader__.load({
|
|
|
311
352
|
document.head.appendChild(style)
|
|
312
353
|
ctx.effect(() => () => style.remove(), 'dsh-foxy-memory: style')
|
|
313
354
|
|
|
355
|
+
registerConsolePage()
|
|
314
356
|
ctx.slots.inject('main', () =>
|
|
315
357
|
ctx.slots.register(
|
|
316
358
|
{ name: 'main', key: PANEL_ID, inject: () => ({ workbench: true }) },
|
|
317
359
|
() => h(Panel),
|
|
318
360
|
),
|
|
319
361
|
)
|
|
320
|
-
ctx.slots.inject('sidebar.panellist', () =>
|
|
321
|
-
ctx.slots.register(
|
|
322
|
-
{ name: 'sidebar.panellist', id: PANEL_ID, order: 15, label: () => LABEL },
|
|
323
|
-
MemoryIcon,
|
|
324
|
-
),
|
|
325
|
-
)
|
|
326
362
|
ctx.slots.inject('settings.section', () =>
|
|
327
363
|
ctx.slots.register(
|
|
328
364
|
{ name: 'settings.section', id: 'foxy-memory', order: 33, label: LABEL },
|
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,88 @@ export function apply(ctx, config = {}) {
|
|
|
343
351
|
return data
|
|
344
352
|
}
|
|
345
353
|
|
|
354
|
+
// Koishi 侧返回的分面统计按全集计算,仅用于展示来源平台数量。
|
|
355
|
+
let bridgeSummary = { total: 0, platforms: {}, syncedAt: 0 }
|
|
356
|
+
|
|
357
|
+
function bridgePlatforms(value) {
|
|
358
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return {}
|
|
359
|
+
return Object.fromEntries(Object.entries(value).filter(([, count]) => Number.isFinite(Number(count))))
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
async function syncBridge() {
|
|
363
|
+
const result = await bridgePost('/list', {
|
|
364
|
+
presetId: bridgePresetId,
|
|
365
|
+
status: 'active',
|
|
366
|
+
page: 1,
|
|
367
|
+
pageSize: 1,
|
|
368
|
+
})
|
|
369
|
+
const total = Number(result.total)
|
|
370
|
+
const platforms = bridgePlatforms(result.facets?.platforms)
|
|
371
|
+
bridgeSummary = {
|
|
372
|
+
total: Number.isFinite(total) ? total : bridgeSummary.total,
|
|
373
|
+
platforms: Object.keys(platforms).length ? platforms : bridgeSummary.platforms,
|
|
374
|
+
syncedAt: Date.now(),
|
|
375
|
+
}
|
|
376
|
+
return bridgeSummary
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
async function bridgeListPage(input = {}) {
|
|
380
|
+
const status = input.status === '*' ? 'all' : input.status || 'active'
|
|
381
|
+
const result = await bridgePost('/list', {
|
|
382
|
+
presetId: bridgePresetId,
|
|
383
|
+
platform: input.platform,
|
|
384
|
+
status,
|
|
385
|
+
keyword: input.keyword,
|
|
386
|
+
page: input.page || 1,
|
|
387
|
+
pageSize: input.limit || input.pageSize || 50,
|
|
388
|
+
})
|
|
389
|
+
const items = Array.isArray(result.items) ? result.items : []
|
|
390
|
+
const total = Number(result.total)
|
|
391
|
+
const platforms = bridgePlatforms(result.facets?.platforms)
|
|
392
|
+
bridgeSummary = {
|
|
393
|
+
// 仅无筛选的活跃列表代表整个记忆池的总数。
|
|
394
|
+
total: status === 'active' && !input.platform && !input.keyword && Number(input.page || 1) === 1 && Number.isFinite(total)
|
|
395
|
+
? total
|
|
396
|
+
: bridgeSummary.total,
|
|
397
|
+
platforms: Object.keys(platforms).length ? platforms : bridgeSummary.platforms,
|
|
398
|
+
syncedAt: Date.now(),
|
|
399
|
+
}
|
|
400
|
+
return { items, total: Number.isFinite(total) ? total : items.length }
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function bridgePlatformLabel(value) {
|
|
404
|
+
const platform = String(value || '').toLowerCase()
|
|
405
|
+
if (platform === 'qq') return 'QQ'
|
|
406
|
+
if (platform === 'weibo') return '微博'
|
|
407
|
+
if (platform === 'bilibili') return 'B站'
|
|
408
|
+
if (platform === 'dsh') return 'DSH'
|
|
409
|
+
return value || 'Koishi'
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function bridgeMemoryLine(item) {
|
|
413
|
+
const raw = String(item?.content || item?.summary || '').replace(/\s+/g, ' ').trim()
|
|
414
|
+
const content = clip(raw, maxContentChars)
|
|
415
|
+
if (!content) return ''
|
|
416
|
+
const timestamp = Number(new Date(item?.updatedAt || item?.createdAt))
|
|
417
|
+
const date = Number.isFinite(timestamp) ? new Date(timestamp).toISOString().slice(0, 16).replace('T', ' ') : ''
|
|
418
|
+
const platform = bridgePlatformLabel(item?.platform)
|
|
419
|
+
return `- [${[date, platform].filter(Boolean).join(' ')}] ${content}`
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// 无检索词的回忆取 Koishi 最新记忆;预算超出时优先保留最新条目。
|
|
423
|
+
function formatBridgeMemories(items, limit) {
|
|
424
|
+
const lines = []
|
|
425
|
+
let size = 0
|
|
426
|
+
for (const item of items.slice(0, limit)) {
|
|
427
|
+
const line = bridgeMemoryLine(item)
|
|
428
|
+
if (!line) continue
|
|
429
|
+
if (size + line.length > maxContextChars) break
|
|
430
|
+
lines.push(line)
|
|
431
|
+
size += line.length + 1
|
|
432
|
+
}
|
|
433
|
+
return lines.reverse().join('\n')
|
|
434
|
+
}
|
|
435
|
+
|
|
346
436
|
async function persistBridgePreset(presetId) {
|
|
347
437
|
const value = String(presetId || '').trim()
|
|
348
438
|
if (!value) throw new Error('presetId is required')
|
|
@@ -353,6 +443,7 @@ export function apply(ctx, config = {}) {
|
|
|
353
443
|
saved.bridgePresetId = value
|
|
354
444
|
await mkdir(dirname(settingsFile), { recursive: true })
|
|
355
445
|
await writeFile(settingsFile, `${JSON.stringify(saved, null, 2)}\n`, 'utf8')
|
|
446
|
+
void syncBridge().catch(() => {})
|
|
356
447
|
return { ok: true, presetId: value }
|
|
357
448
|
}
|
|
358
449
|
|
|
@@ -391,39 +482,48 @@ export function apply(ctx, config = {}) {
|
|
|
391
482
|
}
|
|
392
483
|
},
|
|
393
484
|
async context(input = {}) {
|
|
485
|
+
const limit = Math.min(maxContextEntries, Math.max(1, Number(input.limit) || maxContextEntries))
|
|
486
|
+
const query = String(input.query || '').trim()
|
|
394
487
|
try {
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
488
|
+
if (query) {
|
|
489
|
+
const result = await bridgePost('/context', {
|
|
490
|
+
presetId: bridgePresetId,
|
|
491
|
+
sessionId: input.sessionId,
|
|
492
|
+
query,
|
|
493
|
+
limit,
|
|
494
|
+
})
|
|
495
|
+
if (result.text) return result.text
|
|
496
|
+
} else {
|
|
497
|
+
const page = await bridgeListPage({ status: 'active', limit })
|
|
498
|
+
const text = formatBridgeMemories(page.items, limit)
|
|
499
|
+
if (text) return text
|
|
500
|
+
}
|
|
402
501
|
} catch (error) {
|
|
403
502
|
ctx.logger?.warn?.(`[dsh-foxy-memory] bridge context failed, falling back to local: ${error instanceof Error ? error.message : String(error)}`)
|
|
404
503
|
}
|
|
405
|
-
return context(input)
|
|
504
|
+
return context({ ...input, limit })
|
|
406
505
|
},
|
|
407
506
|
async list(input = {}) {
|
|
408
507
|
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 : []
|
|
508
|
+
return (await bridgeListPage(input)).items
|
|
418
509
|
} catch (error) {
|
|
419
510
|
ctx.logger?.warn?.(`[dsh-foxy-memory] bridge list failed, falling back to local: ${error instanceof Error ? error.message : String(error)}`)
|
|
420
511
|
return list(input)
|
|
421
512
|
}
|
|
422
513
|
},
|
|
514
|
+
async listPage(input = {}) {
|
|
515
|
+
try {
|
|
516
|
+
return await bridgeListPage(input)
|
|
517
|
+
} catch (error) {
|
|
518
|
+
ctx.logger?.warn?.(`[dsh-foxy-memory] bridge list failed, falling back to local: ${error instanceof Error ? error.message : String(error)}`)
|
|
519
|
+
const items = await list(input)
|
|
520
|
+
return { items, total: items.length }
|
|
521
|
+
}
|
|
522
|
+
},
|
|
423
523
|
async search(query, input = {}) {
|
|
424
524
|
try {
|
|
425
525
|
const result = await bridgePost('/search', {
|
|
426
|
-
presetId:
|
|
526
|
+
presetId: bridgePresetId,
|
|
427
527
|
query,
|
|
428
528
|
platform: input.platform,
|
|
429
529
|
limit: input.limit || 20,
|
|
@@ -448,7 +548,10 @@ export function apply(ctx, config = {}) {
|
|
|
448
548
|
archive,
|
|
449
549
|
stats() {
|
|
450
550
|
const pending = [...pendingBySession.values()].reduce((sum, list) => sum + list.length, 0)
|
|
451
|
-
|
|
551
|
+
const synced = bridgeSummary.syncedAt
|
|
552
|
+
? { total: bridgeSummary.total, platforms: bridgeSummary.platforms, syncedAt: bridgeSummary.syncedAt }
|
|
553
|
+
: {}
|
|
554
|
+
return { ...stats(), ...synced, provider: 'livingmemory', bridgeUrl, bridgePresetId, pending }
|
|
452
555
|
},
|
|
453
556
|
})
|
|
454
557
|
: null
|
|
@@ -462,6 +565,14 @@ export function apply(ctx, config = {}) {
|
|
|
462
565
|
: null
|
|
463
566
|
flushTimer = setInterval(() => { void flushAll() }, flushIntervalMs)
|
|
464
567
|
flushTimer.unref?.()
|
|
568
|
+
let syncTimer
|
|
569
|
+
if (bridgeProvider && active === bridgeProvider) {
|
|
570
|
+
void syncBridge().catch((error) => ctx.logger?.warn?.(`[dsh-foxy-memory] bridge sync failed: ${error instanceof Error ? error.message : String(error)}`))
|
|
571
|
+
syncTimer = setInterval(() => {
|
|
572
|
+
void syncBridge().catch((error) => ctx.logger?.warn?.(`[dsh-foxy-memory] bridge sync failed: ${error instanceof Error ? error.message : String(error)}`))
|
|
573
|
+
}, syncIntervalMs)
|
|
574
|
+
syncTimer.unref?.()
|
|
575
|
+
}
|
|
465
576
|
|
|
466
577
|
if (typeof ctx.provide === 'function') ctx.provide('foxyMemory', active)
|
|
467
578
|
ctx.logger?.info?.(`[dsh-foxy-memory] ready; provider=${active.name}; file=${entriesFile}${bridgeUrl ? `; bridge=${bridgeUrl}` : ''}`)
|
|
@@ -516,8 +627,17 @@ export function apply(ctx, config = {}) {
|
|
|
516
627
|
page: Number(url.searchParams.get('page')) || 1,
|
|
517
628
|
limit: Number(url.searchParams.get('pageSize')) || 50,
|
|
518
629
|
}
|
|
519
|
-
const
|
|
520
|
-
|
|
630
|
+
const page = typeof active.listPage === 'function'
|
|
631
|
+
? await active.listPage(input)
|
|
632
|
+
: { items: await active.list(input) }
|
|
633
|
+
const items = Array.isArray(page.items) ? page.items : []
|
|
634
|
+
const total = Number(page.total)
|
|
635
|
+
return json({
|
|
636
|
+
ok: true,
|
|
637
|
+
items,
|
|
638
|
+
total: Number.isFinite(total) ? total : items.length,
|
|
639
|
+
stats: typeof active.stats === 'function' ? active.stats() : {},
|
|
640
|
+
})
|
|
521
641
|
})
|
|
522
642
|
|
|
523
643
|
route('/api/dsh-foxy-memory/search', ['GET'], async (request) => {
|
|
@@ -551,6 +671,7 @@ export function apply(ctx, config = {}) {
|
|
|
551
671
|
provider: active,
|
|
552
672
|
dispose: () => {
|
|
553
673
|
clearInterval(flushTimer)
|
|
674
|
+
clearInterval(syncTimer)
|
|
554
675
|
void flushAll()
|
|
555
676
|
disposeLocal()
|
|
556
677
|
disposeBridge?.()
|
package/package.json
CHANGED