@sema-agent/core
Version:
Stateless, task-oriented AI agent core
44 lines (43 loc) • 1.86 kB
JavaScript
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 });
}
function selectedBy(entry, tool) {
return tool.name === entry || (tool.aliases?.includes(entry) ?? false);
}
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 selectedFor = (f) => {
const d = f.denyTools ?? [];
const a = f.allowTools;
const isNarrowed = a !== undefined && !a.includes("*");
return input.pool.filter((t) => (!isNarrowed || a.some((x) => selectedBy(x, t))) && !d.some((x) => selectedBy(x, t)));
};
if (selectedFor(face).some((t) => input.isPolluting(t)))
return true;
if (face.canRedelegate === true && input.faces.some((f) => selectedFor(f).some((t) => input.isPolluting(t))))
return true;
return false;
}
export function contentOriginPollutes(origin, strict) {
return origin === "external" || (strict && origin === "execution");
}