histoire
Version:
A fast story book powered by Vite
54 lines (53 loc) • 3.15 kB
JavaScript
import { z } from 'zod';
/** Package-manager distribution with registry-owned launch environment. */
const packageSchema = z.object({ package: z.string().min(1).max(2048), args: z.array(z.string().max(2048)).max(64).default([]), env: z.record(z.string().regex(/^[a-z_]\w*$/i), z.string().max(16000)).default({}) });
/** Archives are resolved on server; no browser-provided URL reaches installation. */
const binarySchema = z.object({ archive: z.url().refine(value => new URL(value).protocol === 'https:'), cmd: z.string().min(1).max(2048), args: z.array(z.string()).max(64).default([]), sha256: z.string().regex(/^[a-f0-9]{64}$/i).optional() });
/** Invalid optional logos fall back without removing otherwise valid agents. */
const iconSchema = z.unknown().transform((value) => {
if (typeof value !== 'string' || value.length > 2048)
return undefined;
try {
const url = new URL(value);
if (url.protocol === 'https:' && !url.username && !url.password)
return url.href;
}
catch { /* Unsupported image sources use bundled fallback icon. */ }
});
/** Unknown display metadata is ignored while launch fields receive strict validation. */
const entrySchema = z.object({
id: z.string().regex(/^[\w-]{1,80}$/).refine(value => !['__proto__', 'constructor', 'prototype'].includes(value)),
name: z.string().min(1).max(120),
version: z.string().max(120).optional(),
icon: iconSchema.optional(),
distribution: z.object({ npx: packageSchema.optional(), uvx: packageSchema.optional(), binary: z.record(z.string(), binarySchema).optional() }).optional(),
});
/** Bad entries cannot erase a usable cached catalog or inject unsafe identities. */
export function parseAgentRegistry(value) {
const root = z.object({ agents: z.array(z.unknown()) }).parse(value);
const entries = [];
const ids = new Set();
for (const raw of root.agents) {
const parsed = entrySchema.safeParse(raw);
if (parsed.success && !ids.has(parsed.data.id)) {
entries.push(parsed.data);
ids.add(parsed.data.id);
}
}
if (root.agents.length && !entries.length)
throw new Error('Invalid ACP registry document');
return entries;
}
/** Prefer package runners, then exact OS/architecture binary without guessing aliases. */
export function resolveAgentDistribution(entry, platform = process.platform, arch = process.arch) {
const distribution = entry.distribution;
if (distribution?.npx)
return { kind: 'npx', command: 'npx', args: ['--yes', distribution.npx.package, ...distribution.npx.args], env: distribution.npx.env };
if (distribution?.uvx)
return { kind: 'uvx', command: 'uvx', args: [distribution.uvx.package, ...distribution.uvx.args], env: distribution.uvx.env };
const os = platform === 'win32' ? 'windows' : platform;
const cpu = arch === 'x64' ? 'x86_64' : arch === 'arm64' ? 'aarch64' : arch;
const binary = distribution?.binary?.[`${os}-${cpu}`];
if (binary)
return { kind: 'binary', command: binary.cmd, args: binary.args, env: {}, archive: binary.archive, sha256: binary.sha256 };
}