@sema-agent/core
Version:
Stateless, task-oriented AI agent core
84 lines (83 loc) • 4.54 kB
JavaScript
import { a2aPeerRuleOf, a2aWholePeerName, isAddStamp, parseRuleText, suggestRuleForPeer } from "./permission-rule-model.js";
import { A2A_NAMESPACE, mintNamespacePrefix } from "@sema-agent/primitives";
export function a2aInsideRelaxed(spec, isolatePeers) {
return spec.boundary === "inside" && !isolatePeers;
}
export function a2aInsidePeerReader(declared, isolatePeers) {
return declaredPeerReader(declared, (spec) => (a2aInsideRelaxed(spec, isolatePeers) && typeof spec.fingerprint === "string" ? Object.freeze({ peer: spec.name, fingerprint: spec.fingerprint }) : undefined));
}
export function declaredPeerReader(declared, project) {
const peers = (declared ?? []).map((spec) => ({ prefix: mintNamespacePrefix(A2A_NAMESPACE, spec.name), value: project(spec) }));
if (peers.length === 0)
return () => undefined;
return (toolName) => peers.find((p) => toolName.startsWith(p.prefix))?.value;
}
export function seatStampOf(seat, name) {
if (seat === undefined)
return undefined;
const peer = a2aPeerRuleOf(name);
if (peer === undefined)
return undefined;
let peers;
try {
peers = seat();
}
catch (e) {
throw seatRefusal(`it threw (${e instanceof Error ? e.message : typeof e})`);
}
if (!Array.isArray(peers))
throw seatRefusal("it answered no list");
const named = (p) => typeof p === "object" && p !== null && typeof p.name === "string";
if (!peers.every(named))
throw seatRefusal("an element is no registration with a name");
const found = a2aInsidePeerReader(peers, false)(`${peer}__`);
return found !== undefined && isAddStamp(found.fingerprint) ? found : undefined;
}
export function insidePeerOfferOf(seat, toolName) {
const stamp = seatStampOf(seat, toolName);
return stamp !== undefined ? suggestRuleForPeer(toolName, stamp.fingerprint) : undefined;
}
function seatRefusal(why) {
return Object.assign(new Error(`the permission-rule store's mounted-peers seat (a2aPeers) could not be read: ${why} — refusing to write a grant without the identity stamp it is bound to`), { code: "config.invalid_argument" });
}
export function storedRowBindingOf(stamps, fingerprint) {
if (stamps === undefined || stamps.length === 0)
return "unbound";
return stamps.includes(fingerprint) ? "current" : "identity_changed";
}
function withheld(mayClear, carriesAnotherStamp, entries) {
const shown = entries !== undefined ? { entries } : {};
return mayClear ? { clears: false, reason: carriesAnotherStamp ? "identity_changed" : "unbound", ...shown } : { clears: false, ...shown };
}
export function boundListGrantOf(hit, fingerprint, mayClear) {
const bound = hit.bound ?? [];
const current = bound.filter((b) => b.peerFingerprint === fingerprint);
if (!mayClear || current.length === 0) {
const shown = [...new Set([...hit.entries, ...bound.map((b) => b.entry)])];
return withheld(mayClear, bound.some((b) => b.peerFingerprint !== undefined), shown.length > hit.entries.length ? shown : undefined);
}
const positions = [...new Set(current.flatMap((b) => b.positions ?? []))].sort((a, b) => a - b);
const grants = new Set(current.map((b) => b.grant));
const grant = grants.size === 1 ? [...grants][0] : undefined;
return { clears: true, entries: current.map((b) => b.entry), ...(positions.length > 0 ? { positions } : {}), ...(grant !== undefined ? { grant } : {}) };
}
export function boundStoredGrantOf(rules, bound, fingerprint, mayClear) {
const bindingOf = (r) => storedRowBindingOf(r.peerFingerprints, fingerprint);
const standing = rules.length > 0 && rules.every((r) => bindingOf(r) === "current");
const current = standing ? rules : bound.filter((r) => bindingOf(r) === "current");
if (!mayClear || current.length === 0) {
const own = new Set(rules.map((r) => r.rule));
const shown = new Set([...own, ...bound.map((r) => r.rule)]);
return withheld(mayClear, [...rules, ...bound].some((r) => bindingOf(r) === "identity_changed"), shown.size > own.size ? [...shown] : undefined);
}
return { clears: true, entries: [...new Set(current.map((r) => r.rule))], rules: current };
}
export function standsOnWholePeerRow(rules) {
return rules.some((r) => {
const read = parseRuleText(r.rule, "allow");
return "rule" in read && a2aWholePeerName(read.rule.rule) !== undefined;
});
}
export function unboundGrantOf(mayClear) {
return withheld(mayClear, false);
}