@sema-agent/core
Version:
Stateless, task-oriented AI agent core
88 lines (87 loc) • 3.79 kB
JavaScript
export const STATION_REFUSAL_LAYERS = Object.freeze(["read_boundary"]);
const ATTESTATION_GROUP_OF = {
settlement: "answer",
decidedVia: "answer",
origin: "answer",
classes: "answer",
inputDigest: "answer",
classifierCause: "classifierCause",
classifierRound: "classifierRound",
classifierAsked: "classifierRound",
classifierBlock: "classifierBlock",
unresolvableCause: "unresolvableCause",
foldedClasses: "foldedClasses",
guards: "guards",
pathDenial: "pathDenial",
bindingRefusal: "bindingRefusal",
unanswered: "unanswered",
sentence: "sentence",
receivedAskReasons: "receivedAskReasons",
stationRefusal: "stationRefusal",
};
const ATTESTATION_MEMBERS = Object.keys(ATTESTATION_GROUP_OF);
const groupLead = (g) => ATTESTATION_MEMBERS.find((k) => ATTESTATION_GROUP_OF[k] === g);
const ENGINE_ATTESTATIONS = new WeakSet();
function detachedMember(v) {
if (Array.isArray(v))
return Object.freeze([...v]);
if (typeof v !== "object" || v === null)
return v;
return Object.freeze(Object.fromEntries(Object.entries(v).map(([k, x]) => [k, typeof x === "object" && x !== null ? Object.freeze(Array.isArray(x) ? [...x] : { ...x }) : x])));
}
export function mintAttestation(call, members) {
const record = { toolCallId: call.toolCallId, toolName: call.toolName };
for (const k of ATTESTATION_MEMBERS)
if (members[k] !== undefined)
record[k] = detachedMember(members[k]);
const minted = Object.freeze(record);
ENGINE_ATTESTATIONS.add(minted);
return minted;
}
export function isEngineAttestation(v) {
return typeof v === "object" && v !== null && ENGINE_ATTESTATIONS.has(v);
}
const boundTo = (r, call) => isEngineAttestation(r) && r.toolCallId === call.toolCallId && r.toolName === call.toolName ? r : undefined;
export function attestationOf(d, call) {
if (typeof d !== "object" || d === null)
return undefined;
return boundTo(Object.getOwnPropertyDescriptor(d, "attestation")?.value, call);
}
export const hostWindowUnanswered = (record) => record?.unanswered?.cause === "approval_window_expired" || record?.unresolvableCause === "approval_window_expired";
export function engineSentenceOf(d, call) {
const sentence = attestationOf(d, call)?.sentence;
return sentence !== undefined && d.message === sentence.text ? sentence : undefined;
}
export function withRestatedSentence(longer, standing, call) {
const account = engineSentenceOf(standing, call);
return account === undefined || longer.message === undefined ? longer : attest(longer, call, { sentence: { ...account, text: longer.message } });
}
export function mergeAttestation(call, earlier, later, scope = "all") {
const a = boundTo(earlier, call);
const b = boundTo(later, call);
if (a === undefined)
return b;
const members = {};
if (b !== undefined)
for (const k of ATTESTATION_MEMBERS)
if (b[k] !== undefined)
members[k] = b[k];
let filled = false;
for (const g of new Set(Object.values(ATTESTATION_GROUP_OF))) {
if ((scope === "answer" && g !== "answer") || members[groupLead(g)] !== undefined || a[groupLead(g)] === undefined)
continue;
for (const k of ATTESTATION_MEMBERS)
if (ATTESTATION_GROUP_OF[k] === g && a[k] !== undefined)
members[k] = a[k];
filled = true;
}
if (!filled)
return b;
return mintAttestation(call, members);
}
export function attest(d, call, members) {
return withAttestation(d, mergeAttestation(call, attestationOf(d, call), mintAttestation(call, members)));
}
export function withAttestation(d, record) {
return record === undefined ? d : { ...d, attestation: record };
}