@sema-agent/core
Version:
Stateless, task-oriented AI agent core
27 lines (26 loc) • 1.57 kB
JavaScript
export const READ_ROOT_GRANT_LASTS = Object.freeze(["session", "run"]);
const isLasts = (v) => READ_ROOT_GRANT_LASTS.some((w) => w === v);
export function readRootGrantSeat(grant, lasts, candidate, byPerson) {
if (grant === undefined && lasts === undefined)
return {};
const offered = candidate !== undefined ? { offered: candidate.dir } : {};
const requested = typeof grant === "string" ? { requested: grant } : {};
const reason = grant === undefined ? "lasts_invalid"
: typeof grant !== "string" ? "not_a_string"
: lasts !== undefined && !isLasts(lasts) ? "lasts_invalid"
: candidate === undefined ? "no_candidate"
: grant !== candidate.dir ? "mismatch"
: undefined;
if (reason !== undefined)
return { readRootGrant: { verdict: "rejected", reason, ...offered, ...requested } };
const narrowed = lasts !== "session" ? undefined : candidate.lasts !== "session" ? "not_offered" : !byPerson ? "not_a_person" : undefined;
return { readRootGrant: { verdict: "granted", candidate: candidate, lasts: lasts === "session" && narrowed === undefined ? "session" : "run", ...(narrowed !== undefined ? { narrowed } : {}) } };
}
export function ownReadRootCandidate(c) {
if (c === undefined)
return undefined;
const dir = c.dir;
const covers = c.covers;
const lasts = c.lasts;
return typeof dir === "string" ? { dir, clearsThisAsk: true, ...(covers === "exact" ? { covers } : {}), ...(isLasts(lasts) ? { lasts } : {}) } : undefined;
}