UNPKG

@adonis-agora/agent

Version:

Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.

193 lines • 7.68 kB
import { isSameOriginPath } from '../console-switcher.js'; /** A same-origin path with no backslash or control character (what the SPA will follow). */ function safePath(path) { // biome-ignore lint/suspicious/noControlCharactersInRegex: rejecting control characters is the point return isSameOriginPath(path) && !path.includes('\\') && !/[\u0000-\u001f\u007f]/.test(path); } function text(value) { if (typeof value === 'string' && value.trim() !== '') return value.trim(); return undefined; } /** A role's name: a string, or an object's `name` / `role` (an authz role row has `role`). */ function roleName(value) { if (typeof value === 'string') return text(value); if (typeof value !== 'object' || value === null) return undefined; const r = value; return text(r.name) ?? text(r.role); } /** Every role name in a field that is one role or a list of them. */ function roleNames(value) { const list = Array.isArray(value) ? value : [value]; const names = []; for (const item of list) { const name = roleName(item); if (name) names.push(name); } return names; } /** The one role the footer shows: the first admin-like one (`ADMIN`, `org_admin`), else the first. */ export function pickRole(names) { return names.find((name) => /admin/i.test(name)) ?? names[0]; } /** Read a property without letting a throwing getter escape. */ function read(source, key) { if (typeof source !== 'object' || source === null) return undefined; try { return source[key]; } catch { return undefined; } } /** * The roles a user object carries: `role`, `roles` (strings, `{ name }` or `{ role }` rows) and * `globalRoles`, in that order. */ function rolesOf(user) { return [ ...roleNames(read(user, 'role')), ...roleNames(read(user, 'roles')), ...roleNames(read(user, 'globalRoles')), ]; } /** * A viewer from any user-ish object: `fullName` / `name` / `email` for the label, `email`, and one * role from `role` / `roles` / `globalRoles` plus `extraRoles` (an admin-like one first, else the * first). `undefined` when nothing usable is there. */ export function viewerFromUser(user, extraRoles = []) { if (typeof user !== 'object' || user === null) return undefined; const email = text(read(user, 'email')); const label = text(read(user, 'fullName')) ?? text(read(user, 'name')) ?? email; if (!label) return undefined; const role = pickRole([...rolesOf(user), ...extraRoles]); return { label, ...(email ? { email } : {}), ...(role ? { role } : {}) }; } /** How long the footer waits for authkit's per-request identity before giving up on the role. */ const IDENTITY_TIMEOUT_MS = 250; /** * The global roles of authkit's per-request identity (`ctx.authkit`), read structurally: the * already-resolved `identity` when there is one, else `getIdentity()` (memoized per request) under * a short timeout. Any failure means no roles. */ async function authkitRoles(ctx) { const authkit = read(ctx, 'authkit'); if (typeof authkit !== 'object' || authkit === null) return []; let identity = read(authkit, 'identity'); const getIdentity = read(authkit, 'getIdentity'); if ((identity === null || identity === undefined) && typeof getIdentity === 'function') { let timer; try { identity = await Promise.race([ Promise.resolve(getIdentity.call(authkit)), new Promise((resolve) => { timer = setTimeout(() => resolve(undefined), IDENTITY_TIMEOUT_MS); }), ]); } catch { identity = undefined; } finally { clearTimeout(timer); } } return roleNames(read(identity, 'globalRoles')); } /** Keep only the fields the SPA renders, as trimmed non-empty strings. */ function cleanViewer(value) { if (typeof value !== 'object' || value === null) return undefined; const v = value; const label = text(v.label); if (!label) return undefined; const email = text(v.email); const role = text(v.role); return { label, ...(email ? { email } : {}), ...(role ? { role } : {}) }; } /** * The viewer for this request: the host's `viewer(ctx)` hook when configured (a throw or an empty * answer means no user row), else a structural read of `ctx.auth?.user` — only when the request is * already authenticated; it never triggers an authentication. The fallback's role also looks at * authkit's per-request identity (`ctx.authkit`, its `globalRoles`) once a user is there. */ export async function resolveViewer(ctx, hook) { if (hook) { try { return cleanViewer(await hook(ctx)); } catch { return undefined; } } // `read` swallows guards that throw on `.user` before authentication: that just means "no viewer". const user = read(read(ctx, 'auth'), 'user'); if (!viewerFromUser(user)) return undefined; return viewerFromUser(user, await authkitRoles(ctx)); } /** The request's CSRF token when `@adonisjs/shield` (or anything like it) put one on the request. */ export function csrfTokenOf(ctx) { const token = ctx.request.csrfToken; return typeof token === 'string' && token !== '' ? token : undefined; } /** What a registry's `account.signOut` (announced by authkit) may look like. */ function announcedSignOut(registry) { if (typeof registry !== 'object' || registry === null) return undefined; const account = registry.account; if (typeof account !== 'object' || account === null) return undefined; const signOut = account.signOut; if (typeof signOut === 'string') return signOut; if (typeof signOut === 'object' && signOut !== null) { const s = signOut; if (typeof s.action === 'string') return { action: s.action }; if (typeof s.href === 'string') return { href: s.href }; } return undefined; } /** * The sign-out the footer offers: the host's `signOut` option when set (`false` = none), else the * one an account console (authkit) announced on the `agora.consoles` registry, else none. Only * same-origin paths are ever emitted. A POST action carries the request's CSRF token when there is * one. */ export function resolveSignOut(ctx, configured, registry) { if (configured === false) return undefined; const option = configured ?? announcedSignOut(registry); if (option === undefined || option === false) return undefined; if (typeof option === 'object' && 'href' in option) { return safePath(option.href) ? { href: option.href } : undefined; } const action = typeof option === 'string' ? option : option.action; if (!safePath(action)) return undefined; const csrfToken = csrfTokenOf(ctx); return { action, ...(csrfToken ? { csrfToken } : {}) }; } /** * The data block's `viewer` and `signOut` for this request, from the console config's `viewer` hook * and `signOut` option, falling back to `ctx.auth.user` and to the sign-out an account console * announced on `registry` (the `agora.consoles` binding, read structurally; `null` when absent). */ export async function consoleShellIdentity(ctx, options, registry) { const viewer = await resolveViewer(ctx, options.viewer); const signOut = resolveSignOut(ctx, options.signOut, registry); return { ...(viewer ? { viewer } : {}), ...(signOut ? { signOut } : {}) }; } //# sourceMappingURL=console_shell.js.map