Skip to the content.

API

createAuthority(options)

import { createAuthority } from "@solustiq/authority"
const authority = createAuthority({ store, tenant, tools, rules, intents, unknown, confirmKey, isWrite, autonomy, clock, messages, onDecision })
Option Type Default Notes
store AuthorityStore required memoryStore() or sqlStore({ db }).
tenant string "" At most 200 characters. One instance per tenant.
tools ToolDefinition[] [] See below.
rules Rule[] [] In order; first classification wins; may be async.
intents IntentPattern[] [] Regexes see the lowercased request; functions the original.
unknown "B" \| "C" "B" The level of an unregistered tool no rule decided.
confirmKey string "confirm" The argument that confirms a two-step tool.
isWrite (tool, args, level) => boolean level is B Which calls a C intent stops.
autonomy () => boolean always true Read through enabled().
clock () => Date new Date() For tests.
messages Partial<Messages> English parked, refusedTool, refusedIntent, queueFailed.
onDecision (d: DecisionRecord) => void none Observer; its errors are swallowed.

ToolDefinition

{ name: string; level: "A" | "B"; confirmLevel?: "B"; levelFor?: (args) => { level: "A" | "B" | "C"; reason: string } }

Names match ^[A-Za-z0-9][A-Za-z0-9._:-]{0,99}$. levelFor wins over confirmLevel, which wins over level.

Rule

(call: { tool; args; ctx; registered: ToolDefinition | null }) => Classification | null | Promise<Classification | null>

Included: directoryRecipientRule({ tool, field?, directory, selfOk? }), probationRule({ tools, until, reason?, clock? }), fixedRule({ [tool]: classification }), refuseWhenRule({ tool, when, reason }).

authority.check(input)

const r = await authority.check({ tool, args, ctx: { agentId, channel, byEmail?, request? }, intent? })

Returns one of:

Never throws. intent undefined means “detect it from ctx.request”; null means “already screened, nothing found”; a string means “refuse writes for this label”.

authority.actions

Method Returns
list({ status?, agentId?, limit? }) Newest first, at most 500.
get(id) The action or null.
approve(id, by, run) { ok: true, action, result } or { ok: false, reason: "not_pending" \| "failed", action, error? }.
reject(id, by, note?) The rejected action, or null when it was not waiting.
expire(olderThan) How many waiting actions older than the Date (or the age in milliseconds) were expired.

authority.decisions

record({ agentId, level, tool, args, channel, byEmail?, outcome, actionId?, approvedBy?, note? }) for A calls after they ran; recent(limit = 50).

Other methods

classify(tool, args, ctx), classifySync(tool, args, ctx) (synchronous rules only), tool(name), intent(text), register(tools) (replace), add(tools) (merge), enabled().

Stores

runAcceptance(authority, { ctx, via, cRequests, bCalls })

Returns { refused, parked, failures }. Assert failures is empty and the counts equal the list lengths, with the lists frozen in your test.