Files
plugins/packages/fingerprint/web/magpie.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

93 lines
2.8 KiB
TypeScript

export type MagpieTheme = {
dark: boolean;
lang: string;
tokens: Record<string, string>;
};
type ThemeListener = (theme: MagpieTheme) => void;
type MagpieBridge = {
theme?: unknown;
ready?: Promise<unknown>;
onTheme?: (listener: (theme: unknown) => void) => void;
};
declare global {
interface Window {
magpie?: MagpieBridge;
}
}
/** True only when the page is hosted in Magpie's iframe. */
export const embedded = typeof window !== 'undefined' && window.parent !== window;
/** Keep host data at the narrow boundary used by the page. */
export function normalizeTheme(value: unknown): MagpieTheme | null {
if (!value || typeof value !== 'object') return null;
const candidate = value as { dark?: unknown; lang?: unknown; tokens?: unknown };
if (typeof candidate.dark !== 'boolean') return null;
const tokens: Record<string, string> = {};
if (candidate.tokens && typeof candidate.tokens === 'object') {
for (const [key, token] of Object.entries(candidate.tokens)) {
if (typeof token === 'string') tokens[key] = token;
}
}
return {
dark: candidate.dark,
lang: typeof candidate.lang === 'string' && candidate.lang ? candidate.lang : 'en',
tokens,
};
}
const listeners = new Set<ThemeListener>();
let bridgeWired = false;
let bridgeWiring = false;
let latestTheme: MagpieTheme | null = null;
const bridgeLoaded =
typeof window !== 'undefined' && typeof document !== 'undefined'
? import('./vendor/magpie-bridge.js').catch(() => undefined)
: Promise.resolve(undefined);
function bridge(): MagpieBridge | undefined {
return typeof window === 'undefined' ? undefined : window.magpie;
}
function publish(value: unknown) {
const theme = normalizeTheme(value);
if (!theme) return;
latestTheme = theme;
// The CSS bridge owns the token values; this marker lets the page opt into
// aliases only after a real host theme has arrived.
document.documentElement.dataset.magpie = 'connected';
for (const listener of listeners) {
try {
listener(theme);
} catch {
// A page listener must not break delivery to the other listeners.
}
}
}
function wireBridge() {
if (bridgeWired || bridgeWiring || !embedded) return;
bridgeWiring = true;
void bridgeLoaded.then(() => {
const api = bridge();
if (!api) return;
bridgeWired = true;
api.onTheme?.(publish);
// `ready` is intentionally observed without awaiting it here: standalone
// pages may never receive a parent hello response.
api.ready?.then(publish, () => undefined);
if (api.theme) publish(api.theme);
});
}
/** Subscribe to host theme changes. Returns a cleanup function. */
export function subscribeTheme(listener: ThemeListener): () => void {
if (!embedded) return () => undefined;
listeners.add(listener);
if (latestTheme) listener(latestTheme);
wireBridge();
return () => listeners.delete(listener);
}