Files
plugins/packages/fingerprint/src/gateway.ts
T
KaranocaVe 00f4d84b8f fingerprint 0.1.0: a page that checks whether a model answers like the one it claims to be (#516)
Adds the Fingerprint page plugin (packages/fingerprint): it checks whether a model behind the local magpie gateway answers like the model it claims to be.

Reviewed at 531ca11f4 (KaranocaVe). Run at that head merged with main 848497c, through mote-run on the Mac mini with Bun 1.4.2:
- bun run rebuild from the committed bun.lock (frozen, --ignore-scripts): typecheck, 30/30 tests, build; diff -r against the committed dist/ is empty.
- HOME=$(mktemp -d) bun test: 3838 pass, 0 fail. bun scripts/check.mjs: exit 0.
- Offline preload refusing every non-loopback fetch/socket: 30/30, nothing refused.
- Broken by hand, each red: ensureDetector() in bootstrap, dropping ttl: Infinity, validating the job after the download.

Maintainer decision: the reference data comes from two third-party hosts (lm.ikale.io, cdn.jsdmirror.com), only on an explicit Download or a valid job, and both are named on the page, in registry.json and the READMEs. It is data used locally to rank candidates and is sent nowhere; the @latest fallback has no pinned hash, so a bad snapshot would rank wrongly, which the page states.

Not on npm yet (E404): magpie installs it from the dist branch until an owner publishes the first version.
2026-10-31 02:00:33 +08:00

115 lines
4.2 KiB
TypeScript

import type { Account, Model, Provenance } from './types';
import { boundedText } from './http';
export class Gateway {
constructor(
readonly url: string,
private key = '',
) {}
headers(extra: Record<string, string> = {}) {
return {
'content-type': 'application/json',
...(this.key ? { authorization: `Bearer ${this.key}` } : {}),
...extra,
};
}
redact(message: unknown) {
return String(message instanceof Error ? message.message : message)
.replaceAll(this.key || '\u0000', '[redacted]')
.slice(0, 500);
}
async catalog(): Promise<{ models: Model[]; accounts: Account[] }> {
const res = await fetch(`${this.url}/v1/models`, {
headers: this.headers(),
signal: AbortSignal.timeout(5000),
redirect: 'error',
});
if (!res.ok) {
await res.body?.cancel();
throw new Error(`Magpie model catalog returned HTTP ${res.status}.`);
}
const data = JSON.parse(await boundedText(res, 4 * 1024 * 1024)) as {
data?: Record<string, any>[];
};
const models = (data.data || [])
.filter((m) => {
if (typeof m.id !== 'string' || !m.id.includes('/') || m.id.startsWith('group/'))
return false;
if (/^(embedding|image|audio)(_|$)/.test(m.type || '')) return false;
if (Array.isArray(m.modalities?.output) && !m.modalities.output.includes('text'))
return false;
const native = Array.isArray(m.native_endpoints) ? m.native_endpoints : [];
return (
!native.length ||
!native.every(
(endpoint: unknown) =>
typeof endpoint === 'string' && /\/(images|embeddings|audio)\b/.test(endpoint),
)
);
})
.map((m) => ({
id: m.id,
name: String(m.display_name || m.name || m.id),
provider: m.id.split('/')[0],
efforts: (Array.isArray(m.reasoning_levels)
? m.reasoning_levels
: (m.supported_reasoning_levels || []).map((v: any) => v.effort)
).filter((v: unknown) => typeof v === 'string'),
nativeEndpoints: Array.isArray(m.native_endpoints)
? m.native_endpoints.filter((v: unknown) => typeof v === 'string')
: [],
}));
// kept=1 reads Magpie's cached report; opening this page must never probe
// upstream accounts just to populate a selector.
const accounts: Account[] = [];
try {
const quotas = await fetch(`${this.url}/v1/magpie/quotas?kept=1`, {
headers: this.headers(),
signal: AbortSignal.timeout(2500),
redirect: 'error',
});
if (quotas.ok) {
const report = JSON.parse(await boundedText(quotas, 1024 * 1024));
for (const item of report.data || [])
if (
typeof item.provider === 'string' &&
typeof item.user === 'string' &&
item.user &&
(!item.kind || item.kind === 'subscription')
) {
if (!accounts.some((a) => a.provider === item.provider && a.id === item.user))
accounts.push({
id: item.user,
name: String(item.name || item.user),
provider: item.provider,
});
}
} else {
await quotas.body?.cancel();
}
} catch {
/* Account enumeration is optional and may be denied by a scoped key. */
}
return { models, accounts };
}
async route(provenance: Provenance) {
try {
const res = await fetch(
`${this.url}/v1/magpie/route?session=${encodeURIComponent(provenance.session)}`,
{ headers: this.headers(), signal: AbortSignal.timeout(2500), redirect: 'error' },
);
if (!res.ok) {
await res.body?.cancel();
throw new Error(`Route metadata HTTP ${res.status}`);
}
provenance.route = JSON.parse(await boundedText(res, 256 * 1024));
const route = (provenance.route as any)?.route ?? (provenance.route as any);
if (route?.model && typeof route.model === 'string') {
provenance.model ??= route.model;
if (route.model !== provenance.requestedModel) provenance.mismatch = true;
}
} catch (e) {
provenance.routeError = this.redact(e);
}
}
}