@sema-agent/core
Version:
Stateless, task-oriented AI agent core
163 lines (162 loc) • 6.19 kB
JavaScript
import { PERSISTED_RULE_MATCHES, RULE_OFFER_BATCH_MEMBER_KINDS, RULE_OFFER_CAUTIONS, RULE_OFFER_KINDS, UNCOVERED_SEGMENT_REASONS, isAddStamp, } from "./permission-rule-model.js";
import { isPlainRecord } from "./checkpoint-store.js";
const OFFER_MATCHES = PERSISTED_RULE_MATCHES.filter((m) => m !== "call");
export const RULE_OFFER_SHAPE_PROBLEMS = Object.freeze(["not_an_array", "unknown_word", "malformed"]);
const MALFORMED = Object.freeze({ problem: "malformed" });
const UNKNOWN_WORD = Object.freeze({ problem: "unknown_word" });
export function readRuleOffers(raw) {
if (raw === undefined)
return { offers: [], dropped: [] };
if (!Array.isArray(raw))
return { problem: "not_an_array" };
const offers = [];
const dropped = [];
const length = raw.length;
for (let index = 0; index < length; index++) {
const verdict = readOfferElement(raw[index]);
if ("problem" in verdict)
dropped.push({ index, problem: verdict.problem });
else
offers.push({ index, ...verdict });
}
return { offers, dropped };
}
function wordOf(value, words) {
if (typeof value !== "string")
return MALFORMED;
return words.some((w) => w === value) ? value : UNKNOWN_WORD;
}
function readOfferElement(value) {
if (!isPlainRecord(value))
return MALFORMED;
const kind = wordOf(value.kind, RULE_OFFER_KINDS);
if (typeof kind !== "string")
return kind;
switch (kind) {
case "single": {
const rule = value.rule;
const match = wordOf(value.match, OFFER_MATCHES);
const command = value.command;
if (typeof rule !== "string")
return MALFORMED;
if (typeof match !== "string")
return match;
if (typeof command !== "string")
return MALFORMED;
const peerFingerprint = Object.hasOwn(value, "peerFingerprint") ? value.peerFingerprint : undefined;
if (peerFingerprint !== undefined && !isAddStamp(peerFingerprint))
return MALFORMED;
const cautions = cautionsOf(value);
if ("problem" in cautions)
return cautions;
return { offer: { kind: "single", rule, match, command, ...(peerFingerprint !== undefined ? { peerFingerprint } : {}), ...cautions.seat } };
}
case "batch":
return readBatch(value);
default: {
const unhandled = kind;
return unhandled;
}
}
}
function readBatch(value) {
const rules = value.rules;
const count = value.uncoveredSegments;
const detail = value.uncoveredDetail;
if (!Array.isArray(rules))
return MALFORMED;
const length = rules.length;
if (length === 0)
return MALFORMED;
const members = [];
for (let i = 0; i < length; i++) {
const verdict = readBatchMember(rules[i]);
if ("problem" in verdict)
return verdict;
members.push(verdict.member);
}
if (typeof count !== "number" || !Number.isSafeInteger(count) || count < 0)
return MALFORMED;
const offer = { kind: "batch", rules: members, uncoveredSegments: count };
if (detail === undefined)
return { offer };
const rows = detailRowsOf(detail, count);
return rows !== undefined ? { offer: { ...offer, uncoveredDetail: rows } } : { offer, uncoveredDetailDropped: true };
}
function readBatchMember(value) {
if (!isPlainRecord(value))
return MALFORMED;
const kind = wordOf(value.kind, RULE_OFFER_BATCH_MEMBER_KINDS);
if (typeof kind !== "string")
return kind;
switch (kind) {
case "command": {
const read = commandMember(value.rule, value.match, value.command, value.segment);
if ("problem" in read)
return read;
const cautions = cautionsOf(value);
return "problem" in cautions ? cautions : { member: { ...read.member, ...cautions.seat } };
}
case "directoryRead": {
const rule = value.rule;
const directory = value.directory;
const segment = value.segment;
if (typeof rule !== "string" || typeof directory !== "string" || typeof segment !== "string")
return MALFORMED;
return { member: { kind: "directoryRead", rule, directory, segment } };
}
default: {
const unhandled = kind;
return unhandled;
}
}
}
function commandMember(rule, rawMatch, command, segment) {
if (typeof rule !== "string")
return MALFORMED;
const match = wordOf(rawMatch, OFFER_MATCHES);
if (typeof match !== "string")
return match;
if (typeof command !== "string" || typeof segment !== "string")
return MALFORMED;
return { member: { kind: "command", rule, match, command, segment } };
}
function cautionsOf(value) {
const raw = Object.hasOwn(value, "cautions") ? value.cautions : undefined;
if (raw === undefined)
return { seat: {} };
if (!Array.isArray(raw))
return MALFORMED;
const words = [];
let unknown = false;
const length = raw.length;
for (let i = 0; i < length; i++) {
const word = wordOf(raw[i], RULE_OFFER_CAUTIONS);
if (typeof word === "string")
words.push(word);
else if (word === MALFORMED)
return MALFORMED;
else
unknown = true;
}
return unknown ? UNKNOWN_WORD : { seat: { cautions: words } };
}
function detailRowsOf(detail, count) {
if (!Array.isArray(detail))
return undefined;
const length = detail.length;
if (length !== count)
return undefined;
const rows = [];
for (let i = 0; i < length; i++) {
const row = detail[i];
if (!isPlainRecord(row))
return undefined;
const segment = row.segment;
const reason = wordOf(row.reason, UNCOVERED_SEGMENT_REASONS);
if (typeof segment !== "string" || typeof reason !== "string")
return undefined;
rows.push({ segment, reason });
}
return rows;
}