UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

97 lines (96 loc) • 4.74 kB
import { isAbsoluteHostPath } from "@sema-agent/primitives"; export const PROJECT_ID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; export const PROJECT_SEAT_NAME_MAX = 256; export const PROJECT_SEAT_FILES_MAX = 100; const SEAT_MEMBERS = ["id", "name", "description", "instructions", "files"]; const TEXT_MEMBERS = ["name", "description", "instructions"]; const shown = (v) => { if (typeof v === "string") return JSON.stringify(v.length > 48 ? `${v.slice(0, 48)}…` : v); if (v === null) return "null"; if (Array.isArray(v)) return "an array"; return typeof v === "object" ? "an object" : typeof v === "function" ? "a function" : String(v); }; export function screenProjectSeat(value, at = "TaskSpec.project") { try { return screenRead(value, at); } catch { return refusal(at, "could not be read: its read threw — a seat whose read throws is refused rather than read as absent"); } } function refusal(field, why, code = "config.invalid_argument") { return { ok: false, code, field, message: `${field}: ${why} — the leg is refused rather than run under a project it was not given.` }; } function screenRead(value, at) { if (value === undefined) return { ok: true }; const refuse = refusal; const proto = value === null || typeof value !== "object" || Array.isArray(value) ? undefined : Object.getPrototypeOf(value); if (proto === undefined || (proto !== null && Object.getPrototypeOf(proto) !== null)) { return refuse(at, `must be a plain object { id, name?, description?, instructions?, files? } (got ${shown(value)})`); } const own = Reflect.ownKeys(value); const stray = own.find((k) => typeof k !== "string" || !SEAT_MEMBERS.includes(k)); if (stray !== undefined) return refuse(`${at}.${String(stray)}`, `is no member of a project seat (its members are ${SEAT_MEMBERS.join(", ")})`); const read = {}; for (const k of own) { const d = Object.getOwnPropertyDescriptor(value, k); if (d === undefined || !("value" in d)) return refuse(`${at}.${k}`, "must be a plain data member, not an accessor"); if (d.value !== undefined) read[k] = d.value; } const id = read["id"]; if (id === undefined) return refuse(`${at}.id`, "is required — a seat names its project"); if (typeof id !== "string" || !PROJECT_ID_REGEX.test(id)) { return refuse(`${at}.id`, `must be a project UUID (the id a \`.sema/project\` marker carries), got ${shown(id)}`, "config.project_id_invalid"); } for (const k of TEXT_MEMBERS) { const v = read[k]; if (v === undefined) continue; if (typeof v !== "string") return refuse(`${at}.${k}`, `must be a string (got ${shown(v)}); leave it out to say none`); if (v.trim() === "") return refuse(`${at}.${k}`, "is empty or only whitespace; leave it out to say none"); } const name = read["name"]; if (name !== undefined && /[\r\n]/.test(name)) return refuse(`${at}.name`, "is one line — it carries a line break"); if (name !== undefined && name.length > PROJECT_SEAT_NAME_MAX) return refuse(`${at}.name`, `is at most ${PROJECT_SEAT_NAME_MAX} characters (got ${name.length})`); const given = read["files"]; let files; if (given !== undefined) { if (!Array.isArray(given)) return refuse(`${at}.files`, `must be a list of absolute host paths (got ${shown(given)})`); const length = given.length; if (length > PROJECT_SEAT_FILES_MAX) return refuse(`${at}.files`, `holds at most ${PROJECT_SEAT_FILES_MAX} entries (got ${length})`); const entries = Array.from({ length }, (_, i) => given[i]); const bad = entries.findIndex((p) => !isAbsoluteHostPath(p)); if (bad >= 0) return refuse(`${at}.files[${bad}]`, `must be an absolute host path (got ${shown(entries[bad])})`); files = entries; } const seat = Object.freeze({ id, ...Object.fromEntries(TEXT_MEMBERS.filter((k) => read[k] !== undefined).map((k) => [k, read[k]])), ...(files !== undefined ? { files: Object.freeze(files) } : {}), }); return { ok: true, seat }; } const REFUSED = Symbol("refused project seat"); export function captureProjectSeat(value, at) { const screened = screenProjectSeat(value, at); return screened.ok ? screened.seat : Object.freeze({ [REFUSED]: screened }); } export function handedProjectSeat(captured) { const refused = captured?.[REFUSED]; return refused ?? { ok: true, ...(captured !== undefined ? { seat: captured } : {}) }; }