@sema-agent/core
Version:
Stateless, task-oriented AI agent core
56 lines (55 loc) • 3.06 kB
JavaScript
import { compileToolSelector, resolveToolSubset, valveLeaves, valveLeavesPeer } from "../../agents/tool-filter.js";
import { MCP_RESOURCE_FACES } from "../mounted-mcp.js";
import { legRetiredNameView, ruleListUnderView } from "../tool-name-aliases.js";
import { MCP_NAMESPACE, mintNamespacePrefix } from "@sema-agent/primitives";
const TOOL_CONTENT_ORIGIN_SET = { external: true, execution: true, local: true };
export const TOOL_CONTENT_ORIGINS = Object.keys(TOOL_CONTENT_ORIGIN_SET);
export function isToolContentOrigin(value) {
return typeof value === "string" && Object.prototype.hasOwnProperty.call(TOOL_CONTENT_ORIGIN_SET, value);
}
export function classifyToolContentOrigin(input) {
if (input.declared !== undefined)
return isToolContentOrigin(input.declared) ? input.declared : "external";
if (input.trusted)
return "local";
if (input.isProtocolTool)
return "external";
if (input.isCallerTool)
return "external";
return "local";
}
export function withContentOrigin(tool, origin) {
return Object.assign(tool, { contentOrigin: origin });
}
export function delegationCallIsExternal(input) {
if (input.faces === undefined || input.pool === undefined)
return true;
const face = input.requestedType === undefined ? undefined : input.faces.find((f) => f.name === input.requestedType);
if (face === undefined)
return true;
const view = legRetiredNameView(input.pool);
const selectedFor = (f) => resolveToolSubset(input.pool, ruleListUnderView(f.allowTools, view), ruleListUnderView(f.denyTools, view));
const broughtBy = (f) => {
if (f.servers === undefined || f.servers.length === 0)
return [];
const own = f.servers;
const lists = compileToolSelector([{ ...(f.allowTools !== undefined ? { allow: ruleListUnderView(f.allowTools, view) } : {}), deny: ruleListUnderView(f.denyTools, view) ?? [] }]);
const peerLeft = valveLeavesPeer(lists);
const sameNamespace = (input.inheritedServers ?? []).filter((p) => own.some((s) => mintNamespacePrefix(MCP_NAMESPACE, s.name) === mintNamespacePrefix(MCP_NAMESPACE, p.name)));
const namespaces = [...own, ...sameNamespace].flatMap((s) => {
const prefix = mintNamespacePrefix(MCP_NAMESPACE, s.name);
return peerLeft(prefix) ? [{ name: prefix, ...(s.contentOrigin !== undefined ? { contentOrigin: s.contentOrigin } : {}) }] : [];
});
const readers = MCP_RESOURCE_FACES.filter((name) => valveLeaves(lists)({ name })).map((name) => ({ name, contentOrigin: "external" }));
return [...namespaces, ...readers];
};
const reachesPolluting = (f) => [...selectedFor(f), ...broughtBy(f)].some((t) => input.isPolluting(t));
if (reachesPolluting(face))
return true;
if (face.canRedelegate === true && input.faces.some(reachesPolluting))
return true;
return false;
}
export function contentOriginPollutes(origin, strict) {
return origin === "external" || (strict && origin === "execution");
}