|
| 1 | +/** |
| 2 | + * Registry entries whose remotes are several servers. The registry means the remotes of an entry as ways to reach one |
| 3 | + * server (a transport, a region), and the probe measures the first one. Some publishers list a set of servers in one |
| 4 | + * entry instead: com.cloudflare.mcp/mcp has 15 (docs, radar, browser, ...), each under /mcp and /sse. The probe then |
| 5 | + * measured docs only, and fell back to radar when docs failed, under the entry's name. |
| 6 | + * |
| 7 | + * An entry whose callable remotes belong to BUNDLE_MIN or more servers (family keys, see probe/sources.ts) is a bundle. |
| 8 | + * Its first server keeps the entry's name and row, and its fallbacks stay inside that server. Each other server gets a |
| 9 | + * row of its own with the status "bundled", named like a URL tracked from /check (url/<host>/<path>), with a link back |
| 10 | + * to the entry. Regional or per-client copies of one server are bundles by this rule too; their pages say "one of the N |
| 11 | + * servers of the entry", which is true for both. Two remotes are left as they are: mostly a public and a signed-in path |
| 12 | + * of one server, where the second one is the right fallback. |
| 13 | + */ |
| 14 | +import type { RegistryEntry, RegistryRemote } from '../packages/mcpcost/src/index.js'; |
| 15 | +import { endpointKey, familyKey, listedName, type Listed, type Listing } from './sources.js'; |
| 16 | + |
| 17 | +/** The fewest servers in one entry that make it a bundle. */ |
| 18 | +export const BUNDLE_MIN = 3; |
| 19 | + |
| 20 | +/** The key in the cached registry file that carries the bundle of an entry or of one of its servers. */ |
| 21 | +export const BUNDLE_META = 'dev.ulehla.mcpcost/bundle'; |
| 22 | + |
| 23 | +/** On the entry: its other servers. On one of them: the entry, and the catalogs that list it on its own. */ |
| 24 | +export type Bundle = |
| 25 | + | { members: Array<{ name: string; host: string }>; servers: number } |
| 26 | + | { entry: string; servers: number; listedIn?: Listing[] }; |
| 27 | + |
| 28 | +export type BundledEntry = RegistryEntry & { bundle: Bundle }; |
| 29 | + |
| 30 | +const callable = (r: RegistryRemote) => (r.type === 'streamable-http' || r.type === 'sse') && endpointKey(r.url) !== undefined; |
| 31 | + |
| 32 | +/** The servers of an entry in the probe's order (streamable-http first): one family key each, with its remotes. */ |
| 33 | +export function familiesOf(entry: RegistryEntry): Array<{ key: string; remotes: RegistryRemote[] }> { |
| 34 | + const remotes = entry.server.remotes ?? []; |
| 35 | + const ordered = [...remotes.filter((r) => r.type === 'streamable-http'), ...remotes.filter((r) => r.type === 'sse')].filter(callable); |
| 36 | + const out: Array<{ key: string; remotes: RegistryRemote[] }> = []; |
| 37 | + for (const r of ordered) { |
| 38 | + const key = familyKey(r.url)!; |
| 39 | + const f = out.find((x) => x.key === key); |
| 40 | + if (f) { |
| 41 | + if (!f.remotes.some((x) => endpointKey(x.url) === endpointKey(r.url))) f.remotes.push(r); |
| 42 | + } else out.push({ key, remotes: [r] }); |
| 43 | + } |
| 44 | + return out; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Splits the bundles of the registry. Returns: |
| 49 | + * - targets: the registry with each bundle cut to its first server, so the probe does not fall back to another server |
| 50 | + * under the entry's name. The cached registry file keeps the whole entry; only the probe gets the cut one. |
| 51 | + * - members: one entry per other server of a bundle. A catalog that lists such a server on its own gives it its title |
| 52 | + * and description. A URL in two bundles is one member, the first. |
| 53 | + * - entries: the bundle of each entry that is one, by name, for its row. |
| 54 | + */ |
| 55 | +export function splitBundles(registry: RegistryEntry[], catalog: Listed[] = []): { targets: RegistryEntry[]; members: BundledEntry[]; entries: Map<string, Bundle> } { |
| 56 | + const byFamily = new Map<string, Listed[]>(); |
| 57 | + for (const l of catalog) { |
| 58 | + const k = familyKey(l.url); |
| 59 | + if (k) byFamily.set(k, [...(byFamily.get(k) ?? []), l]); |
| 60 | + } |
| 61 | + const members: BundledEntry[] = []; |
| 62 | + const targets: RegistryEntry[] = []; |
| 63 | + const entries = new Map<string, Bundle>(); |
| 64 | + const taken = new Set(registry.map((e) => e.server.name)); |
| 65 | + for (const e of registry) { |
| 66 | + const families = familiesOf(e); |
| 67 | + if (families.length < BUNDLE_MIN) { |
| 68 | + targets.push(e); |
| 69 | + continue; |
| 70 | + } |
| 71 | + const [first, ...rest] = families; |
| 72 | + const own: BundledEntry[] = rest.flatMap((f) => { |
| 73 | + const url = new URL(f.remotes[0].url); |
| 74 | + const name = listedName(url); |
| 75 | + if (taken.has(name)) return []; |
| 76 | + taken.add(name); |
| 77 | + const listed = byFamily.get(f.key) ?? []; |
| 78 | + const listedIn: Listing[] = []; |
| 79 | + for (const l of listed) if (!listedIn.some((x) => x.name === l.listing.name)) listedIn.push(l.listing); |
| 80 | + const member: BundledEntry = { |
| 81 | + server: { |
| 82 | + name, |
| 83 | + title: listed.find((l) => l.title)?.title ?? e.server.title ?? e.server.name, |
| 84 | + description: (listed.find((l) => l.description)?.description ?? e.server.description)?.slice(0, 300), |
| 85 | + websiteUrl: e.server.websiteUrl, |
| 86 | + repository: e.server.repository, |
| 87 | + remotes: f.remotes.slice(0, 2) |
| 88 | + }, |
| 89 | + meta: { ...e.meta, status: 'bundled' }, |
| 90 | + bundle: { entry: e.server.name, servers: families.length, ...(listedIn.length ? { listedIn } : {}) } |
| 91 | + }; |
| 92 | + return [member]; |
| 93 | + }); |
| 94 | + members.push(...own); |
| 95 | + targets.push({ server: { ...e.server, remotes: first.remotes }, meta: e.meta }); |
| 96 | + entries.set(e.server.name, { members: own.map((m) => ({ name: m.server.name, host: new URL(m.server.remotes![0].url).hostname })), servers: families.length }); |
| 97 | + } |
| 98 | + return { targets, members, entries }; |
| 99 | +} |
0 commit comments