@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
36 lines • 1.85 kB
JavaScript
import { consoleAuthorizeFor } from './console-authorize.js';
/**
* Decide whether a request (and its actor, when one resolved) passes the console's `authorize` — the
* router-free core of the `/agent/governance/*` gate (and of the "may act across actors" check on the per-actor
* run/thread routes), extracted so it can be unit tested without booting an app.
*
* - No `authorize` → `{ ok: true }`. The provider handles the omitted case before calling (open
* outside production, `403` in production), and never treats it as cross-actor privilege.
* - `true` / `{ allowed: true }` → `{ ok: true }`.
* - `false` / `{ allowed: false }` / `reason: 'forbidden'` → `403 forbidden`.
* - `{ allowed: false, reason: 'unauthenticated' }` → `401 unauthenticated`.
* - `{ redirectTo }` → `401 unauthenticated` carrying `redirectTo`.
* - a throw → `403 forbidden`; with `debug` the thrown message rides along as `detail`.
*/
export async function evaluateGovernanceGate(actor, ctx, authorize, debug = false) {
if (authorize === undefined)
return { ok: true, status: 200 };
const decision = await consoleAuthorizeFor(authorize)(ctx, actor);
if (decision.allowed)
return { ok: true, status: 200 };
if (decision.error !== undefined) {
return {
ok: false,
status: 403,
error: 'forbidden',
...(debug && decision.error instanceof Error ? { detail: decision.error.message } : {}),
};
}
if (decision.redirectTo !== undefined) {
return { ok: false, status: 401, error: 'unauthenticated', redirectTo: decision.redirectTo };
}
return decision.status === 401
? { ok: false, status: 401, error: 'unauthenticated' }
: { ok: false, status: 403, error: 'forbidden' };
}
//# sourceMappingURL=governance-gate.js.map