UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

81 lines (80 loc) • 4.37 kB
const NOT_IN_A_NAME = /[\p{Cc}\p{Zl}\p{Zp}]|(?![\u200c\u200d])\p{Cf}/u; const WHITE_SPACE = String.raw `\s`; const NOT_VISIBLE = new RegExp(`[${WHITE_SPACE}\\p{Cf}]+`, "gu"); const ONLY_WHITE_SPACE = new RegExp(`^${WHITE_SPACE}*$`, "u"); const STARTS_WHITE = new RegExp(`^${WHITE_SPACE}`, "u"); const ENDS_WHITE = new RegExp(`${WHITE_SPACE}$`, "u"); export function whiteSpaceAtEdgesOf(raw) { return { starts: STARTS_WHITE.test(raw), ends: ENDS_WHITE.test(raw) }; } const UNPAIRED_SURROGATE = /\p{Cs}/u; export function holdsUnpairedSurrogate(raw) { return UNPAIRED_SURROGATE.test(raw); } export function unpairedSurrogateRefusal(what) { return `${what} holds an unpaired surrogate (half of a UTF-16 pair with no partner); UTF-8 spells it as U+FFFD, so it would name another key's partition — nothing was read or written; pass the key as well-formed text`; } export const PRINCIPAL_NAME_MAX_CHARS = 256; export const PRINCIPAL_NAME_MAX_BYTES = 3 * PRINCIPAL_NAME_MAX_CHARS; export const PRINCIPAL_NAME_REFUSALS = Object.freeze(["not_a_string", "blank", "too_long", "refused_character", "ill_formed", "edge_whitespace"]); const FAULT_OF = { not_a_string: (raw) => (raw === null ? "null" : typeof raw), blank: (raw) => (raw === "" ? "an empty string" : ONLY_WHITE_SPACE.test(String(raw)) ? "a blank string" : "a string with no visible character"), too_long: () => `a string longer than ${PRINCIPAL_NAME_MAX_CHARS} characters`, refused_character: () => "a string holding a control, separator or format character", ill_formed: () => "a string holding an unpaired surrogate", edge_whitespace: (raw) => { const { starts, ends } = whiteSpaceAtEdgesOf(String(raw)); return `a string that ${starts && ends ? "starts and ends" : starts ? "starts" : "ends"} with white space`; }, }; const refusal = (reason, raw) => ({ ok: false, reason, fault: FAULT_OF[reason](raw) }); export function judgePrincipalName(raw) { if (typeof raw !== "string") return refusal("not_a_string", raw); if (!hasVisibleContent(raw)) return refusal("blank", raw); if (raw.length > PRINCIPAL_NAME_MAX_CHARS) return refusal("too_long", raw); if (NOT_IN_A_NAME.test(raw)) return refusal("refused_character", raw); if (holdsUnpairedSurrogate(raw)) return refusal("ill_formed", raw); const edges = whiteSpaceAtEdgesOf(raw); if (edges.starts || edges.ends) return refusal("edge_whitespace", raw); return { ok: true, name: raw }; } export const PRINCIPAL_NAME_SPELLING = `a string with a visible character and no white space at either end, no control, line or paragraph separator character, no format character but a joiner, no unpaired surrogate, and at most ${PRINCIPAL_NAME_MAX_CHARS} characters`; const NEWER_CLAUSES_FROM = PRINCIPAL_NAME_REFUSALS.indexOf("ill_formed"); export const RECORDED_PRINCIPAL_NAME_SPELLING = `a principal name (${PRINCIPAL_NAME_SPELLING}), or one whose only faults are an unpaired surrogate or white space at an end (an earlier release wrote such names)`; export function hasVisibleContent(raw) { return raw.replace(NOT_VISIBLE, "") !== ""; } export function principalNameOf(raw) { const verdict = judgePrincipalName(raw); return verdict.ok ? verdict.name : undefined; } export function recordedPrincipalNameOf(raw) { const verdict = judgePrincipalName(raw); return verdict.ok ? verdict.name : PRINCIPAL_NAME_REFUSALS.indexOf(verdict.reason) >= NEWER_CLAUSES_FROM ? raw : undefined; } export function readPrincipal(raw) { if (raw === undefined || raw === "") return { kind: "absent" }; const verdict = judgePrincipalName(raw); return verdict.ok ? { kind: "name", name: verdict.name } : { kind: "invalid", fault: verdict.fault }; } export function readStoredPrincipal(raw) { return readPrincipal(raw === null ? undefined : raw); } export function judgedStoredPrincipalOf(raw) { const reading = readStoredPrincipal(raw); if (reading.kind === "invalid") throw new TypeError(`a stored identity member that is no principal name (${reading.fault}) was read past the row verdict`); return reading.kind === "name" ? reading.name : undefined; } export function principalNameFault(raw) { const verdict = judgePrincipalName(raw); return verdict.ok ? "a principal name" : verdict.fault; }