@sema-agent/core
Version:
Stateless, task-oriented AI agent core
42 lines (41 loc) • 2.2 kB
JavaScript
import { engineMountedAs, TOOL_CATALOG_ENTRIES } from "../../core/tool-catalog-entries.js";
import { pathTargetOf, pathTargetValue } from "../../core/tool-registry.js";
import { violationDetails, violationText } from "./safety.js";
import { readStationMemberOf } from "./shell-read-check.js";
const VOUCH = { disposition: "vouch", finding: "none" };
function engineJudgesOf(tools) {
const judges = new Map();
for (const e of TOOL_CATALOG_ENTRIES)
if (tools.has(e.name) && e.source === "builtin" && e.face.contract !== undefined)
judges.set(e.name, engineMountedAs([e.id]));
return judges;
}
const faceOf = (call) => call.face;
export function createFileWriteStation(opts) {
const engineOwns = engineJudgesOf(opts.tools);
const check = {
judge(call, args, _cwd, signal, callOpts) {
const seat = readStationMemberOf(call, engineOwns);
const member = seat !== undefined && seat.value(faceOf(call)) ? seat.name : undefined;
const slot = member === undefined ? undefined : pathTargetOf(member);
const spelled = slot === undefined ? undefined : pathTargetValue(args, slot);
if (spelled === undefined)
return null;
return (async () => {
const r = await opts.contain(spelled, signal, callOpts.toolCallId);
if (r.ok)
return VOUCH;
if (r.violation.code === "path_not_in_root") {
const details = violationDetails(r.violation);
return { disposition: "refuse", finding: "outside_roots", face: "write", sentence: violationText(call.toolName, r.violation), ...(details !== undefined ? { details } : {}) };
}
if (r.violation.code === "write_path_denied") {
return { disposition: "refuse", finding: "deny_hit", face: "write", sentence: violationText(call.toolName, r.violation), ...(r.violation.echoed !== undefined ? { echoed: r.violation.echoed } : {}) };
}
return VOUCH;
})();
},
unanswered: () => VOUCH,
};
return { check, tools: opts.tools };
}