@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
69 lines • 3.36 kB
JavaScript
import { createDecipheriv, createHash } from 'node:crypto';
import { createKeyring, open, seal, sealedStatus, } from '../crypto-kit/index.js';
/** Bound into every sealed value: the app's keys never open these values for another use. */
export const POPPY_CONNECTIONS_PURPOSE = 'agent:poppy-connections';
/** What a value is bound to: copying it to another row makes it unreadable. */
export const connectionStateAad = (owner, issuer) => `agent_poppy_connection.state:${owner}\n${issuer}`;
export const pendingFlowAad = (id) => `agent_poppy_connect.flow:${id}`;
/**
* Opens what agent sealed before the crypto kit (`v1.<iv>.<tag>.<body>`, AES-256-GCM under
* sha256("poppy-connections:v1:" + secret)). Read-only: new values use the crypto kit's format.
*/
export function legacySealerReader(secret) {
const raw = typeof secret === 'string' ? secret : secret.release();
const key = createHash('sha256').update(`poppy-connections:v1:${raw}`).digest();
return (sealed) => {
const [version, iv, tag, body, extra] = sealed.split('.');
if (version !== 'v1' || !iv || !tag || body === undefined || extra !== undefined) {
return undefined;
}
const decipher = createDecipheriv('aes-256-gcm', key, Buffer.from(iv, 'base64url'));
decipher.setAuthTag(Buffer.from(tag, 'base64url'));
return Buffer.concat([
decipher.update(Buffer.from(body, 'base64url')),
decipher.final(),
]).toString('utf8');
};
}
/**
* Seals what a connection keeps at rest — the refresh token, the DPoP and delegated private keys,
* the access tokens, and the sign-in flows — with the Agora crypto kit over Adonis' encryption,
* each value bound to its row. Values sealed by older versions still open; `agent:crypto:reseal`
* rewrites them.
*/
export class Sealer {
keyring;
/**
* `secret`: the app key (or `secret` of the config). It opens the values older versions sealed,
* and seals new ones unless `crypto` names `keys` or an `encryption`.
*/
constructor(secret, crypto = {}) {
const raw = typeof secret === 'string' ? secret : secret?.release?.();
if (typeof raw !== 'string' || raw.length < 16) {
throw new Error('[@adonis-agora/agent] poppy-connections: the sealing secret must be ≥ 16 chars');
}
// Old values were sealed with the sealing secret of the time: any secret still configured.
const secrets = new Set([raw]);
for (const key of crypto.keys ?? [])
secrets.add(typeof key === 'string' ? key : key.release());
const source = crypto.keys?.length || !crypto.encryption
? { keys: crypto.keys?.length ? crypto.keys : [raw] }
: { encryption: crypto.encryption };
this.keyring = createKeyring({
purpose: POPPY_CONNECTIONS_PURPOSE,
...source,
...(crypto.kms ? { kms: crypto.kms } : {}),
legacy: [...secrets].map(legacySealerReader),
});
}
seal(value, aad) {
return seal(this.keyring, JSON.stringify(value), { aad });
}
async open(sealed, aad) {
return JSON.parse(await open(this.keyring, sealed, { aad }));
}
status(sealed, aad) {
return sealedStatus(this.keyring, sealed, { aad });
}
}
//# sourceMappingURL=crypto.js.map