UNPKG

@adonis-agora/agent

Version:

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

741 lines • 33 kB
import { createHmac, randomBytes, randomUUID, timingSafeEqual } from 'node:crypto'; import { openWith, sealTo } from '../crypto-kit/index.js'; import { canonicalJson, resolveTenantToken, resolveTenantVerifier, } from './tenant_auth.js'; /** * The Redis channel the federated console talks to another app over: the console publishes a * command on `<prefix>-agent-console`, every instance of the owning app hears it, the one that wins * `SET <prefix>-agent-console-claim:<id> NX PX` runs it, and the answer comes back on * `<prefix>-agent-console-reply`. Same design as `@adonis-agora/config`'s and * `@adonis-agora/durable`'s console commands, on channels of its own (`-agent-`), so all of them can * share an app's prefix. Ported from `@adonis-agora/config`'s channel (only {@link CONSOLE_TOPIC} * and the names differ). * * Redis is not a trust boundary: anyone who can publish there could otherwise approve a pending tool * call as any approver or rewrite prices. Every command is authenticated (one of three modes, see * {@link RedisConsoleChannel}), refused when older than a minute, claimed once (a replay reuses the * id and loses the claim), and every answer is MACed so a forged one is ignored. */ /** This package's part of the channel names. */ export const CONSOLE_TOPIC = 'agent'; const PACKAGE = `@adonis-agora/${CONSOLE_TOPIC}`; /** How far a command's `ts` may be from this clock (and so how long a replay could live). */ const MAX_COMMAND_SKEW_MS = 60_000; function sameMac(expected, actual) { if (typeof actual !== 'string') return false; const left = Buffer.from(expected); const right = Buffer.from(actual); return left.length === right.length && timingSafeEqual(left, right); } function fresh(ts) { return typeof ts === 'number' && Math.abs(Date.now() - ts) <= MAX_COMMAND_SKEW_MS; } /** * Signs and checks the messages of one channel under a secret both sides share: every command and * reply carries an HMAC over its fields, bound to this channel's prefix. */ class ChannelSigner { #key; constructor(secret, prefix) { const raw = typeof secret === 'string' ? secret : secret?.release?.(); if (typeof raw !== 'string' || raw.length < 16) { throw new Error(`[${PACKAGE}] console channel: the shared secret must be at least 16 characters`); } this.#key = createHmac('sha256', raw) .update(`agora:console-commands:${CONSOLE_TOPIC}:${prefix}`) .digest(); } #mac(kind, fields) { return createHmac('sha256', this.#key) .update(`${kind}\n${canonicalJson(fields)}`) .digest('base64url'); } #commandFields(command) { return [ command.id, command.verb, command.args ?? [], command.actor ?? null, command.capabilities ?? null, command.via ?? null, command.ts ?? null, ]; } #replyFields(reply) { return [reply.id, reply.ok, reply.ok ? (reply.data ?? null) : reply.error]; } signCommand(command) { const { mac: _mac, verified: _verified, ...rest } = command; const stamped = { ...rest, ts: Date.now() }; return { ...stamped, mac: this.#mac('command', this.#commandFields(stamped)) }; } verifyCommand(command) { return fresh(command.ts) && this.verifyMac(command); } verifyMac(command) { return sameMac(this.#mac('command', this.#commandFields(command)), command.mac); } signReply(reply) { const { mac: _mac, ...rest } = reply; return { ...rest, mac: this.#mac('reply', this.#replyFields(rest)), }; } verifyReply(reply) { return sameMac(this.#mac('reply', this.#replyFields(reply)), reply.mac); } } /** The owning app refused the console's token (the console drops its cached token). */ export const UNAUTHORIZED_CONSOLE = 'federated-console-unauthorized'; /** Shared-secret mode: both sides hold the same secret and HMAC every command and reply. */ class SecretAuth { #signer; constructor(secret, prefix) { this.#signer = new ChannelSigner(secret, prefix); } async wrap(command) { return { wire: this.#signer.signCommand(command), verifyReply: (reply) => this.#signer.verifyReply(reply), }; } async unwrap(wire) { if (typeof wire.mac !== 'string') return null; const command = wire; if (typeof command.id !== 'string' || typeof command.verb !== 'string') return null; // Tampered or forged: not from the console that holds the secret; stale: over a minute old. A // replay inside the window reuses the id, and the claim (kept 2 minutes) refuses it. if (!this.#signer.verifyMac(command)) throw refusedCommand('bad signature'); if (!fresh(command.ts)) throw refusedCommand('stale'); const { mac: _mac, ts: _ts, verified: _verified, ...fields } = command; return { id: command.id, command: fields, signReply: (reply) => this.#signer.signReply(reply), }; } } /** * The `@adonis-agora/crypto` key console commands are sealed with, unless another is named. Every * Agora lib names its key `console-<lib>` (an app opens its own `console-*` keys with no grant). */ export const CONSOLE_CRYPTO_KEY = 'console-agent'; /** Seals with an `@adonis-agora/crypto` key: whoever may `encrypt` with it sends, the app opens. */ export function cryptoConsoleSealer(options) { const key = options.key ?? CONSOLE_CRYPTO_KEY; const source = options.cipher; const cipher = async () => { const resolved = typeof source === 'function' ? await source() : source; if (!resolved || typeof resolved.encrypt !== 'function' || typeof resolved.decrypt !== 'function') { throw new Error(`[${PACKAGE}] console channel: \`crypto.cipher\` is not ready (is @adonis-agora/crypto booted?)`); } return resolved; }; return { // `agora:ct1:<key>:<version>:…`: only this key's ciphertext is taken as a crypto command. owns: (sealed) => options.owns !== undefined ? options.owns(sealed) : sealed.startsWith(`agora:ct1:${key}:`), async seal(plaintext, aad) { const { ciphertext } = await (await cipher()).encrypt({ key, plaintext, aad, context: aad }); return ciphertext; }, async open(sealed, aad) { const { plaintext } = await (await cipher()).decrypt({ key, ciphertext: sealed, aad, context: aad, }); return plaintext; }, }; } /** Seals to the app's X25519 command key: the console side (the app's public key, configured). */ export function appKeySealerTo(recipient) { const key = typeof recipient === 'function' ? recipient : async () => recipient; return { owns: (sealed) => sealed.startsWith('agora:pk1:'), seal: async (plaintext, aad) => sealTo(await key(), plaintext, { aad }), open: async () => { throw new Error('the console side cannot open commands'); }, }; } /** Opens with the app's X25519 command key pair (derived from its `APP_KEY`): the app side. */ export function appKeySealer(keys) { return { owns: (sealed) => sealed.startsWith('agora:pk1:'), seal: async (plaintext, aad) => sealTo(keys.publicKey, plaintext, { aad }), open: async (sealed, aad) => openWith(keys.privateKey, sealed, { aad }), }; } /** The key that MACs the replies to one command: a secret only the console and the app know. */ function replyKey(secret, prefix, id) { return createHmac('sha256', Buffer.from(secret, 'base64url')) .update(`agora:console-reply:${CONSOLE_TOPIC}:${prefix}:${id}`) .digest(); } function replyMac(key, reply) { const fields = [reply.id, reply.ok, reply.ok ? (reply.data ?? null) : reply.error]; return createHmac('sha256', key) .update(`reply\n${canonicalJson(fields)}`) .digest('base64url'); } const commandAad = (prefix, id) => JSON.stringify(['agora:console-command', CONSOLE_TOPIC, prefix, id]); /** * Sealed mode (console side): the whole command, with the console's token when it has one, travels * sealed so only the owning app opens it. A Redis reader sees an id and ciphertext: no token to * lift and replay, no command to alter. The sealed payload also carries a one-off secret the app * MACs its answer with, so a forged answer (from someone who cannot open the command) is ignored. */ class SealedSenderAuth { sealer; prefix; onTokenSkipped; #token; /** Crypto mode with an automatic token: until when a failed mint is not retried. */ #skipTokenUntil = 0; constructor(sealer, token, prefix, /** The token is optional (crypto mode): a failed mint sends without it, retried in 5 min. */ onTokenSkipped) { this.sealer = sealer; this.prefix = prefix; this.onTokenSkipped = onTokenSkipped; this.#token = token === undefined ? undefined : resolveTenantToken(token); } /** * The token for this command. Optional (crypto mode, §13b): a token that fails to mint is left * out, said (the callback dedupes per reason), and asked for again after 5 minutes. Required * (token mode): its error stands, coded {@link TOKEN_UNAVAILABLE} so a console that can also * reach the app by crypto falls back (§13a). */ async #tokenFor() { if (!this.#token) return undefined; const optional = this.onTokenSkipped !== undefined; if (optional && Date.now() < this.#skipTokenUntil) return undefined; try { return await this.#token.get(); } catch (error) { const reason = String(error?.reason ?? (error instanceof Error ? error.message : String(error))); if (!this.onTokenSkipped) { // The full message (with the grant hint) stands; the raw reason is for the §13b warning. const message = error instanceof Error ? error.message : reason; throw Object.assign(new AgentConsoleChannelError(message, TOKEN_UNAVAILABLE, reason), { cause: error, }); } this.#skipTokenUntil = Date.now() + TOKEN_RETRY_MS; try { this.onTokenSkipped(reason, error); } catch { // a logger that throws must not block the command } return undefined; } } async wrap(command) { const token = await this.#tokenFor(); const secret = randomBytes(32).toString('base64url'); const { mac: _mac, verified: _verified, ...fields } = command; const sealed = await this.sealer.seal(JSON.stringify({ command: { ...fields, ts: Date.now() }, ...(token !== undefined ? { token } : {}), replySecret: secret, }), commandAad(this.prefix, command.id)); const key = replyKey(secret, this.prefix, command.id); return { wire: { id: command.id, sealed }, verifyReply: (reply) => sameMac(replyMac(key, reply), reply.mac), }; } unauthorized() { this.#token?.reset?.(); } } /** * Sealed mode (owning app side): open, check freshness, and — when `verifyTenant` is set — verify * the token the command carries. With a token the command's capabilities come from it; a command * without one is accepted only when no `verifyTenant` is set (crypto mode: whoever may encrypt with * the key is the console, like a shared secret). */ class SealedReceiverAuth { sealer; prefix; app; tokenOptional; #verify; constructor(sealer, verifyTenant, prefix, app, /** A command without a token is accepted (crypto authenticates the console); one with a token must pass it. */ tokenOptional = false) { this.sealer = sealer; this.prefix = prefix; this.app = app; this.tokenOptional = tokenOptional; this.#verify = verifyTenant === undefined ? undefined : resolveTenantVerifier(verifyTenant); } async unwrap(wire) { if (typeof wire.sealed !== 'string' || typeof wire.id !== 'string') return null; if (!this.sealer.owns(wire.sealed)) return null; const id = wire.id; let payload; try { payload = JSON.parse(await this.sealer.open(wire.sealed, commandAad(this.prefix, id))); } catch { throw refusedCommand("did not open with this app's key"); } const command = payload?.command; if (!command || command.id !== id || typeof command.verb !== 'string' || typeof payload.replySecret !== 'string' || !fresh(command.ts)) { throw refusedCommand(command && command.id === id && !fresh(command.ts) ? 'stale' : "did not open with this app's key"); } const key = replyKey(payload.replySecret, this.prefix, id); const signReply = (reply) => { const { mac: _mac, ...rest } = reply; return { ...rest, mac: replyMac(key, rest) }; }; const { mac: _mac, ts: _ts, verified: _claimed, ...fields } = command; if (!this.#verify) return { id, signReply, command: fields }; const carriesToken = typeof payload.token === 'string' && payload.token !== ''; // A verifier from a fallback (authkit's app-call verifier): crypto alone authenticates a // command without a token, and a token, when sent, must still pass and caps the command. if (!carriesToken && this.tokenOptional) return { id, signReply, command: fields }; let verified = null; try { verified = typeof payload.token === 'string' && payload.token !== '' ? await this.#verify({ token: payload.token, app: this.app, partition: this.app, tenant: this.app, headers: {}, }) : null; } catch { verified = null; } if (!verified) { return { id, signReply, error: { message: `the console's token was refused by the "${this.prefix}" app`, code: UNAUTHORIZED_CONSOLE, status: 401, }, }; } return { id, signReply, command: { ...fields, verified: { partition: verified.partition, capabilities: verified.capabilities, ...(verified.clientId !== undefined ? { clientId: verified.clientId } : {}), ...(verified.audienceBound === true ? { audienceBound: true } : {}), }, }, }; } } /** How long a command waits for its answer by default. */ export const AGENT_CONSOLE_TIMEOUT_MS = 15_000; /** How long the claim on a command lives (longer than any answer is waited for). */ const CLAIM_TTL_MS = 120_000; /** How long a console waits before minting a failed optional token again. */ const TOKEN_RETRY_MS = 5 * 60_000; /** How many claimed command ids an instance remembers to recognise a replay. */ const MAX_RAN_IDS = 10_000; function isIoredis(client) { return typeof client.duplicate === 'function'; } /** `SET key value PX ttl NX` on either client: `'OK'` for the one caller that wins the key. */ function claimOf(client) { const target = typeof client.set === 'function' ? client : client.ioConnection; const set = target?.set; if (typeof set !== 'function') return undefined; return (key, value) => set.call(target, key, value, 'PX', CLAIM_TTL_MS, 'NX'); } /** A command the owning app refused or failed, or that nobody answered. */ export class AgentConsoleChannelError extends Error { code; /** Set when this app refused an incoming command: the uniform reason the app logs. */ reason; constructor(message, code, reason) { super(message); this.name = 'AgentConsoleChannelError'; this.code = code; this.reason = reason; } } /** * An incoming command this app refused, with the uniform reason every Agora lib logs: `not JSON`, * `no command id`, `did not open with this app's key`, `bad signature`, `stale`, `token refused`, * `capabilities-required`, or `mode not accepted here (…)`. */ export function refusedCommand(reason) { return new AgentConsoleChannelError(`refused a command (${reason})`, UNSIGNED_CHANNEL, reason); } /** Token mode: the console's app-call token could not be minted (no grant, authkit down). */ export const TOKEN_UNAVAILABLE = 'federated-console-token-unavailable'; /** Nobody listens on the app's channel, or nobody answered in time. */ export const APP_UNREACHABLE = 'federated-app-unreachable'; /** The channel has no authentication, or a command arrived without valid authentication. */ export const UNSIGNED_CHANNEL = 'federated-channel-unsigned'; export class RedisConsoleChannel { prefix; #client; #subscriptions = []; #handlers = new Map(); #pending = new Map(); #subscriber; #replies; #answering = false; #closed = false; /** How commands are sent (one mode). */ #sender; /** How commands are accepted (each configured mode; an app may accept several while migrating). */ #receivers; /** Command ids this instance claimed, until their claim expires: how a replay is told apart. */ #ran = new Map(); /** The modes this channel accepts, for the "mode not accepted here" reason. */ #acceptedModes; /** * Redis is not a trust boundary, so a channel neither sends nor answers commands without one of: * * - `crypto` — commands sealed with an `@adonis-agora/crypto` key (the console may encrypt with it, * the app decrypt), optionally with a `token` the app checks with `verifyTenant`; * - a `token` sealed to the app's command key (`recipient` / `keys`, from its `APP_KEY`), checked * with `verifyTenant` (authkit, typically); * - `secret` — shared by the console and the app (the same value on both sides). */ constructor(options) { if (!options.prefix) throw new Error(`[${PACKAGE}] console channel: prefix is required`); this.#client = options.client; this.prefix = options.prefix; const secret = options.secret === undefined ? undefined : new SecretAuth(options.secret, options.prefix); const crypto = options.crypto ? cryptoConsoleSealer(options.crypto) : undefined; // Sending: crypto when configured, else a token sealed to the app's key, else the secret. if (options.token !== undefined && !crypto && !options.recipient) { throw new Error(`[${PACKAGE}] console channel: a \`token\` needs \`crypto\` or the app's \`publicKey\``); } const sendSealer = crypto ?? (options.recipient ? appKeySealerTo(options.recipient) : undefined); const sealedSender = sendSealer && (crypto || options.token !== undefined) ? new SealedSenderAuth(sendSealer, options.token, options.prefix, crypto ? options.onTokenSkipped : undefined) : undefined; this.#sender = sealedSender ?? secret; // Answering: every configured way. Without crypto, sealed commands need a token check. const receivers = []; if (crypto) { receivers.push(new SealedReceiverAuth(crypto, options.verifyTenant, options.prefix, options.app, options.tokenOptional === true)); } if (options.keys && options.verifyTenant !== undefined) { receivers.push(new SealedReceiverAuth(appKeySealer(options.keys), options.verifyTenant, options.prefix, options.app)); } if (secret) receivers.push(secret); this.#acceptedModes = [ ...(crypto ? ['crypto'] : []), ...(options.keys && options.verifyTenant !== undefined ? ['token'] : []), ...(secret ? ['secret'] : []), ]; this.#receivers = receivers; } /** Remember that this instance claimed `id` (bounded: expired ids go first, then the oldest). */ #remember(id) { const now = Date.now(); for (const [ran, expires] of this.#ran) { if (expires > now && this.#ran.size < MAX_RAN_IDS) break; this.#ran.delete(ran); } this.#ran.set(id, now + CLAIM_TTL_MS); } /** Whether this instance claimed `id` within the claim's lifetime. */ #ranHere(id) { const expires = this.#ran.get(id); return expires !== undefined && expires > Date.now(); } /** Whether this channel can send commands (crypto, a token and the app's key, or a secret). */ get signed() { return this.#sender !== undefined; } #unsigned() { return new AgentConsoleChannelError(`the "${this.prefix}" ${CONSOLE_TOPIC} console channel has no authentication; use @adonis-agora/crypto (\`crypto\` on both sides), authkit (a \`token\` on the console, \`verifyTenant\` on the app) or the same \`secret\` on both`, UNSIGNED_CHANNEL); } get commandChannel() { return `${this.prefix}-${CONSOLE_TOPIC}-console`; } get replyChannel() { return `${this.prefix}-${CONSOLE_TOPIC}-console-reply`; } /** Subscribe `handler` to `channel`; resolves once the subscription is in place. */ async #subscribe(channel, handler) { const client = this.#client; if (isIoredis(client)) { if (!this.#subscriber) { const sub = client.duplicate(); this.#subscriber = sub; sub.on('message', (ch, payload) => this.#handlers.get(ch)?.(payload)); } this.#handlers.set(channel, handler); await this.#subscriber.subscribe(channel); return; } await client.subscribe(channel, handler); this.#subscriptions.push(() => void client.unsubscribe?.(channel)); } /** * Send `command` to the owning app and wait for its answer. Rejects at once when the channel has * no authentication or no instance listens (`PUBLISH` reached nobody), and after `timeoutMs` when * nobody answers. An answer without this command's MAC is ignored. */ async send(command, options = {}) { if (this.#closed) { throw new AgentConsoleChannelError('console channel closed', APP_UNREACHABLE); } const sender = this.#sender; if (!sender?.wrap) throw this.#unsigned(); let outgoing; try { outgoing = await sender.wrap(command); } catch (error) { if (error instanceof AgentConsoleChannelError) throw error; throw new AgentConsoleChannelError(`could not prepare the ${CONSOLE_TOPIC} console command for "${this.prefix}": ${error instanceof Error ? error.message : String(error)}`, APP_UNREACHABLE); } if (!this.#replies) { const replies = this.#subscribe(this.replyChannel, (payload) => { let reply; try { reply = JSON.parse(payload); } catch { return; } if (typeof reply?.id !== 'string') return; this.#pending.get(reply.id)?.(reply); }); this.#replies = replies; // A failed subscribe (a Redis blip) is retried by the next send, not kept for good. replies.catch(() => { if (this.#replies === replies) this.#replies = undefined; }); } try { await this.#replies; } catch (error) { throw new AgentConsoleChannelError(`could not subscribe to the "${this.prefix}" app's console replies: ${error instanceof Error ? error.message : String(error)}`, APP_UNREACHABLE); } const timeoutMs = options.timeoutMs ?? AGENT_CONSOLE_TIMEOUT_MS; return new Promise((resolve, reject) => { const fail = (message) => { clearTimeout(timer); this.#pending.delete(command.id); reject(new AgentConsoleChannelError(message, APP_UNREACHABLE)); }; const timer = setTimeout(() => fail(`no instance of "${this.prefix}" answered within ${Math.round(timeoutMs / 100) / 10}s — the app may have refused the command (check its logs, or run \`node ace agora:doctor\` there)`), timeoutMs); timer.unref?.(); this.#pending.set(command.id, (reply) => { // `close()` settles with an unsigned reply of its own; anything else must carry the MAC. const local = !reply.ok && reply.error.code === APP_UNREACHABLE && this.#closed; // A reply without this command's MAC is someone else's (or forged): wait for the real one. if (!local && !outgoing.verifyReply(reply)) return; clearTimeout(timer); this.#pending.delete(command.id); if (!reply.ok && reply.error.code === UNAUTHORIZED_CONSOLE) sender.unauthorized?.(); resolve(reply); }); this.#client.publish(this.commandChannel, JSON.stringify(outgoing.wire)).then((receivers) => { // Redis says how many subscribers got it: none means no instance answers commands. if (receivers === 0) { fail(`no instance of the "${this.prefix}" app listens for ${CONSOLE_TOPIC} console commands (is it up, with \`consoleCommands\` in config/${CONSOLE_TOPIC}.ts?)`); } }, (err) => fail(err instanceof Error ? err.message : String(err))); }); } /** * Answer the commands other apps' consoles send this app. Every instance subscribes; the one that * wins `SET <prefix>-agent-console-claim:<id> NX` runs the command and publishes the answer, so a * command runs once however many processes the app has. Throws when the channel has no * authentication, or when the client cannot run that `SET`: a command never runs without both. */ async answer(handler, options = {}) { if (this.#answering) return; if (this.#receivers.length === 0) throw this.#unsigned(); const claim = claimOf(this.#client); if (!claim) { throw new Error(`the "${this.prefix}" Redis connection cannot run \`SET key value PX ms NX\`, so ${CONSOLE_TOPIC} console commands cannot be claimed by one instance; refusing to answer them`); } this.#answering = true; const instance = randomUUID(); await this.#subscribe(this.commandChannel, (payload) => { // Never crash on a bad message: every failure is handed to `onError` and the message dropped. void this.#run(payload, instance, claim, handler).catch((error) => { try { options.onError?.(error); } catch { // A failing logger must not take the process down either. } }); }); } async #run(payload, instance, claim, handler) { let wire; try { wire = JSON.parse(payload); } catch { throw refusedCommand('not JSON'); } if (!wire || typeof wire !== 'object' || typeof wire.id !== 'string') { throw refusedCommand('no command id'); } let incoming = null; for (const receiver of this.#receivers) { incoming = (await receiver.unwrap?.(wire)) ?? null; if (incoming) break; } if (!incoming) { throw refusedCommand(`mode not accepted here (this app accepts: ${this.#acceptedModes.join(', ')}; the command came as ${modeOfWire(wire)})`); } // One instance runs (or refuses) it; a replay inside the freshness window reuses the id, and // the claim (kept 2 minutes) refuses it. const claimed = await claim(`${this.prefix}-${CONSOLE_TOPIC}-console-claim:${incoming.id}`, instance); if (claimed !== 'OK') { // Another instance won the claim (the normal case): silent. A command id this instance // already ran is a replay. if (this.#ranHere(incoming.id)) throw refusedCommand('replayed'); return; } this.#remember(incoming.id); if (!incoming.command) { await this.#client.publish(this.replyChannel, JSON.stringify(incoming.signReply({ id: incoming.id, ok: false, error: incoming.error ?? { message: 'refused', code: UNSIGNED_CHANNEL, status: 401 }, }))); // Answered (the console fetches a new token); logged here too, once per reason. throw refusedCommand('token refused'); } const command = incoming.command; let reply; try { const data = await handler({ ...command, args: Array.isArray(command.args) ? command.args : [], ...(command.capabilities !== undefined ? { capabilities: Array.isArray(command.capabilities) ? command.capabilities.filter((c) => typeof c === 'string') : [], } : {}), }); // `undefined` (an action with no answer) travels as `null`. reply = { id: command.id, ok: true, data: data === undefined ? null : data }; } catch (err) { const { code, status } = (err ?? {}); reply = { id: command.id, ok: false, error: { message: err instanceof Error ? err.message : String(err), ...(typeof code === 'string' ? { code } : {}), ...(typeof status === 'number' ? { status } : {}), }, }; } // MAC'd in its wire form (a `Date` becomes a string, `undefined` fields vanish), or the // console would compute another MAC and drop a genuine answer. await this.#client.publish(this.replyChannel, JSON.stringify(incoming.signReply(JSON.parse(JSON.stringify(reply))))); if (!reply.ok && reply.error.code === 'capabilities-required') { throw refusedCommand('capabilities-required'); } } /** Stop listening and fail the commands still waiting for an answer. */ async close() { this.#closed = true; for (const [id, settle] of this.#pending) { settle({ id, ok: false, error: { message: 'console channel closed', code: APP_UNREACHABLE }, }); } this.#pending.clear(); for (const unsubscribe of this.#subscriptions.splice(0)) unsubscribe(); this.#subscriber?.disconnect?.(); this.#subscriber = undefined; } } /** * The Redis client for `connection` (an `@adonisjs/redis` connection name; omit for its default), * resolved through the container's `redis` binding. */ export async function resolveRedisConnection(container, connection) { let redis; try { redis = (await container.make('redis')); } catch (error) { throw new Error(`[${PACKAGE}] the federated console needs @adonisjs/redis (no "redis" binding: ${error instanceof Error ? error.message : String(error)})`); } return redis.connection(connection); } /** How a command arrived on the wire, for the refusal reason. */ function modeOfWire(wire) { if (typeof wire.sealed === 'string') { if (wire.sealed.startsWith('agora:pk1:')) return 'token'; if (wire.sealed.startsWith('agora:ct1:')) return 'crypto'; return 'sealed with an unknown cipher'; } if (typeof wire.mac === 'string') return 'secret'; return 'unsigned'; } //# sourceMappingURL=redis_channel.js.map