@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
265 lines • 14.2 kB
TypeScript
import type { AgentService } from '../agent-service.js';
import { type OkpJwk } from '../crypto-kit/index.js';
import { type ConsoleCapabilityTable } from '../dashboard/console_capabilities.js';
import type { AgentGovernanceQueries } from '../spi/governance-queries.js';
import type { AgentPricingStore } from '../spi/pricing-store.js';
import type { GovernanceQueriesFactory, PricingFactory, StoreContext } from '../stores/factory.js';
import { type ToolCallDecision } from '../tool-call-decision.js';
import { type AgentConsoleCommand, type AgentConsoleReply, type ChannelSecret, type ConsoleCryptoOptions, RedisConsoleChannel } from './redis_channel.js';
import { type TenantTokenSource } from './tenant_auth.js';
/**
* The agent's console capabilities, for the shared matcher (`../dashboard/console_capabilities.ts`):
* both are writes; the agent has no console reads (its console reads the other app's tables).
*/
export declare const AGENT_CONSOLE_CAPABILITY_TABLE: ConsoleCapabilityTable;
/**
* The federated agent console: one app's agent console also shows the governance data of other
* apps (runs, tool calls, threads, spend, approvals), each of which keeps its own tables and runs
* its own agent. The console reads an app's governance read-model straight from that app's tables,
* and sends what only that app can do (settling a pending approval, changing a model price) to the
* app itself over Redis.
*/
/** One other app shown in this console (`config/agent.ts` → `dashboard.apps`). */
export interface FederatedConsoleApp {
/** The name shown in the app selector. Defaults to the key. */
label?: string | undefined;
/**
* That app's governance read-model: `governanceQueries.lucid({ connection, schema })` naming where
* ITS agent tables are. Only read (never created or altered): give the role `SELECT` on them.
*/
governance: GovernanceQueriesFactory | AgentGovernanceQueries;
/**
* That app's model prices: `pricingStores.lucid({ connection, schema })`. Prices the spend
* rollups and fills the Pricing page. Omit and costs show as that app's tables recorded them, and
* the Pricing page says it is not available.
*/
pricing?: PricingFactory | AgentPricingStore | undefined;
/**
* Where that app answers (its `consoleCommands` in `config/agent.ts`): the `@adonisjs/redis`
* connection to publish on, the app's `prefix`, and how this console proves itself to it — one
* of `crypto`, `token` (+ `publicKey` without crypto) or `secret`. Without one, actions answer
* `503` (the channel refuses to send).
*/
controlPlane?: FederatedControlPlane | RedisConsoleChannel | undefined;
/**
* Send actions (approvals, price changes) to that app. Default `true`, and then the console must
* authenticate to it (automatic with `@adonis-agora/crypto` or authkit, else `controlPlane`'s
* `secret`/`token`/`crypto`): a boot error says so otherwise. `false`: browse-only, no
* `controlPlane` needed, and every action on that app answers `403`.
*/
actions?: boolean | undefined;
/**
* That app's name for `@adonis-agora/crypto` and authkit (its crypto partition, its authkit app):
* the automatic modes seal its commands with its key and ask authkit for a token for it. Default:
* this entry's key.
*/
partition?: string | undefined;
/** How long an action waits for the app to answer, in ms. Default 15 000. */
timeoutMs?: number | undefined;
}
/** {@link FederatedConsoleApp.controlPlane} as configuration. */
export interface FederatedControlPlane {
connection?: string | undefined;
prefix: string;
/**
* Shared-secret mode: the secret shared with that app (its `consoleCommands.secret`), 16+
* characters. Use it when there is no authkit and no `@adonis-agora/crypto`.
*/
secret?: ChannelSecret | undefined;
/**
* Token mode: this console's token for that app, e.g. `authkitTenantToken({ issuer, clientId,
* clientSecret, resource: 'urn:agora:agent' })` of a service-tenant client whose `partition` is
* that app and whose capabilities say what this console may do there
* (`agent:approvals:decide`, `agent:pricing:write`). The app checks it with its
* `consoleCommands.verifyTenant`. Without `crypto`, commands travel sealed to `publicKey`.
*/
token?: TenantTokenSource | false | undefined;
/**
* Token mode without `crypto`: that app's console-command public key, as printed by
* `node ace agent:console-key` on that app (a JWK, or its JSON text from an env var). Taken from
* configuration only, never from Redis.
*/
publicKey?: OkpJwk | string | (() => Promise<OkpJwk>) | undefined;
/**
* Sealed with `@adonis-agora/crypto`: the cipher this console encrypts with, for a key
* (`console-agent` by default) it may `encrypt` with and that app may `decrypt`. Where crypto is
* embedded in this app (the Hub): `() => serviceCipher(crypto.service, '<that app>')`; on a
* tenant of a crypto server: `() => crypto.client!`. Combine with `token` when the app also
* checks one. `false` turns off the automatic crypto mode for that app.
*/
crypto?: ConsoleCryptoOptions | false | undefined;
/**
* Filled by the automatic modes, not by configuration: crypto mode's token is optional, and this
* is told why when minting it failed (the command went crypto-only).
* @internal
*/
onTokenSkipped?: ((reason: string, error?: unknown) => void) | undefined;
/**
* Filled by the automatic modes, not by configuration (§13a): crypto and authkit's key directory
* are both possible; each command picks token mode when the app publishes a key, else crypto.
* @internal
*/
autoMode?: {
hasDirectoryKey(): Promise<boolean>;
} | undefined;
}
/** What a federated app sends its commands through: one channel, or the per-app mode chooser. */
export interface ConsoleCommandSender {
send(command: AgentConsoleCommand, options?: {
timeoutMs?: number | undefined;
}): Promise<AgentConsoleReply>;
close(): Promise<void>;
}
/**
* The console side's per-app mode (SECURITY-UNIFORM-SPEC §13a), chosen on the first command and
* cached: token mode when the app publishes a key in authkit's directory (an app publishes only
* when it answers in token mode), else crypto. Chosen again after 5 minutes, or when the app did
* not answer (it may not accept the mode it was sent in).
*/
export declare class AutoModeChannel implements ConsoleCommandSender {
readonly channels: {
token: ConsoleCommandSender;
crypto: ConsoleCommandSender;
};
readonly hasDirectoryKey: () => Promise<boolean>;
readonly now: () => number;
/**
* Told why when token mode's app-call token cannot be minted (a stale published key, no grant,
* authkit down): that command, and the next ones for 5 minutes, go by crypto instead (§13b).
*/
readonly onTokenUnavailable?: ((reason: string, error?: unknown) => void) | undefined;
constructor(channels: {
token: ConsoleCommandSender;
crypto: ConsoleCommandSender;
}, hasDirectoryKey: () => Promise<boolean>, now?: () => number,
/**
* Told why when token mode's app-call token cannot be minted (a stale published key, no grant,
* authkit down): that command, and the next ones for 5 minutes, go by crypto instead (§13b).
*/
onTokenUnavailable?: ((reason: string, error?: unknown) => void) | undefined);
/** The mode the next command goes in (`undefined` until the first one). */
get mode(): 'token' | 'crypto' | undefined;
send(command: AgentConsoleCommand, options?: {
timeoutMs?: number | undefined;
}): Promise<AgentConsoleReply>;
close(): Promise<void>;
}
export { FEDERATED_APP_KEY } from './app_key.js';
/**
* The X25519 key pair an app's console commands are sealed to in token mode, derived from its
* `APP_KEY` (so every instance has the same one, and a new APP_KEY makes a new one).
*/
export declare function consoleCommandKeys(appKey: string | {
release(): string;
}, prefix: string): {
publicKey: OkpJwk;
privateKey: OkpJwk;
};
/**
* A configured `controlPlane.publicKey` (a JWK or its JSON text) as an X25519 public JWK; throws
* on anything else, and never keeps a private part.
*/
export declare function parseConsolePublicKey(value: OkpJwk | string): OkpJwk;
/**
* Checks one `dashboard.apps` entry at boot, so a misconfigured app fails the boot instead of every
* request that picks it. `redisConnections` are the names in `config/redis.ts` (`undefined`: none).
*/
export declare function validateFederatedConsoleApp(key: string, entry: FederatedConsoleApp, redisConnections: {
names: string[];
default: string | undefined;
} | undefined,
/** authkit's key directory is available: a token may be sealed to the key that app publishes. */
keyDirectory?: boolean): void;
/** The commands another app's console may send this app. */
export declare const AGENT_CONSOLE_VERBS: readonly ['decide', 'upsertPrice'];
export type AgentConsoleVerb = (typeof AGENT_CONSOLE_VERBS)[number];
/** The capability each command needs (in the console's token, and in the viewer's grant). */
export declare const AGENT_CONSOLE_VERB_CAPABILITY: Record<AgentConsoleVerb, string>;
/** Every console capability the agent knows, the set a console narrows a viewer within. */
export declare const AGENT_CONSOLE_CAPABILITIES: readonly string[];
/**
* A viewer's capabilities (from the console's `authorize`) narrowed to the agent's console set:
* each console capability they hold, directly or through a wildcard (`agent:console:*`, `agent:*`,
* `*`, `agent:console:write`). `undefined` (authorize named none) is the full set.
*/
export declare function agentConsoleCapabilities(granted: readonly string[] | undefined): string[];
/** The refusal code of a command that carries no `capabilities` array (the same in every lib). */
export declare const CAPABILITIES_REQUIRED = "capabilities-required";
/** A command this app does not answer. Answered `501` by the console. */
export declare const UNSUPPORTED = "unsupported-request";
/** What a `decide` command carries. */
export interface DecideCommandArgs {
toolCallId: string;
runId?: string | undefined;
decision: ToolCallDecision;
}
/** An error carrying the HTTP status the console answers with. */
export declare class FederatedAppError extends Error {
readonly status: number;
/** The owning app's own message, for this app's log (a `502` does not show it to the browser). */
readonly detail: string | undefined;
constructor(message: string, status: number, detail?: string);
}
/**
* Run the commands another app's federated console sends this app. Only commands the channel
* authenticated reach it (shared secret, `@adonis-agora/crypto`, or a verified token), and each one
* must still pass:
*
* - `actions` — this app takes actions from another console at all (off by default);
* - token mode — the token is for THIS app (`app`) and holds the verb's capability
* (`agent:approvals:decide`, `agent:pricing:write`); the viewer capabilities the console sends
* can only narrow it;
* - otherwise — the viewer capabilities the console sends must hold it (none sent: refused).
*
* A `decide` settles a pending tool call under the same rules as `POST <path>/tool-call/approve|
* reject`, with the console viewer as the deciding actor and cross-actor privilege only because
* the checks above passed; an approval that names its own approver still asks the approval policy.
* Each command is logged with who sent it.
*/
export declare function agentConsoleCommandHandler(options: {
service: AgentService;
pricing?: AgentPricingStore | undefined;
/** Take actions (decide, upsertPrice) from another console. Default `false`: refuse them. */
actions?: boolean | undefined;
/** This app's partition: a verified token must be for it. */
app?: string | undefined;
log?: ((line: string) => void) | undefined;
}): (command: AgentConsoleCommand) => Promise<unknown>;
/** Another app as the console sees it: its read-model, its prices and its command channel. */
export declare class FederatedApp {
readonly id: string;
readonly label: string;
readonly governance: AgentGovernanceQueries;
readonly pricing: AgentPricingStore | undefined;
/** Absent for a browse-only app (`actions: false`). */
readonly channel: ConsoleCommandSender | undefined;
readonly timeoutMs: number | undefined;
/** This console's name (its `appLabel`), sent with each command for the app's log. */
readonly via: string | undefined;
constructor(options: {
id: string;
label?: string | undefined;
governance: AgentGovernanceQueries;
pricing?: AgentPricingStore | undefined;
channel?: ConsoleCommandSender | undefined;
timeoutMs?: number | undefined;
via?: string | undefined;
});
/**
* Send `verb` to the app and return its answer. `503` (naming the app) when it does not answer
* (or this console has no authentication for it), `501` for a command it does not run, the app's
* own status for its refusals, else `502`. The viewer's grant travels as `capabilities` (what the
* console's `authorize` returned, narrowed to the agent's console set, or the full set): the app
* checks the verb's capability in it, against its own rules (and, in token mode, its token's).
*/
send(verb: AgentConsoleVerb, args: unknown[], actor?: string,
/** The viewer's console capabilities (see {@link agentConsoleCapabilities}). Default: the verb's. */
capabilities?: readonly string[]): Promise<unknown>;
close(): Promise<void>;
}
/** Build one configured app: its read-model and prices (read-only) and its command channel. */
export declare function buildFederatedAgentApp(id: string, config: FederatedConsoleApp, app: StoreContext['app'], options?: {
via?: string | undefined;
}): Promise<FederatedApp>;
//# sourceMappingURL=console.d.ts.map