@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
125 lines • 6.46 kB
TypeScript
import type { HttpContext } from '@adonisjs/core/http';
import { type AuthorizeResult } from './authorize-decision.js';
import type { Actor } from './types.js';
/**
* What a console `authorize` returns — the contract shared by every `@adonis-agora/*` console
* (durable, telescope, media, payments, agent, authkit admin). Each lib carries its own identical
* copy, so one authorizer class satisfies every console.
*
* - `true` / `{ allowed: true }` → allowed.
* - `false` / `{ allowed: false }` / `reason: 'forbidden'` → `403`.
* - `{ allowed: false, reason: 'unauthenticated' }` → `401`.
* - `{ redirectTo }` → a `302` to that path for a page navigation, `401` for anything else.
*/
export type ConsoleAuthorizeResult = boolean | {
allowed: true;
} | {
allowed: false;
reason?: 'unauthenticated' | 'forbidden';
} | {
redirectTo: string;
};
/** A class form, for an authorizer in its own file (`@inject()` works). */
export interface ConsoleAuthorizer {
authorize(ctx: HttpContext): ConsoleAuthorizeResult | Promise<ConsoleAuthorizeResult>;
}
export type ConsoleAuthorizerClass = new (...args: any[]) => ConsoleAuthorizer;
/**
* A console `authorize` in one of three forms: an inline function, an authorizer class (built
* through the container, so `@inject()` works), or a lazy import of a module whose default export
* is that class.
*/
export type ConsoleAuthorize = ((ctx: HttpContext) => ConsoleAuthorizeResult | Promise<ConsoleAuthorizeResult>) | ConsoleAuthorizerClass | (() => Promise<{
default: ConsoleAuthorizerClass;
}>);
/**
* What the agent's console hooks may return: the shared {@link ConsoleAuthorizeResult}, or an
* {@link AuthorizeResult} (`{ allowed: boolean }`, what `authorizeByRoles` from
* `@adonis-agora/authz` returns).
*/
export type AgentConsoleAuthorizeResult = ConsoleAuthorizeResult | AuthorizeResult;
/**
* The class form of the agent console's `authorize`. Like {@link ConsoleAuthorizer}, plus the
* agent's resolved {@link Actor} as an optional second argument (`undefined` when the request has
* none) — so a class written for any other console works here unchanged.
*/
export interface AgentConsoleAuthorizer {
authorize(ctx: HttpContext, actor?: Actor): AgentConsoleAuthorizeResult | Promise<AgentConsoleAuthorizeResult>;
}
export type AgentConsoleAuthorizerClass = new (...args: any[]) => AgentConsoleAuthorizer;
/**
* The agent console's `dashboard.authorize`: the shared {@link ConsoleAuthorize} (every value of it is
* accepted), with the resolved {@link Actor} handed over as an optional second argument.
*
* ```ts
* authorize: (ctx) => ctx.auth.user?.isAdmin === true,
* authorize: (ctx, actor) => actor?.roles?.includes('ADMIN') ?? false,
* authorize: authorizeByRoles({ roles: ['ADMIN'] }),
* authorize: ConsoleAccess,
* authorize: () => import('#policies/console_access'),
* ```
*/
export type AgentConsoleAuthorize = ((ctx: HttpContext, actor?: Actor) => AgentConsoleAuthorizeResult | Promise<AgentConsoleAuthorizeResult>) | AgentConsoleAuthorizerClass | (() => Promise<{
default: AgentConsoleAuthorizerClass;
}>);
/**
* A console authorize result, normalised. A denial carries its HTTP status, the `redirectTo` path
* when the hook asked for one, and the thrown error when the hook threw.
*/
export type ConsoleAuthorizeDecision = {
allowed: true;
} | {
allowed: false;
status: 401 | 403;
redirectTo?: string;
error?: unknown;
};
/** Normalise any value an authorize hook returned. Anything unrecognised denies (`403`). */
export declare function normalizeConsoleAuthorizeResult(result: unknown): ConsoleAuthorizeDecision;
/** A resolved console authorize: run it for a request, get a normalised decision. */
export type ResolvedConsoleAuthorize = (ctx: HttpContext, actor?: Actor) => Promise<ConsoleAuthorizeDecision>;
/**
* Turn a console `authorize` (any of the three forms) into one function returning a normalised
* decision. A class is built through `ctx.containerResolver` on each request; a lazy import is
* imported once and its class remembered. A thrown error is a `403` denial, never an allow.
*/
export declare function resolveConsoleAuthorize(authorize: AgentConsoleAuthorize): ResolvedConsoleAuthorize;
/**
* {@link resolveConsoleAuthorize}, memoised per configured value — so a lazy import configured once
* in `config/agent.ts` is imported once, however many routes and requests run it.
*/
export declare function consoleAuthorizeFor(authorize: AgentConsoleAuthorize): ResolvedConsoleAuthorize;
/**
* Whether the request is a browser page navigation (it accepts HTML) — the only kind of request a
* `{ redirectTo }` decision is answered with a redirect; everything else gets a `401`.
*/
export declare function isPageNavigation(ctx: HttpContext): boolean;
/** Options for {@link authorizeByToken}. */
export interface AuthorizeByTokenOptions {
/** The shared secret callers send as `Authorization: Bearer <token>`. */
token: string | undefined;
}
/**
* An `authorize` check for machine access: allows a request whose `Authorization` header is
* `Bearer <token>`, and answers `{ allowed: false, reason: 'unauthenticated' }` (`401`) otherwise.
*
* ```ts
* dashboard: { authorize: authorizeByToken({ token: env.get('CONSOLE_TOKEN') }) },
* ```
*
* Throws at config time when `token` is empty or undefined.
*/
export declare function authorizeByToken(options: AuthorizeByTokenOptions): ConsoleAuthorize;
/** The boot warning logged once when the console has no `authorize`. Same wording in every console. */
export declare const NO_CONSOLE_AUTHORIZE_WARNING = "[@adonis-agora/agent] console has no `authorize` configured: it is open outside production and denies every request in production. Set `authorize` in config/agent.ts.";
/**
* Log {@link NO_CONSOLE_AUTHORIZE_WARNING} once per application, however many providers (the agent's
* governance routes, the console shell) find `authorize` missing.
*/
export declare function warnNoConsoleAuthorizeOnce(app: object, warn: (message: string) => void): void;
/**
* Whether the console refuses the request before anything else runs: `authorize` is omitted and the
* app is in production. (Omitted outside production, the console is open.)
*/
export declare function consoleClosedByDefault(authorize: AgentConsoleAuthorize | undefined, inProduction: boolean): boolean;
//# sourceMappingURL=console-authorize.d.ts.map