New package @magpie-community/gateway-aelios 0.1.0: asynchronous gateway hooks for Aelios memory, by sakisakisa-design (Aelios's author).
- Recall before each human turn, appended after the last user message. Recording of completed turns is opt-in (record: false by default).
- Traffic goes only to the user's own serverUrl (HTTPS or loopback HTTP, redirects refused, a key required). Only local and magpie-key origins by default.
- Local admission: an authenticated, dialogue-free GET /v1/hook/identities decides which model maps to which identity. Unmatched turns, or an unavailable list, send no dialogue.
- The request is slimmed (system prompts, thinking and tool results dropped, historical credentials masked). The recorded reply is slimmed to text and tool calls before the secret check and the send.
- Settings page served by a loopback Bun service (Host, Origin null and per-process token checks).
- The cleanMessageText/appendMemory logic ported from AGPL Aelios is relicensed MIT by its sole author (PR thread).
Run at the reviewed head 620cd47 on mote (macOS, Bun 1.4.2):
- bun test packages/aelios: 178 pass. Full HOME=$(mktemp -d) bun test: 4176 pass, 0 fail. Merged onto main 39de030: 4179 pass, 0 fail. check.mjs: exit 0 at both.
- Broken by hand: recording the raw reply turns 5 tests red; admitting every model turns 11 red.
- Probe against a fake Aelios: no thinking, reasoning, signature or encrypted_content is recorded for Anthropic, Responses or Chat. A secret in visible text skips the record. An unmatched model, or an identities endpoint answering 404, malformed JSON or a network error, sends only the GET.
- Real magpie Bun host (origin/main ceeb73a0f, GatewayBegin/Finish): recall injected, the slimmed record sent, nothing for an unmatched model or a remote origin.
- PR CI: test red only on gcp-ai's browser test 40s timeout (#471, unrelated, passes on mote).
Not tried: a real Aelios deployment, or a real agent behind a running magpie.
54 lines
3.0 KiB
JavaScript
54 lines
3.0 KiB
JavaScript
/* Pure helpers shared by the classic browser script and standalone tests. */
|
|
globalThis.aeliosForm = (() => {
|
|
const reserved = new Set(['v1', 'api', 'admin', 'health', 'mcp', 'memory-mcp', 'memory-admin', 'guide-dog']);
|
|
const fail = (key, field) => { throw Object.assign(new Error(key), { field }); };
|
|
function selectors(value, field, models = false) {
|
|
if (!value.trim()) return undefined;
|
|
const values = value.split(',').map((v) => v.trim());
|
|
if (values.some((v) => !v || (models && v.length > 200))) fail('invalidSelectors', field);
|
|
return values;
|
|
}
|
|
function build(previous, values) {
|
|
if (!previous || typeof previous !== 'object' || Array.isArray(previous)) fail('invalidOptions');
|
|
const next = { ...previous, recall: values.recall, record: values.record, allowRemote: values.allowRemote };
|
|
const optional = (name, value) => { if (value) next[name] = value; else delete next[name]; };
|
|
const raw = values.url.trim();
|
|
if (raw) {
|
|
let url;
|
|
try { url = new URL(raw); } catch { fail('invalidUrl', 'url'); }
|
|
const local = ['localhost', '127.0.0.1', '[::1]'].includes(url.hostname);
|
|
if (url.username || url.password || url.search || url.hash || !(url.protocol === 'https:' || (url.protocol === 'http:' && local))) fail('invalidUrl', 'url');
|
|
optional('serverUrl', url.href);
|
|
} else delete next.serverUrl;
|
|
const key = values.key.trim().replace(/^Bearer\s+/i, '');
|
|
if (key && /[\s\u0000-\u001f\u007f]/.test(key)) fail('invalidKey', 'key');
|
|
optional('apiKey', key);
|
|
optional('apiKeyEnv', values.keyEnv.trim());
|
|
const identity = values.identity.trim();
|
|
if (identity.length > 128) fail('invalidIdentity', 'identity');
|
|
optional('identity', identity);
|
|
optional('agents', selectors(values.agents, 'agents'));
|
|
if (values.routes.length > 64) fail('tooManyRoutes', 'add-route');
|
|
const routes = values.routes.map((route, i) => {
|
|
const identity = route.identity.trim();
|
|
if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$/.test(identity) || reserved.has(identity.toLowerCase())) fail('invalidRoute', `route-${i}-identity`);
|
|
const models = selectors(route.models, `route-${i}-models`, true);
|
|
const agents = selectors(route.agents, `route-${i}-agents`);
|
|
if (!models && !agents) fail('missingSelector', `route-${i}-models`);
|
|
const out = { ...route.original, identity };
|
|
if (models) out.models = models; else delete out.models;
|
|
if (agents) out.agents = agents; else delete out.agents;
|
|
return out;
|
|
});
|
|
optional('routes', routes.length ? routes : undefined);
|
|
// The host otherwise gives hooks only 1000 ms. Keep an explicit user value.
|
|
next.gatewayTimeoutMs ??= 5000;
|
|
for (const [name, fallback, min, max] of [['gatewayTimeoutMs', 5000, 100, 10000], ['requestTimeoutMs', 4000, 50, 9000], ['recallBackoffMs', 30000, 0, 3600000]]) {
|
|
const value = next[name] ?? fallback;
|
|
if (!Number.isInteger(value) || value < min || value > max) fail('invalidAdvanced');
|
|
}
|
|
return next;
|
|
}
|
|
return { build, selectors };
|
|
})();
|