// The allowed-domains editor of an opened agent card: which entries it // shows and the bodies it sends. Kept out of the component so `npm test` can // run it. /** One entry as `GET /api/egress/allowed-domains` carries it. */ export interface AllowedDomain { domain: string; /** Absent for an entry that holds for every agent in the swarm. */ agent?: string; /** * Kept by the controller from the agent's backend environment: shown as * automatic, and the route refuses to remove it. */ derived?: boolean; } export const ALLOWED_DOMAINS_URL = "/api/egress/allowed-domains"; /** * The entries that apply to `agent`: the swarm-wide ones first, then the ones * for this agent alone. Entries for other agents are not shown on this panel. */ export function entriesFor( entries: AllowedDomain[], agent: string, ): AllowedDomain[] { const swarmWide = entries.filter((e) => e.agent === undefined); const own = entries.filter((e) => e.agent === agent); return [...swarmWide, ...own]; } /** What the panel says about a list of entries. */ export function allowedSummary(shown: AllowedDomain[]): string { if (shown.length === 0) return "none added"; const swarmWide = shown.filter((e) => e.agent === undefined).length; const automatic = shown.filter((e) => e.derived === true).length; const own = shown.length - swarmWide - automatic; return [ swarmWide > 0 ? `${swarmWide} swarm-wide` : null, own > 0 ? `${own} for this agent` : null, automatic > 0 ? `${automatic} automatic` : null, ] .filter((part) => part !== null) .join(", "); } /** * The body that adds or removes `domain`, as typed: the route lower-cases it * and refuses what is not a name. `forSwarm` makes it hold for every agent; * otherwise it is for `agent` alone. */ export function entryBody( domain: string, forSwarm: boolean, agent: string, ): AllowedDomain { const name = domain.trim(); return forSwarm ? { domain: name } : { domain: name, agent }; }