@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
54 lines • 2.69 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`.
* - `action: 'write'` (a change) and `{ allowed: true, access: 'read' }` → `403 forbidden`: a
* read-level grant never changes anything.
*/
export async function evaluateGovernanceGate(actor, ctx, authorize, debug = false, action = 'read') {
if (authorize === undefined)
return { ok: true, status: 200 };
const decision = await consoleAuthorizeFor(authorize)(ctx, actor, { action });
if (decision.allowed && action === 'write' && decision.access === 'read') {
return {
ok: false,
status: 403,
error: 'forbidden',
...(debug ? { detail: "this access is read-only (`access: 'read'`)" } : {}),
};
}
if (decision.allowed) {
return {
ok: true,
status: 200,
...(decision.tenants !== undefined ? { tenants: decision.tenants } : {}),
...(decision.apps !== undefined ? { apps: decision.apps } : {}),
...(decision.access !== undefined ? { access: decision.access } : {}),
...(decision.capabilities !== undefined ? { capabilities: decision.capabilities } : {}),
};
}
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