@sema-agent/core
Version:
Stateless, task-oriented AI agent core
38 lines (37 loc) • 1.41 kB
JavaScript
export const HOOK_FAMILIES = Object.freeze([
"preToolUse",
"postToolUse",
"userPromptSubmit",
"stop",
"postToolUseFailure",
"postToolBatch",
"preCompact",
"postCompact",
"stopFailure",
"permissionDenied",
]);
export const HOOKS_INVALID_CODE = "config.hooks_invalid";
export function assertHookSeatsCallable(hooks, provenance) {
if (hooks === undefined)
return;
for (const family of HOOK_FAMILIES) {
const seat = hooks[family];
if (seat === undefined || typeof seat === "function")
continue;
const e = new Error(`the resolved hooks record (${provenance}) holds ${seat === null ? "null" : typeof seat} at \`${family}\` — a hook seat is a callback or it is absent. ` +
`A present non-callback is not an unwired seat: the tool gate arms on presence and would refuse every call through it. Omit the key, or wire a function.`);
e.code = HOOKS_INVALID_CODE;
throw e;
}
}
export function wiredHookEntries(hooks, owner, preToolUseObservational) {
if (hooks === undefined)
return [];
const entries = [];
for (const family of HOOK_FAMILIES) {
if (typeof hooks[family] !== "function")
continue;
entries.push({ family, owner, ...(family === "preToolUse" && preToolUseObservational ? { observational: true } : {}) });
}
return entries;
}