Files
plugins/packages/respire-memory/page/settings.js
T
luodaoyi 21cbeeefe0 respire-memory 0.3.6: per-turn recall and policy reads, verified background archival (luodaoyi)
Adds the respire-memory gateway middleware (packages/respire-memory, 0.3.6) by luodaoyi, with its registry.json entry and README row. It recalls from a loopback Respire Runtime on each new turn, reads the engine policy each turn and puts it in the history once (again only when it changes), keeps turn state in an encrypted store, and optionally archives turns in a detached worker through a user-configured Chat Completions service. Diary writes are confirmed by a bounded fragment readback.

Run at 7aba754 on mote (Node 22.22.0, Bun 1.4.2):
- packages/respire-memory: `node scripts/check.mjs` ok (4 protocol, policy dedup/change/revert, byte-identical prefixes, failed read, diary readback); `bun test` 1 pass.
- root: `HOME=$(mktemp -d) bun test` 4187 pass, 9 skip, 0 fail (4196 tests, 328 files); `bun scripts/check.mjs` exit 0.
- Review probe (fake loopback Runtime, 2000-byte policy, six chat turns with a host restart at turn 3, a policy change, then the Runtime down), Bun and Node: per-turn overhead grows ~524 bytes a turn (the read receipt), not the policy; no earlier prefix changed; one policy read per turn; two policy blocks after the change; with the Runtime down the latest turn is unchanged. With no Runtime at all the body passes through unchanged.
- Capture probe: with capture on, a turn whose JSON exceeds the ~4000-char readback budget is written and then recorded unconfirmed, and archival skips it. The PR's check asserts this and the README documents it.
- Harm check: no new dependencies, install scripts or workflow changes; network only to the loopback Runtime and the user's own generation URL.

Not tried: a real Respire Runtime.
2026-10-31 21:36:56 +08:00

56 lines
3.4 KiB
JavaScript

const $ = id => document.getElementById(id);
const status = message => { $('status').textContent = message; };
let busy = false;
magpie.ready.then(async () => {
try {
const o = await magpie.options.get();
$('url').value = o.runtimeUrl ?? 'http://127.0.0.1:15169';
$('project').value = o.project ?? '';
$('remember').checked = o.remember !== false;
$('capture').checked = o.capture === true;
$('readOnly').checked = o.readOnly === true;
$('archive').checked = o.archive !== false;
$('showMemoryStatus').checked = o.showMemoryStatus === true;
for (const name of ['generationBaseUrl', 'generationModel', 'stateDir']) $(name).value = o[name] ?? '';
$('authorizationEnv').value = o.authorizationEnv ?? 'RESPIRE_MAGPIE_AUTHORIZATION';
$('save').disabled = false;
status('配置已读取。这里不检测 Runtime 连接状态。');
} catch { status('无法读取配置,请检查 Magpie 插件页面接口。'); }
}).catch(() => status('Magpie 页面接口不可用。'));
$('form').onsubmit = async event => {
event.preventDefault();
if (busy) return;
let url;
try { url = new URL($('url').value.trim()); } catch { status('请输入有效的本地 Runtime 地址。'); return; }
if (!['http:', 'https:'].includes(url.protocol) || !['127.0.0.1', '[::1]'].includes(url.hostname) ||
url.href.includes('@') || url.search || url.hash || url.pathname !== '/') {
status('地址只允许 127.0.0.1 或 [::1],不接受凭据、路径或查询参数。'); return;
}
const project = $('project').value.trim();
if (/[\x00-\x1f\x7f]/.test(project)) { status('项目名称不能包含控制字符。'); return; }
const generationBaseUrl = $('generationBaseUrl').value.trim(), generationModel = $('generationModel').value.trim();
const authorizationEnv = $('authorizationEnv').value.trim(), stateDir = $('stateDir').value.trim();
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(authorizationEnv)) { status('请填写合法的环境变量名称,不要填写密钥。'); return; }
if ($('archive').checked && (!generationBaseUrl || !generationModel)) { status('启用后台整理前,请填写生成服务地址和模型名称。'); return; }
if (generationBaseUrl) {
let service;
try { service = new URL(generationBaseUrl); } catch { status('生成服务地址无效。'); return; }
if (service.href.includes('@') || service.search || service.hash || !(service.protocol === 'https:' || service.protocol === 'http:' && ['127.0.0.1', '[::1]'].includes(service.hostname))) {
status('生成服务必须使用 HTTPS,或字面回环地址的 HTTP;不接受 URL 凭据和查询参数。'); return;
}
}
if (stateDir && !/^(?:\/|[A-Za-z]:[\\/]|\\\\)/.test(stateDir)) { status('状态目录必须是绝对路径。'); return; }
busy = true; $('save').disabled = true;
try {
const previous = await magpie.options.get();
await magpie.options.set({ ...previous, runtimeUrl: url.href, project,
remember: $('remember').checked, capture: $('capture').checked, readOnly: $('readOnly').checked,
showMemoryStatus: $('showMemoryStatus').checked, archive: $('archive').checked,
generationBaseUrl, generationModel, authorizationEnv, stateDir });
status('配置已保存,将用于后续请求。');
} catch { status('保存未确认,请重新打开页面核对配置后再操作。'); }
finally { busy = false; $('save').disabled = false; }
};