UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

44 lines (43 loc) • 1.87 kB
export const ENABLE_BUILTIN = "enable"; const BUILTIN_KEY_PREFIX = "[]"; export const builtinKey = (name) => BUILTIN_KEY_PREFIX + name; export const isBuiltinKey = (name) => name.startsWith(BUILTIN_KEY_PREFIX); export const BUILTINS_ALL = builtinKey("*"); export const BUILTIN_ENABLED = [{ spelled: "enabled" }]; const BUILTIN_DISABLED = [{ spelled: "disabled" }]; export const isDisabledValue = (v) => v.spelled === "disabled"; export function chainAvailability(words, ask) { const states = words.map((w) => ask?.(w) ?? "enabled"); return states.includes("disabled") ? "disabled" : states.includes("maybe") ? "maybe" : "enabled"; } export const speaksFor = (subject, name) => subject === undefined || subject === name || (subject === BUILTINS_ALL && isBuiltinKey(name)); const ENABLE_SWITCHES = "adfnps"; export function enableBindings(args) { const computed = (w) => w.pieces.some((p) => p.expansion !== undefined || p.literal === undefined); let disable = false; let other = false; let at = 0; for (; at < args.length; at++) { const w = args[at]; if (w.folded === "--") { at++; break; } if (computed(w) || w.folded.length < 2 || !w.folded.startsWith("-")) break; for (const letter of w.folded.slice(1)) { if (!ENABLE_SWITCHES.includes(letter)) return []; if (letter === "f") return [{ name: BUILTINS_ALL, state: "unknown" }]; if (letter === "n") disable = true; else other = true; } } const names = args.slice(at); if (other || names.some(computed)) return [{ name: BUILTINS_ALL, state: "unknown" }]; return names.map((w) => ({ name: builtinKey(w.folded), state: disable ? BUILTIN_DISABLED : BUILTIN_ENABLED })); }